Software Testing Lab 5: Automated Unit Test Generation
Software Testing Lab 5: Automated Unit Test Generation
Software Testing Lab 5: Automated Unit Test Generation
1 Introduction
The goal of this lab is to introduce automated test generation. Writing tests manually can be
difficult and time-consuming. Automating test generation will speed up the software
development process. It is important to understand the strengths and weaknesses of the tools
you are using. Automatically generated tests can sometimes include test cases the developer
did not think of but also test cases the developer would not think of.
The second system under test is the point-of-sale system from the course Software Engineering
(LTAT.05.003). It is a small application to manage items in stock and make sale transactions.
The stock is managed by the class InMemorySalesSystemDAO and purchases are made using
the ShoppingCart class.
3 Randoop
3.1 Introduction
Randoop is a tool used to automatically generate unit tests. The tests generated by Randoop
are classified either as error-revealing tests or as regression tests.
Error-revealing tests are tests which have found a bug. Sometimes the errors revealed are not
too serious and it is up to the programmer to decide to fix the bug. Randoop can expect
exceptions if they are caught properly in the code. Randoop will also expect a
NullPointerException, if null is directly passed as an argument to a method.
Regression tests are tests which passed and therefore did not find any errors. The purpose of
regression tests is to alert you to changes in the program’s behavior after changing the code. It
is important to analyze those tests in the regression test suite that are failing as a result of
changing the code. If your new code is incorrect and has introduced a fault, the code should be
fixed. However, if the changes were intentional, then the test suite is out of date and should be
discarded. This may be because the tests were too sensitive to changes. After discarding the
old (regression) test suite, a new test suite should be generated.
Figure 1 describes the general test generation and execution workflow when using Randoop.
3.2 Setup
In this lab, we are generating unit tests using Randoop. To download the latest version of
Randoop, go to: https://github.com/randoop/randoop/releases/latest. Unzip the downloaded file,
inside you should see three .jar files and the readme file.
Randoop requires Java 8 to work, which can be downloaded here:
https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html.
Choose the JDK which corresponds to your operating system and install it.
3.3 Usage
In the process of generating tests, Randoop will generate test sequence .class files. These are
useful for a larger production environment to guarantee that identical test sequences would be
generated when generating a new test suite. For the purposes of this lab, they may be deleted.
You may run into some technical issues, below are some examples to help you.
Error: Unable to load class "MyExampleClass" due to exception:
java.lang.NoClassDefFoundError: MyExampleClass. To fix this error change MyExampleClass
to ee.ut.cs.swt.example.MyExampleClass
Error: java.lang.IllegalStateException: Cannot find the Java compiler. Check that classpath
his error is thrown when you are using Java 9 or above, use Java 8 instead.
includes tools.jar. T
5.2.1 Download the project file from the course page and unzip it. Open the project in your IDE
5.2.2 Generate a test suite using Randoop for the POS system using a reasonable amount of
time for the time-limit parameter. Move the tests into the test folder of the project and run the
tests using your IDE. It may be necessary to specify the correct package for the tests by adding
package ee.ut.math.tvt.salessystem; t o the start of all the files containing the tests. Keep the
test suite, since you have to run it again in step 5.2.6. NB! Also take screenshots of all runs of
Randoop, as in Task 5.1! You will need to submit them.
NBB! You will also need to submit the test suite generated in this subtask!
5.2.3 If Randoop generates error-revealing tests, analyze the tests and explain the failure(s)
discovered in the report. Point out the block or line of code, which caused the exception. You
will not have to fix the bug(s) found, only report them.
5.2.4 Include a Randoop-generated error-revealing test case in the report
5.2.5 Implement the TODO in ShoppingCart class by uncommenting the code under the TODO.
The purpose of the TODO is to increase the quantity of the item in the shopping cart if the stock
item is already in the cart.
5.2.6 Run the test suite generated in step 5.2.2 again. Look at some of the failing tests in the
regression test suite and explain in the report if the tests reveal a bug you’ve introduced or
whether the test suite has become outdated. You may need to investigate the code more
in-depth. Include a failing regression test in the report.
5.2.7 Move the current test suite and generate a new test suite. Find a test case which contains
the same key steps as a failing test from task 5.2.6 and compare the two tests (a screenshot of
a test generated in this subtask and a brief explanation). Explain why Randoop handles
NullPointerExceptions differently in some cases. Hint: Look at how Randoop handles the
exception. NB! You will also need to submit the test suite generated in this subtask!
5.2.8 Assume that you want Randoop to classify the passing test (and other essentially similar
tests) from task 5.2.7 as error-revealing. Generate a new test suite, which classifies these tests
as error-revealing. Include a Randoop-generated error-revealing test case in the report. Hint:
checked exceptions.
6.1 Submission
For the homework, you have to submit a ZIP-file at the course lab submission page, that
includes the following:
● A report PDF, which includes the following points:
○ The table from Task 1, filled as required in subtasks 5.1.3, 5.1.6, 5.1.10
○ Answer to subtask 5.1.4
○ A test case with a valid input date, generated by Randoop (subtask 5.1.8)
○ Answer to subtask 5.1.9
○ Answer to subtask 5.2.3
○ An error-revealing test case, generated by Randoop (subtask 5.2.4)
○ Answer to subtask 5.2.6, including the now-failing test case from the regression
test suite
○ Answer to subtask 5.2.7, including a test case from the new test suite
○ An error-revealing test case, generated by Randoop (subtask 5.2.8)
● Literals file used in Task 1 (subtask 5.1.7)
● Screenshots of Randoop commands (for details see subtasks 5.1.2 and 5.2.2)
● Test Suites generated by Randoop
○ The last test suite from Task 5.1, subtask 5.1.5, where literals were used
○ The first test suite from Task 5.2, subtask 5.2.2
○ The second test suite from Task 5.2, subtask 5.2.7
6.2 Grading
START CLASSLITERALS
CLASSNAME
classname
LITERALS
type:value
...
type:value
END CLASSLITERALS
* If none of the default parameter settings has been changed, then write ‘none’.