Search in sources :

Example 1 with RandomLengthTestFactory

use of org.evosuite.testcase.factories.RandomLengthTestFactory 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 2 with RandomLengthTestFactory

use of org.evosuite.testcase.factories.RandomLengthTestFactory 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 3 with RandomLengthTestFactory

use of org.evosuite.testcase.factories.RandomLengthTestFactory in project evosuite by EvoSuite.

the class PropertiesNoveltySearchFactory method getChromosomeFactory.

private ChromosomeFactory<TestChromosome> getChromosomeFactory() {
    switch(Properties.TEST_FACTORY) {
        case ALLMETHODS:
            logger.info("Using all methods chromosome factory");
            return new AllMethodsTestChromosomeFactory();
        case RANDOM:
            logger.info("Using random chromosome factory");
            return new RandomLengthTestFactory();
        case ARCHIVE:
            logger.info("Using archive chromosome factory");
            return new ArchiveTestChromosomeFactory();
        case JUNIT:
            logger.info("Using seeding chromosome factory");
            JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(new RandomLengthTestFactory());
            return factory;
        case SERIALIZATION:
            logger.info("Using serialization seeding chromosome factory");
            return new RandomLengthTestFactory();
        default:
            throw new RuntimeException("Unsupported test factory: " + Properties.TEST_FACTORY);
    }
}
Also used : JUnitTestCarvedChromosomeFactory(org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory) RandomLengthTestFactory(org.evosuite.testcase.factories.RandomLengthTestFactory) AllMethodsTestChromosomeFactory(org.evosuite.testcase.factories.AllMethodsTestChromosomeFactory) ArchiveTestChromosomeFactory(org.evosuite.ga.archive.ArchiveTestChromosomeFactory)

Example 4 with RandomLengthTestFactory

use of org.evosuite.testcase.factories.RandomLengthTestFactory in project evosuite by EvoSuite.

the class PropertiesSuiteGAFactory method getChromosomeFactory.

protected ChromosomeFactory<TestSuiteChromosome> getChromosomeFactory() {
    switch(Properties.STRATEGY) {
        case EVOSUITE:
            switch(Properties.TEST_FACTORY) {
                case ALLMETHODS:
                    logger.info("Using all methods chromosome factory");
                    return new TestSuiteChromosomeFactory(new AllMethodsTestChromosomeFactory());
                case RANDOM:
                    logger.info("Using random chromosome factory");
                    return new TestSuiteChromosomeFactory(new RandomLengthTestFactory());
                case ARCHIVE:
                    logger.info("Using archive chromosome factory");
                    return new TestSuiteChromosomeFactory(new ArchiveTestChromosomeFactory());
                case JUNIT:
                    logger.info("Using seeding chromosome factory");
                    JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(new RandomLengthTestFactory());
                    return new TestSuiteChromosomeFactory(factory);
                case SERIALIZATION:
                    logger.info("Using serialization seeding chromosome factory");
                    return new SerializationSuiteChromosomeFactory(new RandomLengthTestFactory());
                default:
                    throw new RuntimeException("Unsupported test factory: " + Properties.TEST_FACTORY);
            }
        case REGRESSION:
            return new RegressionTestSuiteChromosomeFactory();
        case MOSUITE:
            return new TestSuiteChromosomeFactory(new RandomLengthTestFactory());
        default:
            throw new RuntimeException("Unsupported test factory: " + Properties.TEST_FACTORY);
    }
}
Also used : JUnitTestCarvedChromosomeFactory(org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory) TestSuiteChromosomeFactory(org.evosuite.testsuite.factories.TestSuiteChromosomeFactory) RegressionTestSuiteChromosomeFactory(org.evosuite.regression.RegressionTestSuiteChromosomeFactory) RandomLengthTestFactory(org.evosuite.testcase.factories.RandomLengthTestFactory) AllMethodsTestChromosomeFactory(org.evosuite.testcase.factories.AllMethodsTestChromosomeFactory) SerializationSuiteChromosomeFactory(org.evosuite.testsuite.factories.SerializationSuiteChromosomeFactory) RegressionTestSuiteChromosomeFactory(org.evosuite.regression.RegressionTestSuiteChromosomeFactory) ArchiveTestChromosomeFactory(org.evosuite.ga.archive.ArchiveTestChromosomeFactory)

Example 5 with RandomLengthTestFactory

use of org.evosuite.testcase.factories.RandomLengthTestFactory in project evosuite by EvoSuite.

the class JUnitTestSuiteChromosomeFactory 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 = new TestSuiteChromosome(new RandomLengthTestFactory());
    chromosome.clearTests();
    int numTests = Randomness.nextInt(Properties.MIN_INITIAL_TESTS, Properties.MAX_INITIAL_TESTS + 1);
    for (int i = 0; i < numTests; i++) {
        TestChromosome test = defaultFactory.getChromosome();
        chromosome.addTest(test);
    // chromosome.tests.add(test);
    }
    return chromosome;
}
Also used : TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) RandomLengthTestFactory(org.evosuite.testcase.factories.RandomLengthTestFactory) TestChromosome(org.evosuite.testcase.TestChromosome)

Aggregations

RandomLengthTestFactory (org.evosuite.testcase.factories.RandomLengthTestFactory)6 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)4 TestChromosome (org.evosuite.testcase.TestChromosome)3 ArchiveTestChromosomeFactory (org.evosuite.ga.archive.ArchiveTestChromosomeFactory)2 AllMethodsTestChromosomeFactory (org.evosuite.testcase.factories.AllMethodsTestChromosomeFactory)2 JUnitTestCarvedChromosomeFactory (org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TestFitnessFactory (org.evosuite.coverage.TestFitnessFactory)1 ChromosomeFactory (org.evosuite.ga.ChromosomeFactory)1 RegressionTestSuiteChromosomeFactory (org.evosuite.regression.RegressionTestSuiteChromosomeFactory)1 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)1 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)1 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)1 TestCaseExecutor (org.evosuite.testcase.execution.TestCaseExecutor)1 UncompilableCodeException (org.evosuite.testcase.execution.UncompilableCodeException)1 SerializationSuiteChromosomeFactory (org.evosuite.testsuite.factories.SerializationSuiteChromosomeFactory)1 TestSuiteChromosomeFactory (org.evosuite.testsuite.factories.TestSuiteChromosomeFactory)1