Search in sources :

Example 1 with TestCaseMinimizer

use of org.evosuite.testcase.TestCaseMinimizer in project evosuite by EvoSuite.

the class TestCaseReplacer method replaceTest.

/**
 * Given a test, create a GA and look for a replacement test
 *
 * @param test
 */
public TestCase replaceTest(String targetClass, List<TestCase> otherTests, TestCase test) {
    // Various environmental setup necessary for EvoSuite
    Properties.ALGORITHM = Algorithm.MONOTONICGA;
    Properties.STRATEGY = Strategy.ONEBRANCH;
    ExecutionTracer.enableTraceCalls();
    // Run for 10 generations - adapt as necessary
    // Properties.STOPPING_CONDITION = StoppingCondition.MAXGENERATIONS;
    // Properties.SEARCH_BUDGET = 20;
    // Properties.STOPPING_CONDITION = StoppingCondition.MAXTIME;
    // Properties.SEARCH_BUDGET = 20;
    // GeneticAlgorithm ga = TestSuiteGenerator.getGeneticAlgorithm(new RandomLengthTestFactory());
    // TODO: JM: Needs Testing. Not sure if this is equivalent:
    PropertiesTestGAFactory algorithmFactory = new PropertiesTestGAFactory();
    GeneticAlgorithm<TestChromosome> ga = algorithmFactory.getSearchAlgorithm();
    List<TestFitnessFactory<? extends TestFitnessFunction>> factories = TestSuiteGenerator.getFitnessFactories();
    Collection<TestFitnessFunction> fitnessFunctions = new ArrayList<TestFitnessFunction>();
    for (TestFitnessFactory<? extends TestFitnessFunction> factory : factories) {
        // Set up fitness function for the parsed test case
        DifferenceFitnessFunction fitnessFunction = new DifferenceFitnessFunction(test, otherTests, factory);
        // ga.setFitnessFunction(fitness);
        fitnessFunctions.add(fitnessFunction);
        ga.addFitnessFunction(fitnessFunction);
    }
    // Perform calculation
    ga.generateSolution();
    // The best individual at the end of the search contains our candidate solution
    TestChromosome testChromosome = (TestChromosome) ga.getBestIndividual();
    TestCaseMinimizer minimizer = new TestCaseMinimizer(fitnessFunctions);
    minimizer.minimize(testChromosome);
    System.out.println("Best individual has fitness: " + testChromosome.getFitness());
    return testChromosome.getTestCase();
}
Also used : TestCaseMinimizer(org.evosuite.testcase.TestCaseMinimizer) PropertiesTestGAFactory(org.evosuite.strategy.PropertiesTestGAFactory) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ArrayList(java.util.ArrayList) TestFitnessFactory(org.evosuite.coverage.TestFitnessFactory) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 2 with TestCaseMinimizer

use of org.evosuite.testcase.TestCaseMinimizer in project evosuite by EvoSuite.

the class DifferenceFitnessFunction method getSyntacticSimilarity.

/**
 * Determine the syntactic distance between the string representation of two
 * test cases.
 *
 * @param individual
 * @return
 */
private double getSyntacticSimilarity(TestChromosome individual) {
    // The method toCode converts a test case to its JUnit representation
    String originalString = originalTest.toCode();
    // System.out.println("\n orgString: "+ originalString);
    // The length of test cases varies during the search
    // therefore the comparison needs to be on a test case
    // where we have removed all the irrelevant statements
    TestCaseMinimizer minimizer = new TestCaseMinimizer(originalCoveredGoals);
    TestChromosome copy = (TestChromosome) individual.clone();
    minimizer.minimize(copy);
    TestCodeVisitor visitor = new TestCodeVisitor();
    copy.getTestCase().accept(visitor);
    TestCodeVisitor workaroundVisitor = new TestCodeVisitor();
    // <-- new add, replace = original
    originalTest.accept(workaroundVisitor);
    Set<String> varNames = new HashSet<String>(visitor.getVariableNames());
    varNames.addAll(workaroundVisitor.getVariableNames());
    Set<String> classNames = new HashSet<String>(visitor.getClassNames());
    classNames.addAll(workaroundVisitor.getClassNames());
    // System.out.println(newString);
    String newString = visitor.getCode();
    if (!newString.isEmpty()) {
        if (loopNo == 0) {
            TokenSlicer ts1 = new TokenSlicer(originalString, varNames, classNames);
            originalTokenRecord = ts1.getTokenRecord();
            originalTypeRecord = ts1.getTypeRecord();
            loopNo++;
        }
        TokenSlicer ts2 = new TokenSlicer(newString, varNames, classNames);
        replaceTokenRecord = ts2.getTokenRecord();
        replaceTypeRecord = ts2.getTypeRecord();
        /*
			System.out.println("\noriginal:");
			for(int i=0; i<=originalTokenRecord.size();i++){
				System.out.print(originalTokenRecord.get(i)+ " ");
			}
			System.out.println("\n"+originalTokenRecord +"\n replace: ");
			for(int i=0; i<=replaceTypeRecord.size();i++){
				System.out.print(replaceTypeRecord.get(i)+ " ");
			}*/
        // System.out.println("\nreplaced test token:\n "+replaceTokenRecord);
        // System.out.println("replaced test type: \n"+replaceTypeRecord);
        // System.out.println("replaced test case: "+newString);
        nw = new NWAlgo(originalTokenRecord, replaceTokenRecord, originalTypeRecord, replaceTypeRecord);
        double nwDistance = nw.getDistance();
        return nwDistance;
    }
    return originalString.length() + 10;
}
Also used : TestCaseMinimizer(org.evosuite.testcase.TestCaseMinimizer) TestCodeVisitor(org.evosuite.testcase.TestCodeVisitor) TestChromosome(org.evosuite.testcase.TestChromosome) HashSet(java.util.HashSet)

Aggregations

TestCaseMinimizer (org.evosuite.testcase.TestCaseMinimizer)2 TestChromosome (org.evosuite.testcase.TestChromosome)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 TestFitnessFactory (org.evosuite.coverage.TestFitnessFactory)1 PropertiesTestGAFactory (org.evosuite.strategy.PropertiesTestGAFactory)1 TestCodeVisitor (org.evosuite.testcase.TestCodeVisitor)1 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)1