Search in sources :

Example 6 with TestSuiteChromosome

use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.

the class FixedNumRandomTestStrategy method generateTests.

@Override
public TestSuiteChromosome generateTests() {
    LoggingUtils.getEvoLogger().info("* Generating fixed number of random tests");
    RandomLengthTestFactory factory = new org.evosuite.testcase.factories.RandomLengthTestFactory();
    TestSuiteChromosome suite = new TestSuiteChromosome();
    if (!canGenerateTestsForSUT()) {
        LoggingUtils.getEvoLogger().info("* Found no testable methods in the target class " + Properties.TARGET_CLASS);
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, 0);
        return suite;
    }
    for (int i = 0; i < Properties.NUM_RANDOM_TESTS; i++) {
        logger.info("Current test: " + i + "/" + Properties.NUM_RANDOM_TESTS);
        TestChromosome test = factory.getChromosome();
        ExecutionResult result = TestCaseExecutor.runTest(test.getTestCase());
        Integer pos = result.getFirstPositionOfThrownException();
        if (pos != null) {
            if (result.getExceptionThrownAtPosition(pos) instanceof CodeUnderTestException || result.getExceptionThrownAtPosition(pos) instanceof UncompilableCodeException || result.getExceptionThrownAtPosition(pos) instanceof TestCaseExecutor.TimeoutExceeded) {
                // Filter invalid tests
                continue;
            } else {
                // Remove anything that follows an exception
                test.getTestCase().chop(pos + 1);
            }
            test.setChanged(true);
        } else {
            test.setLastExecutionResult(result);
        }
        suite.addTest(test);
    }
    // Search is finished, send statistics
    sendExecutionStatistics();
    return suite;
}
Also used : TestCaseExecutor(org.evosuite.testcase.execution.TestCaseExecutor) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) RandomLengthTestFactory(org.evosuite.testcase.factories.RandomLengthTestFactory) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) TestChromosome(org.evosuite.testcase.TestChromosome) UncompilableCodeException(org.evosuite.testcase.execution.UncompilableCodeException)

Example 7 with TestSuiteChromosome

use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.

the class MOSuiteStrategy method generateTests.

@Override
public TestSuiteChromosome generateTests() {
    // Set up search algorithm
    PropertiesSuiteGAFactory algorithmFactory = new PropertiesSuiteGAFactory();
    GeneticAlgorithm<TestSuiteChromosome> algorithm = algorithmFactory.getSearchAlgorithm();
    // Override chromosome factory
    // TODO handle this better by introducing generics
    ChromosomeFactory factory = new RandomLengthTestFactory();
    algorithm.setChromosomeFactory(factory);
    if (Properties.SERIALIZE_GA || Properties.CLIENT_ON_THREAD)
        TestGenerationResultBuilder.getInstance().setGeneticAlgorithm(algorithm);
    long startTime = System.currentTimeMillis() / 1000;
    // What's the search target
    List<TestFitnessFactory<? extends TestFitnessFunction>> goalFactories = getFitnessFactories();
    List<TestFitnessFunction> fitnessFunctions = new ArrayList<TestFitnessFunction>();
    for (TestFitnessFactory<? extends TestFitnessFunction> goalFactory : goalFactories) {
        fitnessFunctions.addAll(goalFactory.getCoverageGoals());
    }
    algorithm.addFitnessFunctions((List) fitnessFunctions);
    // if (Properties.SHOW_PROGRESS && !logger.isInfoEnabled())
    // FIXME progressMonitor may cause
    algorithm.addListener(progressMonitor);
    if (Properties.ALGORITHM == Properties.Algorithm.LIPS)
        LoggingUtils.getEvoLogger().info("* Total number of test goals for LIPS: {}", fitnessFunctions.size());
    else if (Properties.ALGORITHM == Properties.Algorithm.MOSA)
        LoggingUtils.getEvoLogger().info("* Total number of test goals for MOSA: {}", fitnessFunctions.size());
    if (ArrayUtil.contains(Properties.CRITERION, Criterion.DEFUSE) || ArrayUtil.contains(Properties.CRITERION, Criterion.ALLDEFS) || ArrayUtil.contains(Properties.CRITERION, Criterion.STATEMENT) || ArrayUtil.contains(Properties.CRITERION, Criterion.RHO) || ArrayUtil.contains(Properties.CRITERION, Criterion.BRANCH) || ArrayUtil.contains(Properties.CRITERION, Criterion.AMBIGUITY))
        ExecutionTracer.enableTraceCalls();
    algorithm.resetStoppingConditions();
    TestSuiteChromosome testSuite = null;
    if (!(Properties.STOP_ZERO && fitnessFunctions.isEmpty()) || ArrayUtil.contains(Properties.CRITERION, Criterion.EXCEPTION)) {
        // Perform search
        LoggingUtils.getEvoLogger().info("* Using seed {}", Randomness.getSeed());
        LoggingUtils.getEvoLogger().info("* Starting evolution");
        ClientServices.getInstance().getClientNode().changeState(ClientState.SEARCH);
        algorithm.generateSolution();
        List<TestSuiteChromosome> bestSuites = (List<TestSuiteChromosome>) algorithm.getBestIndividuals();
        if (bestSuites.isEmpty()) {
            LoggingUtils.getEvoLogger().warn("Could not find any suitable chromosome");
            return new TestSuiteChromosome();
        } else {
            testSuite = bestSuites.get(0);
        }
    } else {
        zeroFitness.setFinished();
        testSuite = new TestSuiteChromosome();
        for (FitnessFunction<?> ff : testSuite.getFitnessValues().keySet()) {
            testSuite.setCoverage(ff, 1.0);
        }
    }
    long endTime = System.currentTimeMillis() / 1000;
    // Newline after progress bar
    if (Properties.SHOW_PROGRESS)
        LoggingUtils.getEvoLogger().info("");
    String text = " statements, best individual has fitness: ";
    LoggingUtils.getEvoLogger().info("* Search finished after " + (endTime - startTime) + "s and " + algorithm.getAge() + " generations, " + MaxStatementsStoppingCondition.getNumExecutedStatements() + text + testSuite.getFitness());
    // Search is finished, send statistics
    sendExecutionStatistics();
    // We send the info about the total number of coverage goals/targets only after
    // the end of the search. This is because the number of coverage targets may vary
    // when the criterion Properties.Criterion.EXCEPTION is used (exception coverage
    // goal are dynamically added when the generated tests trigger some exceptions
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, algorithm.getFitnessFunctions().size());
    return testSuite;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ArrayList(java.util.ArrayList) TestFitnessFactory(org.evosuite.coverage.TestFitnessFactory) ChromosomeFactory(org.evosuite.ga.ChromosomeFactory) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) ArrayList(java.util.ArrayList) List(java.util.List) RandomLengthTestFactory(org.evosuite.testcase.factories.RandomLengthTestFactory)

Example 8 with TestSuiteChromosome

use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.

the class TestCaseRecycler method searchFinished.

@Override
public void searchFinished(GeneticAlgorithm<?> algorithm) {
    Chromosome individual = algorithm.getBestIndividual();
    if (individual instanceof TestChromosome) {
        TestChromosome testChromosome = (TestChromosome) individual;
        testPool.add(testChromosome.getTestCase());
    } else if (individual instanceof TestSuiteChromosome) {
        TestSuiteChromosome testSuiteChromosome = (TestSuiteChromosome) individual;
        testPool.addAll(testSuiteChromosome.getTests());
    }
}
Also used : TestChromosome(org.evosuite.testcase.TestChromosome) Chromosome(org.evosuite.ga.Chromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 9 with TestSuiteChromosome

use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.

the class BIMethodSeedingTestSuiteChromosomeFactory method getChromosome.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.evosuite.ga.ChromosomeFactory#getChromosome()
	 */
/**
 * {@inheritDoc}
 */
@Override
public TestSuiteChromosome getChromosome() {
    /*
		 * double P_delta = 0.1d; double P_clone = 0.1d; int MAX_CHANGES = 10;
		 */
    TestSuiteChromosome chromosome = defaultFactory.getChromosome();
    int numTests = chromosome.getTests().size();
    // reduce seed probablility by number of tests to be generated
    final double SEED_CHANCE = Properties.SEED_PROBABILITY / numTests;
    for (int i = 0; i < numTests; i++) {
        if (Randomness.nextDouble() < SEED_CHANCE) {
            int testSize = bestIndividual.getTests().size();
            TestCase test = bestIndividual.getTests().get(Randomness.nextInt(testSize));
            if (test != null) {
                List<TestCase> tests = chromosome.getTests();
                tests.remove(i);
                tests.add(i, test);
                chromosome.clearTests();
                for (TestCase t : tests) {
                    chromosome.addTest(t);
                }
            }
        }
    // chromosome.tests.add(test);
    }
    return chromosome;
}
Also used : TestCase(org.evosuite.testcase.TestCase) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome)

Example 10 with TestSuiteChromosome

use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.

the class RandomMethodSeedingTestSuiteChromosomeFactory method getChromosome.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.evosuite.ga.ChromosomeFactory#getChromosome()
	 */
/**
 * {@inheritDoc}
 */
@Override
public TestSuiteChromosome getChromosome() {
    /*
		 * double P_delta = 0.1d; double P_clone = 0.1d; int MAX_CHANGES = 10;
		 */
    TestSuiteChromosome chromosome = defaultFactory.getChromosome();
    int numTests = chromosome.getTests().size();
    // reduce seed probablility by number of tests to be generated
    final double SEED_CHANCE = Properties.SEED_PROBABILITY / numTests;
    for (int i = 0; i < numTests; i++) {
        if (geneticAlgorithm != null && Randomness.nextDouble() < SEED_CHANCE) {
            int populationSize = geneticAlgorithm.getPopulation().size();
            TestSuiteChromosome tsc = geneticAlgorithm.getPopulation().get(Randomness.nextInt(populationSize));
            int testSize = tsc.getTests().size();
            TestCase test = tsc.getTests().get(Random.nextInt(testSize));
            if (test != null) {
                List<TestCase> tests = chromosome.getTests();
                tests.remove(i);
                tests.add(i, test);
                chromosome.clearTests();
                for (TestCase t : tests) {
                    chromosome.addTest(t);
                }
            }
        }
    }
    return chromosome;
}
Also used : TestCase(org.evosuite.testcase.TestCase) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome)

Aggregations

TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)536 Test (org.junit.Test)430 EvoSuite (org.evosuite.EvoSuite)392 Properties (org.evosuite.Properties)78 OutputVariable (org.evosuite.statistics.OutputVariable)50 GenericSUTString (com.examples.with.different.packagename.generic.GenericSUTString)49 TestChromosome (org.evosuite.testcase.TestChromosome)47 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)44 TestCase (org.evosuite.testcase.TestCase)43 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)30 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)22 Ignore (org.junit.Ignore)19 ArrayList (java.util.ArrayList)17 DefaultLocalSearchObjective (org.evosuite.ga.localsearch.DefaultLocalSearchObjective)17 TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)14 CheapPurityAnalyzer (org.evosuite.assertion.CheapPurityAnalyzer)13 LineCoverageSuiteFitness (org.evosuite.coverage.line.LineCoverageSuiteFitness)13 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)13 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)10 Method (java.lang.reflect.Method)9