Search in sources :

Example 1 with PropertiesTestGAFactory

use of org.evosuite.strategy.PropertiesTestGAFactory 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)

Aggregations

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