Week 3 Introduction to Automated Test Generation with EvoSuite
In this week’s lab you will:
- Use Docker basics and file transfer (local ↔ container)
- Understand why EvoSuite exists and how it works (evolutionary algorithm)
- Run EvoSuite to generate tests for a simple Java project
- Observe the structure and style of EvoSuite-generated tests
Tutorials are one hour. Work through the core activities first.
Prerequisites
- Docker — installed (lab machines or Docker docs). Course image:
comp4130-env:1.0.tar. - Java — familiarity from Week 2; EvoSuite in the course image may use Java 8 for generation.
- (Optional) IntelliJ — to open generated tests locally.
Outline (1 hour)
| Part | Activity | Time (guide) |
|---|---|---|
| 1 | Docker setup and file transfer | ~15 min |
| 2 | EvoSuite introduction | ~10 min |
| 3 | Run EvoSuite on a simple project | ~25 min |
| 4 | Observe generated test structure and style | ~10 min |
Activity 1: Docker setup and file transfer (~15 min)#
Task 1.1: Verify Docker#
- Check version:
docker --version - Run a test container:
docker run hello-world - If using the course Docker image: load it with
docker load -i comp4130-env-1.0.tar. (If you have already loaded it during Tutorial Week 2, you do not need to do it again.) Then start the container using:docker run -it comp4130-env:1.0.
Docker provides a consistent environment so that tools such as EvoSuite and Defects4J run the same way on different machines. You can download the Docker image from the following link: https://drive.google.com/file/d/1TC9172fUuA6LiFaU923BE9av7-_z3Sk5/view?usp=sharing
Task 1.2: File transfer (host ↔ container)#
Copy from host to container:
docker cp /path/to/local/file container-id:/path/inside/container
Copy from container to host:
docker cp container-id:/path/inside/container /path/on/host
Copy a text file (create one using vi hello-world.txt) into the container:
docker cp ./hello-world.txt container-id:/COMP4130_Assignment_1
Example: Copy generated tests out:
docker cp container-id:/COMP4130_Assignment_1/evosuite-1.0.6.jar ./
Use docker ps -a to see container names/IDs. Replace evosuite-container with your container name.
Activity 2: EvoSuite introduction (~10 min)#
Why EvoSuite?#
Writing tests by hand is slow and may miss edge cases. EvoSuite is an automated unit test generator for Java that uses evolutionary algorithms to maximise coverage and produce JUnit tests.
How EvoSuite works (high level)#
- Input: Project (classpath) and a target class.
- Initial population: Random or seeded test cases.
- Fitness: Evaluate each test by coverage (e.g. branch, line).
- Selection & crossover: Keep better tests, combine them to form new ones.
- Mutation: Random changes to explore new inputs.
- Repeat until a time/coverage limit; output the best test suite.
Activity 3: Run EvoSuite on a simple project (~25 min)#
Task 3.1: Create a Simple Project and Upload to Your Container#
- Start the container:
docker start -ai CONTAINER_ID - Download a demo project from the following link: https://drive.google.com/file/d/1B3reAzsG7_VMEU8af19cdDgwjxY0LzHz/view?usp=drive_link
-
Unzip the downloaded file on your local machine. You should obtain a folder named
JustATest. - Copy the JustATest folder into the container using:
docker cp path/xxx/JustATest your-container:/COMP4130_Assignment_1. E.g.,docker cp JustATest be09b8ef5ee0:/COMP4130_Assignment_1.
Task 3.2: Run EvoSuite#
Navigate to the project root directory inside the container (e.g., /COMP4130_Assignment_1/JustATest).
- Compile the project:
mvn compile - Run EvoSuite from this directory:
java -jar ../evosuite-1.0.6.jar -class org.example.calculator -projectCP target/classes -target target/classes
- -class: fully qualified class to test
- -projectCP: classpath
- -target: directory of compiled classes
EvoSuite will generate test cases in the evosuite-tests/ directory (e.g., calculator_ESTest.java).
Use the following command to switch your Java version to Java 1.8:
update-alternatives --config java.
Task 3.3: Copy results to host (optional)#
From a new terminal on the host:
docker cp your-container:/workspace/a-project/evosuite-tests ./your-folder
or directly copy the whole project, e.g., docker cp container-id:/COMP4130_Assignment_1/JustATest ./
Open evosuite-results in IntelliJ to inspect the generated tests.
You have run EvoSuite and obtained generated JUnit tests for the Calculator class.
Activity 4: Observe EvoSuite-generated test structure and style (~10 min)#
Task 4.1: Open and inspect generated tests#
- Open the generated test file (e.g.
calculator_ESTest.java). - Note: annotations (e.g.
@Test), variable names (e.g.calculator0,int0), input values and assertions. - Compare with a hand-written test from Week 2: readability, naming, and coverage focus.
Think about the following questions:
Why EvoSuite uses names like calculator0 and values like 42 or -1?
What are pros and cons of this style vs hand-written tests?
Task 4.2: Run generated tests (if time)#
Compile and run the generated tests (e.g. add them to a Maven/IDE project with the same classpath, or use the project’s test runner). Confirm they pass and cover the target class.
Learning objectives#
By the end of this tutorial you should be able to:
- Use Docker to run the course environment and transfer files
- Explain EvoSuite’s evolutionary approach at a high level
- Run EvoSuite on a simple Java class and locate generated tests
- Describe typical structure and style of EvoSuite-generated tests and compare with hand-written tests