### Example Project Setup Source: https://www.evosuite.org/documentation/commandline Demonstrates setting up EvoSuite for a project named 'test', specifying its build classes and a library jar. This command should be run from the project's root directory. ```bash $ cd test $ java -jar /path/to/evosuite.jar -setup build/classes lib/foo.jar ``` -------------------------------- ### List All Parameters Source: https://www.evosuite.org/documentation/commandline Execute this command to get a comprehensive list of all configurable parameters for EvoSuite. ```bash java -jar evosuite.jar -listParameters ``` -------------------------------- ### Install Python dependencies Source: https://www.evosuite.org/documentation/tutorial-part-3 Use easy_install to set up the required libraries for data processing and visualization. ```bash easy_install numpy easy_install matplotlib easy_install pandas ``` -------------------------------- ### Invoke EvoSuite help goal Source: https://www.evosuite.org/documentation/tutorial-part-2 Run this command to verify the plugin installation and display available goals. ```bash mvn evosuite:help ``` -------------------------------- ### Download and Extract Tutorial Archive Source: https://www.evosuite.org/documentation/tutorial-part-2 Downloads the example project archive and extracts its contents to the local directory. ```bash wget http://evosuite.org/files/tutorial/Tutorial_Maven.zip unzip Tutorial_Maven.zip ``` -------------------------------- ### Verify Java Installation Source: https://www.evosuite.org/documentation/tutorial-part-1 Check if the Java compiler is available on the system path. ```bash javac -version ``` ```text javac 1.8.0_51 ``` -------------------------------- ### Generated test suite example Source: https://www.evosuite.org/documentation/tutorial-part-3 Displays a sample generated test case from the EvoSuite output directory. ```java @Test(timeout = 4000) public void test0() throws Throwable { Company company0 = new Company(""); String string0 = company0.getName(); assertEquals("", string0); } ``` -------------------------------- ### Verify Maven Installation Source: https://www.evosuite.org/documentation/tutorial-part-1 Check the installed version of Apache Maven to ensure compatibility. ```bash mvn -version ``` ```text Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T16:41:47+00:00) ``` -------------------------------- ### Maven Test Execution Output Source: https://www.evosuite.org/documentation/tutorial-part-2 Example output showing a successful test run. ```text ------------------------------------------------------- T E S T S ------------------------------------------------------- Running tutorial.StackTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.094 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.799 s [INFO] Finished at: 2016-04-04T10:08:03+01:00 [INFO] Final Memory: 16M/142M [INFO] ------------------------------------------------------------------------ ``` -------------------------------- ### Example Class Structure for Foo Source: https://www.evosuite.org/documentation/tutorial-part-4 A simple Java class with three methods, used as an example for demonstrating pairwise method coverage. ```java public class Foo { public void bar() { ... } public void foo() { ... } public void zoo() { ... } } ``` -------------------------------- ### Install SciPy for statistical testing Source: https://www.evosuite.org/documentation/tutorial-part-3 Install the SciPy library to enable access to statistical functions like the Wilcoxon rank test. ```bash easy_install scipy ``` -------------------------------- ### Setup EvoSuite Project Configuration Source: https://www.evosuite.org/documentation/commandline Initializes EvoSuite configuration files within a project directory. Requires the target (class, package, or classpath entry) and a list of classpath entries. ```bash java -jar evosuite.jar -setup [ ...] ``` -------------------------------- ### Get help for export goal Source: https://www.evosuite.org/documentation/tutorial-part-2 Retrieve detailed parameter information for the EvoSuite export goal. ```bash mvn evosuite:help -Ddetail=true -Dgoal=export ``` -------------------------------- ### Setup EvoSuite Configuration Source: https://www.evosuite.org/documentation/tutorial-part-3 Configure EvoSuite by creating an evosuite.properties file. This command sets the classpath, including the compiled classes and necessary dependencies, for EvoSuite to use. ```bash $EVOSUITE -setup target/classes target/dependency/commons-collections-3.2.2.jar ``` -------------------------------- ### Example test execution output Source: https://www.evosuite.org/documentation/tutorial-part-2 Sample console output showing successful execution of generated tests. ```text ------------------------------------------------------- T E S T S ------------------------------------------------------- Running tutorial.LinkedList_ESTest Tests run: 10, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.192 sec Running tutorial.LinkedListIterator_ESTest Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.356 sec Running tutorial.Node_ESTest Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.024 sec Running tutorial.Stack_ESTest Tests run: 9, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.051 sec Running tutorial.StackTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec Results : Tests run: 31, Failures: 0, Errors: 0, Skipped: 0 ``` -------------------------------- ### Example statistics output Source: https://www.evosuite.org/documentation/tutorial-part-3 Sample content of the generated statistics.csv file after running the branch coverage experiment. ```csv TARGET_CLASS,criterion,Size,Length,MutationScore tutorial.ATM,BRANCH,10,75,0.3888888888888889 tutorial.ATMCard,BRANCH,8,40,1.0 tutorial.Bank,BRANCH,4,15,0.8 tutorial.BankAccount,BRANCH,2,6,0.8 tutorial.Owner,BRANCH,1,1,1.0 tutorial.CurrentAccount,BRANCH,2,7,0.6521739130434783 tutorial.SavingsAccount,BRANCH,2,7,0.8529411764705882 tutorial.Company,BRANCH,1,2,1.0 tutorial.Person,BRANCH,2,4,0.0 ``` -------------------------------- ### Example test execution output without custom source Source: https://www.evosuite.org/documentation/tutorial-part-2 Sample output showing that tests in custom directories are not picked up by default. ```text ------------------------------------------------------- T E S T S ------------------------------------------------------- Running tutorial.StackTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.079 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 ``` -------------------------------- ### Get detailed help for a plugin goal Source: https://www.evosuite.org/documentation/tutorial-part-2 Use the -Ddetail and -Dgoal flags to inspect parameters for a specific plugin goal. ```bash mvn evosuite:help -Ddetail=true -Dgoal=generate ``` -------------------------------- ### Generate tests with EvoSuite Source: https://www.evosuite.org/documentation/tutorial-part-2 Execute the generate goal to start the automatic test generation process. ```bash mvn evosuite:generate ``` -------------------------------- ### Example statistical output Source: https://www.evosuite.org/documentation/tutorial-part-3 Sample output format showing effect size and p-values for different metrics. ```text Size: 0.76 (0.000402) Length: 0.38 (0.000430) MutationScore: 0.67 (0.006838) ``` -------------------------------- ### Limit Search Budget and Stopping Condition Source: https://www.evosuite.org/documentation/commandline Example of setting a search budget to 1 minute and specifying the stopping condition as maximum time. Useful for controlling test generation duration. ```bash java -jar evosuite.jar -generateSuite -Dsearch_budget=60 -Dstopping_condition=MaxTime ``` -------------------------------- ### Successful Test Execution Output Source: https://www.evosuite.org/documentation/tutorial-part-1 Example output indicating successful execution of 5 EvoSuite tests. ```text JUnit version 4.12 ..... Time: 2.021 OK (5 tests) ``` -------------------------------- ### Initialize test method for method pair fitness Source: https://www.evosuite.org/documentation/tutorial-part-4 Start the test method for verifying method pair fitness on the observer class. ```java @Test public void testMethodPairFitness() { EvoSuite evosuite = new EvoSuite(); ``` -------------------------------- ### Example statistics.csv content Source: https://www.evosuite.org/documentation/tutorial-part-3 Shows the expected header and data row format in the generated statistics file. ```csv TARGET_CLASS,criterion,Size,Length,MutationScore tutorial.Company,BRANCH,1,2,1.0 ``` -------------------------------- ### Define SingleMethod example class Source: https://www.evosuite.org/documentation/tutorial-part-4 A trivial class used for testing basic coverage scenarios. ```java public class SingleMethod { public String foo(){ return "foo"; } } ``` -------------------------------- ### View statistics report content Source: https://www.evosuite.org/documentation/tutorial-part-3 Examples of the CSV output format generated by EvoSuite after test execution. ```csv TARGET_CLASS,criterion,Coverage,Total_Goals,Covered_Goals tutorial.Person,LINE;BRANCH;EXCEPTION;WEAKMUTATION;OUTPUT;METHOD;METHODNOEXCEPTION;CBRANCH,1.0,25,25 ``` ```csv TARGET_CLASS,criterion,Coverage,Total_Goals,Covered_Goals tutorial.Person,LINE;BRANCH;EXCEPTION;WEAKMUTATION;OUTPUT;METHOD;METHODNOEXCEPTION;CBRANCH,1.0,25,25 tutorial.Person,BRANCH,1.0,3,3 ``` ```csv TARGET_CLASS,criterion,Coverage,Total_Goals,Covered_Goals tutorial.Person,LINE;BRANCH;EXCEPTION;WEAKMUTATION;OUTPUT;METHOD;METHODNOEXCEPTION;CBRANCH,1.0,25,25 tutorial.Person,BRANCH,1.0,3,3 tutorial.Company,LINE,1.0,3,3 ``` -------------------------------- ### Generate equals Method Source: https://www.evosuite.org/documentation/tutorial-part-4 Provides an example of an automatically generated 'equals' method for comparing MethodPairTestFitness objects. It checks for object identity, nulls, and class type, then compares relevant fields. ```java @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; MethodPairTestFitness that = (MethodPairTestFitness) o; if (className != null ? !className.equals(that.className) : that.className != null) return false; if (methodName1 != null ? !methodName1.equals(that.methodName1) : that.methodName1 != null) return false; return methodName2 != null ? methodName2.equals(that.methodName2) : that.methodName2 == null; } ``` -------------------------------- ### Define MethodPairCoverageSystemTest class Source: https://www.evosuite.org/documentation/tutorial-part-4 Create a test class extending SystemTestBase to inherit necessary setup and cleanup for deterministic testing. ```java public class MethodPairCoverageSystemTest extends SystemTestBase { // TODO } ``` -------------------------------- ### Generate hashCode Method Source: https://www.evosuite.org/documentation/tutorial-part-4 Provides an example of an automatically generated 'hashCode' method for MethodPairTestFitness objects. It calculates a hash code based on the class name, method name 1, and method name 2. ```java @Override public int hashCode() { int result = className != null ? className.hashCode() : 0; result = 31 * result + (methodName1 != null ? methodName1.hashCode() : 0); result = 31 * result + (methodName2 != null ? methodName2.hashCode() : 0); ``` -------------------------------- ### Download Tutorial Files Source: https://www.evosuite.org/documentation/tutorial-part-3 Use wget to download the tutorial experiment files and unzip to extract them. These files are necessary for the subsequent steps in the tutorial. ```bash wget http://evosuite.org/files/tutorial/Tutorial_Experiments.zip unzip Tutorial_Experiments.zip ``` -------------------------------- ### Navigate to Project Directory Source: https://www.evosuite.org/documentation/tutorial-part-1 Change the current working directory to the tutorial project folder. ```bash cd Tutorial_Stack ``` -------------------------------- ### Download and Extract Tutorial Project Source: https://www.evosuite.org/documentation/tutorial-part-1 Retrieve the sample Stack project archive and extract its contents. ```bash wget http://evosuite.org/files/tutorial/Tutorial_Stack.zip unzip Tutorial_Stack.zip ``` -------------------------------- ### Display Help Information Source: https://www.evosuite.org/documentation/commandline Run this command to view all available command-line options for EvoSuite. ```bash java -jar evosuite.jar -help ``` -------------------------------- ### Navigate to Project Directory Source: https://www.evosuite.org/documentation/tutorial-part-3 Change the current directory to the root of the extracted tutorial project. This is where Maven and EvoSuite commands will be executed. ```bash cd Tutorial_Experiments ``` -------------------------------- ### Run experiment with branch coverage Source: https://www.evosuite.org/documentation/tutorial-part-3 Executes EvoSuite on the tutorial package using branch coverage and specific output variables. ```bash $EVOSUITE -criterion branch -prefix tutorial -Doutput_variables=TARGET_CLASS,criterion,Size,Length,MutationScore ``` -------------------------------- ### View export goal parameters Source: https://www.evosuite.org/documentation/tutorial-part-2 Output showing the targetFolder parameter configuration. ```text Available parameters: targetFolder (Default: src/test/java) User property: targetFolder ``` -------------------------------- ### Compile Project with Maven Source: https://www.evosuite.org/documentation/tutorial-part-1 Build the project to generate the necessary bytecode for EvoSuite. ```bash mvn compile ``` ```text [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.577 s [INFO] Finished at: 2016-04-03T10:38:41+01:00 [INFO] Final Memory: 13M/114M [INFO] ------------------------------------------------------------------------ ``` -------------------------------- ### Basic EvoSuite Execution Source: https://www.evosuite.org/documentation/commandline The fundamental command to run EvoSuite. Replace 'evosuite.jar' with the actual path to the jar file and '' with desired parameters. ```bash java -jar evosuite.jar ``` -------------------------------- ### List Available EvoSuite Parameters Source: https://www.evosuite.org/documentation/tutorial-part-1 Displays a comprehensive list of all configurable properties for EvoSuite. ```bash $EVOSUITE -listParameters ``` -------------------------------- ### Run EvoSuite Executable Source: https://www.evosuite.org/documentation/tutorial-part-4 Execute the main EvoSuite JAR file using Java to view its usage instructions and help text. This confirms that the master module has been successfully built. ```bash java -jar master/target/evosuite-master-1.0.4-SNAPSHOT.jar ``` -------------------------------- ### Existing JUnit Test Class Source: https://www.evosuite.org/documentation/tutorial-part-1 This is an example of a JUnit test class that can be used with EvoSuite to indicate already covered code. ```java package tutorial; import org.junit.Test; import org.junit.Assert; public class StackTest { @Test public void test() { Stack stack = new Stack(); stack.push(new Object()); Assert.assertFalse(stack.isEmpty()); } } ``` -------------------------------- ### Generate, Export, and Test with EvoSuite Source: https://www.evosuite.org/documentation/maven-plugin Use this command to generate tests, export them to the standard test directory, and then execute all tests (including generated ones). Adjust memory and core counts as needed. ```bash mvn -DmemoryInMB=2000 -Dcores=2 evosuite:generate evosuite:export test ``` -------------------------------- ### Repeat EvoSuite Default Configuration Experiments Source: https://www.evosuite.org/documentation/tutorial-part-3 Run EvoSuite with the default configuration 5 times, specifying output variables. This allows for comparison with other configurations on average. ```bash for I in {1..5}; do $EVOSUITE -Dconfiguration_id=Default -prefix tutorial -Doutput_variables=configuration_id,TARGET_CLASS,criterion,Size,Length,MutationScore ; done ``` -------------------------------- ### Get Usable Methods in Java Source: https://www.evosuite.org/documentation/tutorial-part-4 Retrieves all declared methods of a class that are deemed usable by the TestUsageChecker. It filters methods based on accessibility rules. ```Java protected Set getUsableMethods(Class clazz) { Set methods = new LinkedHashSet<>(); Method[] allMethods= clazz.getDeclaredMethods(); for (Method m : allMethods) { if (TestUsageChecker.canUse(m)) { String methodName = m.getName()+ Type.getMethodDescriptor(m); methods.add(methodName); } } return methods; } ``` -------------------------------- ### Compile and Run Maven Tests Source: https://www.evosuite.org/documentation/tutorial-part-1 Use this Maven command to compile your project and run existing JUnit tests. Ensure your tests pass before proceeding with EvoSuite. ```bash mvn test ``` -------------------------------- ### Download EvoSuite JARs Source: https://www.evosuite.org/documentation/tutorial-part-1 Use wget to download the executable and standalone runtime JAR files for EvoSuite version 1.0.6. ```bash wget https://github.com/EvoSuite/evosuite/releases/download/v1.0.6/evosuite-1.0.6.jar wget https://github.com/EvoSuite/evosuite/releases/download/v1.0.6/evosuite-standalone-runtime-1.0.6.jar ``` -------------------------------- ### Navigate to Project Directory Source: https://www.evosuite.org/documentation/tutorial-part-2 Changes the current working directory to the extracted project folder. ```bash cd Tutorial_Maven ``` -------------------------------- ### Get Fitness Method in Java Source: https://www.evosuite.org/documentation/tutorial-part-4 Calculates the fitness of a given test suite. It runs the test suite, determines covered method pairs, and computes fitness based on uncovered goals. ```Java @Override public double getFitness(AbstractTestSuiteChromosome suite) { double fitness = 0.0; // TODO: calculate fitness value updateIndividual(this, suite, fitness); return fitness; } ``` -------------------------------- ### EvoSuite help output Source: https://www.evosuite.org/documentation/tutorial-part-2 Expected output when running the help goal for the EvoSuite Maven plugin. ```text [INFO] --- evosuite-maven-plugin:1.0.6:help (default-cli) @ Tutorial_Maven --- [INFO] Maven Plugin for EvoSuite 1.0.6 Plugin used to run EvoSuite to automatically generate high coverage JUnit tests This plugin has 7 goals: evosuite:clean Remove all local files created by EvoSuite so far evosuite:coverage Execute the manually written test suites (usually located under src/test/java) and return the coverage of each class. evosuite:export When run, EvoSuite generate tests in a specific folder. New runs of EvoSuite can exploit the tests in such folder, and/or modify them. So, with 'export' we can copy all generated tests to a specific folder, which by default points to where Maven searches for tests. If another folder is rather used (or if we want to run with Maven the tests in the default EvoSuite folder), then Maven plugins like build-helper-maven-plugin are needed evosuite:generate Generate JUnit tests evosuite:help Display help information on evosuite-maven-plugin. Call mvn evosuite:help -Ddetail=true -Dgoal= to display parameter details. evosuite:info Obtain info of generated tests so far evosuite:prepare Workaround mojo to overcome bug in Maven. Needed when EvoSuite tests are run together with manual ones [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.489 s [INFO] Finished at: 2016-04-04T10:55:45+01:00 [INFO] Final Memory: 9M/109M [INFO] ------------------------------------------------------------------------ ``` -------------------------------- ### PIT Dependency for Mutation Testing Source: https://www.evosuite.org/documentation/measuring-code-coverage Include the PIT (Project Independence Testing) parent dependency in your Maven project for mutation testing. This setup works out-of-the-box with EvoSuite's JavaAgent. ```xml org.pitest pitest-parent 1.3.2 pom ``` -------------------------------- ### Maven Jacoco Plugin for Offline Instrumentation Source: https://www.evosuite.org/documentation/measuring-code-coverage Configure Jacoco for offline instrumentation, ensuring the `jacoco.exec` file location is explicitly set. This setup is compatible with EvoSuite's environmental handling. ```xml org.jacoco jacoco-maven-plugin 0.8.0 ${project.build.directory}/coverage-reports/jacoco-ut.exec ${project.build.directory}/coverage-reports/jacoco-ut.exec ${project.reporting.outputDirectory}/jacoco-ut default-instrument instrument default-restore-instrumented-classes restore-instrumented-classes default-report report org.apache.maven.plugins maven-surefire-plugin 2.15 ${project.build.directory}/coverage-reports/jacoco-ut.exec ``` -------------------------------- ### Package EvoSuite Project Source: https://www.evosuite.org/documentation/tutorial-part-4 Invoke this command to package the EvoSuite project, generating the main JAR file and the standalone runtime JAR. Ensure you have configured your IDE as a Maven project for this to work correctly. ```bash mvn package ``` -------------------------------- ### Build Project with Maven Source: https://www.evosuite.org/documentation/tutorial-part-4 Build the EvoSuite project into a JAR file using Maven. This step is necessary to package the changes and make the new crossover operator available. ```bash mvn package ``` -------------------------------- ### Generate Tests Using Configuration File Source: https://www.evosuite.org/documentation/tutorial-part-1 Generates tests for the specified class, automatically using settings from the evosuite.properties file, including the project classpath. ```bash $EVOSUITE -class tutorial.Stack ``` -------------------------------- ### Generate Test Suite (Whole Suite Method) Source: https://www.evosuite.org/documentation/commandline Use this command to generate a complete test suite for a target. The target can be a class, a package prefix, or a classpath entry. ```bash java -jar evosuite.jar [options] ``` -------------------------------- ### Initialize test method for coverage fitness Source: https://www.evosuite.org/documentation/tutorial-part-4 Set up the EvoSuite instance and target class for the fitness test. ```java @Test public void testMethodFitnessSimpleExample() { EvoSuite evosuite = new EvoSuite(); String targetClass = SingleMethod.class.getCanonicalName(); Properties.TARGET_CLASS = targetClass; ``` -------------------------------- ### Execute EvoSuite and retrieve test suite Source: https://www.evosuite.org/documentation/tutorial-part-4 Run the command line parser and extract the best individual from the genetic algorithm result. ```java String[] command = new String[] { "-generateSuite", "-class", targetClass }; Object result = evosuite.parseCommandLine(command); GeneticAlgorithm ga = getGAFromResult(result); TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual(); ``` -------------------------------- ### Run Existing Tests Source: https://www.evosuite.org/documentation/tutorial-part-2 Executes the existing test suite using Maven. ```bash mvn test ``` -------------------------------- ### Generate Tests for a Class Source: https://www.evosuite.org/documentation/tutorial-part-1 Generate tests for the 'tutorial.Stack' class using EvoSuite. Specify the class to test and the project classpath, which includes the compiled class files. ```bash $EVOSUITE -class tutorial.Stack -projectCP target/classes ``` -------------------------------- ### List Testable Classes in a Target Directory Source: https://www.evosuite.org/documentation/tutorial-part-1 Use EvoSuite with the -listClasses argument to identify all testable classes within a specified classpath entry. ```bash $EVOSUITE -listClasses -target target/classes ``` -------------------------------- ### Run experiments with configuration IDs Source: https://www.evosuite.org/documentation/tutorial-part-3 Executes two separate runs with distinct configuration IDs to easily differentiate results in the output file. ```bash $EVOSUITE -Dconfiguration_id=Branch -criterion branch -prefix tutorial -Doutput_variables=configuration_id,TARGET_CLASS,criterion,Size,Length,MutationScore $EVOSUITE -Dconfiguration_id=Default -prefix tutorial -Doutput_variables=configuration_id,TARGET_CLASS,criterion,Size,Length,MutationScore ``` -------------------------------- ### Generate Tests with Custom Settings Source: https://www.evosuite.org/documentation/tutorial-part-1 Generates tests with a 20-second search budget, disabled minimization, and the 'all' assertion strategy for longer tests with more assertions. ```bash $EVOSUITE -class tutorial.Stack -projectCP target/classes -Dsearch_budget=20 -Dminimize=false -Dassertion_strategy=all ``` -------------------------------- ### Configure build-helper-maven-plugin Source: https://www.evosuite.org/documentation/tutorial-part-2 Add the build-helper-maven-plugin to the pom.xml to include custom test source directories. ```xml org.codehaus.mojo build-helper-maven-plugin 1.8 add-test-source generate-test-sources add-test-source ${targetFolder} ``` -------------------------------- ### Execute EvoSuite Jar Source: https://www.evosuite.org/documentation/tutorial-part-1 Run the EvoSuite executable JAR file to view its command-line options and usage information. ```bash java -jar evosuite-1.0.6.jar ``` -------------------------------- ### Assert coverage goals and optimality Source: https://www.evosuite.org/documentation/tutorial-part-4 Verify that the number of goals matches expectations and that full coverage was achieved. ```java int goals = TestGenerationStrategy.getFitnessFactories().get(0).getCoverageGoals().size(); // assuming single fitness function Assert.assertEquals(2, goals ); Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001); } ``` -------------------------------- ### EvoSuite Usage Output Source: https://www.evosuite.org/documentation/tutorial-part-1 This is the typical output when executing the EvoSuite JAR without any arguments, showing available commands. ```text * EvoSuite 1.0.6 usage: EvoSuite ... ``` -------------------------------- ### Repeat EvoSuite Branch Coverage Experiments Source: https://www.evosuite.org/documentation/tutorial-part-3 Run EvoSuite with branch coverage 5 times, configuring the output variables. This helps in collecting data for average performance. ```bash for I in {1..5}; do $EVOSUITE -Dconfiguration_id=Branch -criterion branch -prefix tutorial -Doutput_variables=configuration_id,TARGET_CLASS,criterion,Size,Length,MutationScore ; done ``` -------------------------------- ### Implement getCoverageGoals Factory Method Source: https://www.evosuite.org/documentation/tutorial-part-4 Provides the logic to enumerate method pairs as coverage goals, including constructors and methods. ```java @Override public List getCoverageGoals() { List goals = new ArrayList<>(); // pair each constructor with each method and add to goals // pair each method with each other method and add to goals return goals; } ``` ```java @Override public List getCoverageGoals() { List goals = new ArrayList<>(); String className = Properties.TARGET_CLASS; Class clazz = Properties.getTargetClass(); Set constructors = getUsableConstructors(clazz); Set methods = getUsableMethods(clazz); for(String constructor : constructors) for(String method : methods) goals.add(new MethodPairTestFitness(className, constructor, method)); for(String method1 : methods) for(String method2 : methods) goals.add(new MethodPairTestFitness(className, method1, method2)); return goals; } ``` -------------------------------- ### Download JUnit and Hamcrest Dependencies Source: https://www.evosuite.org/documentation/tutorial-part-1 Use Maven to download necessary JUnit and Hamcrest JAR files into the target/dependency directory. ```bash mvn dependency:copy-dependencies ``` -------------------------------- ### Project Meta-information in pom.xml Source: https://www.evosuite.org/documentation/tutorial-part-2 Initial section of the pom.xml file defining project coordinates and metadata. ```xml 4.0.0 org.evosuite.tutorial Tutorial_Maven 1.0-SNAPSHOT jar Tutorial_Maven http://maven.apache.org ``` -------------------------------- ### Use EvoSuite Jar as Runtime Dependency Source: https://www.evosuite.org/documentation/commandline Alternatively, the main 'evosuite.jar' can be used as a runtime dependency. Note that this includes more libraries than the standalone runtime. ```bash evosuite.jar ``` -------------------------------- ### Copy Runtime Dependencies Source: https://www.evosuite.org/documentation/tutorial-part-3 Use the Maven dependency plugin to copy all runtime dependencies to the target/dependency directory. This is crucial for setting up the classpath when running EvoSuite directly. ```bash mvn dependency:copy-dependencies -DincludeScope=runtime ``` -------------------------------- ### Compile Project Classes Source: https://www.evosuite.org/documentation/tutorial-part-2 Compiles the project source code using Maven. ```bash mvn compile ``` -------------------------------- ### Set EvoSuite Parameters via Properties Source: https://www.evosuite.org/documentation/commandline Configure EvoSuite parameters directly on the command line using the '-Dkey=value' syntax. This is an alternative to editing configuration files. ```bash -Dkey=value ``` -------------------------------- ### Verify EvoSuite Classpath Configuration Source: https://www.evosuite.org/documentation/tutorial-part-3 Check the generated evosuite-files/evosuite.properties file to ensure the classpath is correctly set. This confirms that EvoSuite will have access to all required classes and libraries. ```properties CP=target/classes:target/dependency/commons-collections-3.2.2.jar ``` -------------------------------- ### Compile EvoSuite Tests Source: https://www.evosuite.org/documentation/tutorial-part-1 Compile the Java test files located in the evosuite-tests/tutorial directory using the Java compiler. ```bash javac evosuite-tests/tutorial/*.java ``` -------------------------------- ### Configure Surefire Plugin for Java Agent Source: https://www.evosuite.org/documentation/maven-plugin Register the InitializingListener in the Surefire plugin to support EvoSuite tests alongside existing tests. ```xml org.apache.maven.plugins maven-surefire-plugin 2.17 listener org.evosuite.runtime.InitializingListener ``` -------------------------------- ### Handle Long Classpaths on Windows Source: https://www.evosuite.org/documentation/commandline For Windows systems with limitations on command-line length, write the classpath to a file and use '-DCP_file_path' to reference it. ```bash -DCP_file_path= ``` -------------------------------- ### Clone EvoSuite Repository Source: https://www.evosuite.org/documentation/tutorial-part-4 Use this Git command to clone the EvoSuite source code from GitHub. This is the first step to building and modifying EvoSuite. ```bash git clone https://github.com/EvoSuite/evosuite.git ``` -------------------------------- ### Run EvoSuite Tests Source: https://www.evosuite.org/documentation/tutorial-part-1 Execute the compiled EvoSuite tests using the JUnitCore runner from the command line. ```bash java org.junit.runner.JUnitCore tutorial.Stack_ESTest ``` -------------------------------- ### Configure EvoSuite plugin in pom.xml Source: https://www.evosuite.org/documentation/tutorial-part-2 Add this build plugin configuration to the project's pom.xml file to enable EvoSuite integration. ```xml org.evosuite.plugins evosuite-maven-plugin 1.0.6 ``` -------------------------------- ### Print Evolved Test Suite Source: https://www.evosuite.org/documentation/tutorial-part-4 Outputs the generated test suite to standard output. ```java System.out.println("EvolvedTestSuite:\n" + best); ``` -------------------------------- ### Configure Coverage Timeline Tracking Source: https://www.evosuite.org/documentation/tutorial-part-3 Use the CoverageTimeline variable and timeline_interval property to sample branch coverage over a specified search budget. ```bash $EVOSUITE -class tutorial.ATM -criterion branch -Doutput_variables=TARGET_CLASS,BranchCoverage,CoverageTimeline -Dtimeline_interval=1000 -Dsearch_budget=30 ``` -------------------------------- ### Define ExampleObserverClass for testing Source: https://www.evosuite.org/documentation/tutorial-part-4 A class with multiple methods used to test more complex coverage scenarios. ```java public class ExampleObserverClass { private int privateMember = 0; public void setMember(int x) { privateMember = x; } public int getMember() { return privateMember; } } ``` -------------------------------- ### Add Custom Source Folder with Build Helper Source: https://www.evosuite.org/documentation/maven-plugin Use the build-helper-maven-plugin to include non-default test source directories in the build path. ```xml org.codehaus.mojo build-helper-maven-plugin 1.8 add-test-source generate-test-sources add-test-source ${customFolder} ``` -------------------------------- ### Set Project Classpath in Properties File Source: https://www.evosuite.org/documentation/tutorial-part-1 Automatically generates an evosuite.properties file to set the project classpath to 'target/classes'. This avoids specifying it on the command line for future runs. ```bash $EVOSUITE -setup target/classes ``` -------------------------------- ### Set EvoSuite Environment Variable (Linux/macOS) Source: https://www.evosuite.org/documentation/tutorial-part-1 Create an environment variable 'EVOSUITE' to simplify invoking EvoSuite from the command line. This sets it to use the Java command with the JAR file located in the current directory. ```bash export EVOSUITE="java -jar $(pwd)/evosuite-1.0.6.jar" ``` -------------------------------- ### Run EvoSuite with custom output variables Source: https://www.evosuite.org/documentation/tutorial-part-3 Executes EvoSuite with the -Doutput_variables property to specify which metrics to include in the report. ```bash $EVOSUITE -class tutorial.Company -criterion branch -Doutput_variables=TARGET_CLASS,criterion,Size,Length,MutationScore ``` -------------------------------- ### Set Explicit Search Budget for Test Generation Source: https://www.evosuite.org/documentation/tutorial-part-1 Configures EvoSuite to spend a maximum of 20 seconds on test generation using the -Dsearch_budget property. ```bash $EVOSUITE -class tutorial.Stack -projectCP target/classes -Dsearch_budget=20 ``` -------------------------------- ### Add EvoSuite Runtime to Classpath Source: https://www.evosuite.org/documentation/commandline When running generated JUnit tests, ensure the EvoSuite framework is available by adding 'evosuite-standalone-runtime.jar' to the classpath. ```bash evosuite-standalone-runtime.jar ``` -------------------------------- ### List generated test files Source: https://www.evosuite.org/documentation/tutorial-part-2 Locate the generated test suites and scaffolding files within the hidden .evosuite directory. ```bash ls .evosuite/best-tests/tutorial ``` -------------------------------- ### Initialize Method Pairs in Java Source: https://www.evosuite.org/documentation/tutorial-part-4 Initializes the set of method pair goals in the constructor of MethodPairSuiteFitness. It uses a MethodPairFactory to obtain coverage goals. ```Java public MethodPairSuiteFitness() { methodPairGoals.addAll(new MethodPairFactory().getCoverageGoals()); } ``` -------------------------------- ### EvoSuite System Test for NullString Source: https://www.evosuite.org/documentation/tutorial-part-4 This system test demonstrates how to use EvoSuite to generate a test suite for the NullString class. It configures EvoSuite, invokes the test generation process, and asserts the coverage goals and optimality. ```java public class NullStringSystemTest extends SystemTestBase { @Test public void testNullString() { EvoSuite evosuite = new EvoSuite(); String targetClass = NullString.class.getCanonicalName(); Properties.TARGET_CLASS = targetClass; String[] command = new String[] { "-generateSuite", "-class", targetClass }; Object result = evosuite.parseCommandLine(command); GeneticAlgorithm ga = getGAFromResult(result); TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual(); System.out.println("EvolvedTestSuite:\n" + best); int goals = TestGenerationStrategy.getFitnessFactories().get(0).getCoverageGoals().size(); // assuming single fitness function Assert.assertEquals("Wrong number of goals: ", 3, goals); Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001); } } ``` -------------------------------- ### MethodPairTestFitness Constructor and Getters Source: https://www.evosuite.org/documentation/tutorial-part-4 Provides the constructor to initialize the fitness function with method names and getter methods to access these properties. ```java public MethodPairTestFitness(String className, String methodName1, String methodName2) { this.className = className; this.methodName1 = methodName1; this.methodName2 = methodName2; } public String getClassName() { return className; } public String getMethodName1() { return methodName1; } public String getMethodName2() { return methodName2; } ``` -------------------------------- ### Specify Target as Package Prefix Source: https://www.evosuite.org/documentation/commandline To generate tests for all classes matching a prefix within the classpath, use the '-prefix' option. ```bash -prefix ``` -------------------------------- ### Set Project Classpath Source: https://www.evosuite.org/documentation/commandline Crucial option to specify the classpath for test generation. Ensure all necessary classes and libraries are included. ```bash -projectCP ``` -------------------------------- ### Specify Target as Classpath Entry Source: https://www.evosuite.org/documentation/commandline EvoSuite can generate tests for all classes within a specified jar file or directory using the '-target' option. ```bash -target ``` -------------------------------- ### Query EvoSuite project status Source: https://www.evosuite.org/documentation/tutorial-part-2 Check the current state of generated test suites and coverage metrics. ```bash mvn evosuite:info ``` -------------------------------- ### Run EvoSuite on All Classes in a Package Source: https://www.evosuite.org/documentation/tutorial-part-1 Invoke EvoSuite to generate tests for all classes within a specified package. This is an alternative to targeting a directory. ```bash $EVOSUITE -prefix tutorial ``` -------------------------------- ### Compile Project Classes with Maven Source: https://www.evosuite.org/documentation/tutorial-part-3 Compile the Java classes within the Maven project. This step ensures that the code is ready for test generation. ```bash mvn compile ``` -------------------------------- ### Print coverage goals and results Source: https://www.evosuite.org/documentation/tutorial-part-4 Output the evolved test suite and identified coverage goals for debugging purposes. ```java System.out.println("EvolvedTestSuite:\n" + best); for(TestFitnessFunction goal : TestGenerationStrategy.getFitnessFactories().get(0).getCoverageGoals()) { System.out.println("Goal: "+goal); } ``` -------------------------------- ### Run EvoSuite on All Classes in a Classpath Entry Source: https://www.evosuite.org/documentation/tutorial-part-1 Execute EvoSuite on every testable class found within the provided classpath entry. This is useful for generating comprehensive test suites. ```bash $EVOSUITE -target target/classes ``` -------------------------------- ### Export tests to source tree Source: https://www.evosuite.org/documentation/tutorial-part-2 Move generated test suites from the .evosuite directory to the standard src/test/java Maven directory. ```bash mvn evosuite:export ``` -------------------------------- ### Implement compareTo for TestFitnessFunction Source: https://www.evosuite.org/documentation/tutorial-part-4 Implements the compareTo method to allow sorting of TestFitnessFunction instances based on class and method names. ```java @Override public int compareTo(TestFitnessFunction other) { if (other instanceof MethodPairTestFitness) { MethodPairTestFitness otherMethodFitness = (MethodPairTestFitness) other; if (className.equals(otherMethodFitness.getClassName())) { if(methodName1.equals(otherMethodFitness.getMethodName1())) return methodName2.compareTo(otherMethodFitness.getMethodName2()); else return methodName1.compareTo(otherMethodFitness.getMethodName1()); } else return className.compareTo(otherMethodFitness.getClassName()); } return compareClassName(other); } ``` -------------------------------- ### Invoke EvoSuite on a class Source: https://www.evosuite.org/documentation/tutorial-part-3 Run EvoSuite on a specific target class to generate test suites and statistics. ```bash $EVOSUITE -class tutorial.Person ``` ```bash $EVOSUITE -class tutorial.Person -criterion branch ``` ```bash $EVOSUITE -class tutorial.Company -criterion line ``` -------------------------------- ### Invoke EvoSuite via Environment Variable (Windows) Source: https://www.evosuite.org/documentation/tutorial-part-1 Invoke EvoSuite on Windows using the defined environment variable. This is the Windows equivalent of the Linux/macOS command. ```batch %EVOSUITE% ``` -------------------------------- ### EvoSuite Properties Configuration Source: https://www.evosuite.org/documentation/tutorial-part-4 Define the new crossover function as a static field in the Properties class. Use the @Parameter annotation to specify its command-line key and description. ```java import org.evosuite.utils.generic.Parameter; public class Properties { // ... other properties ... @Parameter(key = "crossover_function", group = "Search Algorithm", description = "Crossover function during search") public static CrossoverFunction CROSSOVER_FUNCTION = CrossoverFunction.SINGLEPOINTRELATIVE; // ... other properties ... } ``` -------------------------------- ### Run EvoSuite in parallel Source: https://www.evosuite.org/documentation/tutorial-part-2 Use the -Dcores property to specify the number of parallel jobs for test generation. ```bash mvn -Dcores=4 evosuite:generate ``` -------------------------------- ### Set EvoSuite Environment Variable (Windows) Source: https://www.evosuite.org/documentation/tutorial-part-1 Set the 'EVOSUITE' environment variable on Windows. It uses the Java command with the JAR file in the current directory. ```batch set EVOSUITE=java -jar "%CD%"\evosuite-1.0.6.jar ``` -------------------------------- ### Generate Tests (Individual Goal Method) Source: https://www.evosuite.org/documentation/commandline This command generates tests by addressing each test goal individually, useful for fine-grained coverage. The target can be a class, package, or classpath entry. ```bash java -jar evosuite.jar -generateTests [options] ``` -------------------------------- ### Initialize Fitness Value Source: https://www.evosuite.org/documentation/tutorial-part-4 Initialize the fitness value. A value of 1.0 is used, with 0.0 representing optimal fitness. ```java double fitness = 1.0; ``` -------------------------------- ### Set CLASSPATH for Java Compilation (Windows) Source: https://www.evosuite.org/documentation/tutorial-part-1 Set the CLASSPATH environment variable on Windows systems using a semicolon as the path separator. ```cmd set CLASSPATH=target/classes:evosuite-standalone-runtime-1.0.6.jar;evosuite-tests;target/dependency/junit-4.12.jar;target/dependency/hamcrest-core-1.3.jar ``` -------------------------------- ### Configure target folder in pom.xml Source: https://www.evosuite.org/documentation/tutorial-part-2 Set the targetFolder property within the project's pom.xml file. ```xml src/test/evosuite ``` -------------------------------- ### Implement getUsableConstructors Helper Source: https://www.evosuite.org/documentation/tutorial-part-4 Uses reflection to retrieve and format usable constructors for the target class. ```java protected Set getUsableConstructors(Class clazz) { Set constructors = new LinkedHashSet<>(); Constructor[] allConstructors = clazz.getDeclaredConstructors(); for (Constructor c : allConstructors) { if (TestUsageChecker.canUse(c)) { String methodName = "" + Type.getConstructorDescriptor(c); constructors.add(methodName); } } return constructors; } ``` -------------------------------- ### Configure EvoSuite Standalone Runtime in Gradle Source: https://www.evosuite.org/documentation/intellij-idea-plugin Add the EvoSuite Maven repository and the standalone runtime dependency to your Gradle build file to compile and run generated tests. ```gradle repositories { maven { url "http://www.evosuite.org/m2" } } dependencies { testCompile 'org.evosuite:evosuite-standalone-runtime:1.0.1' } ``` -------------------------------- ### Set CLASSPATH for Java Compilation (Unix/Linux) Source: https://www.evosuite.org/documentation/tutorial-part-1 Set the CLASSPATH environment variable on Unix-like systems to include all required directories and JAR files for compiling tests. ```bash export CLASSPATH=target/classes:evosuite-standalone-runtime-1.0.6.jar:evosuite-tests:target/dependency/junit-4.12.jar:target/dependency/hamcrest-core-1.3.jar ```