Search in sources :

Example 6 with TestFitnessFactory

use of org.evosuite.coverage.TestFitnessFactory in project evosuite by EvoSuite.

the class OutputCoverageFitnessFunctionSystemTest method testOutputCoveragePrimitiveTypes.

public void testOutputCoveragePrimitiveTypes() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = MethodReturnsPrimitive.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();
    int goals = 0;
    for (TestFitnessFactory ff : TestGenerationStrategy.getFitnessFactories()) goals += ff.getCoverageGoals().size();
    Assert.assertEquals("Unexpected number of goals", 24, goals);
    Assert.assertEquals("Non-optimal fitness: ", 0.0, best.getFitness(), 0.001);
    Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) MethodReturnsObject(com.examples.with.different.packagename.coverage.MethodReturnsObject) TestFitnessFactory(org.evosuite.coverage.TestFitnessFactory)

Example 7 with TestFitnessFactory

use of org.evosuite.coverage.TestFitnessFactory in project evosuite by EvoSuite.

the class OutputCoverageFitnessFunctionSystemTest method testOutputCoverageArray.

@Test
public void testOutputCoverageArray() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = MethodReturnsArray.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();
    int goals = 0;
    for (TestFitnessFactory ff : TestGenerationStrategy.getFitnessFactories()) goals += ff.getCoverageGoals().size();
    Assert.assertEquals("Unexpected number of goals", 15, goals);
    Assert.assertEquals("Non-optimal fitness: ", 0.0, best.getFitness(), 0.001);
    Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) MethodReturnsObject(com.examples.with.different.packagename.coverage.MethodReturnsObject) TestFitnessFactory(org.evosuite.coverage.TestFitnessFactory)

Example 8 with TestFitnessFactory

use of org.evosuite.coverage.TestFitnessFactory in project evosuite by EvoSuite.

the class OutputCoverageFitnessFunctionSystemTest method testOutputCoverageObjectType.

public void testOutputCoverageObjectType() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = MethodReturnsObject.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();
    int goals = 0;
    for (TestFitnessFactory ff : TestGenerationStrategy.getFitnessFactories()) goals += ff.getCoverageGoals().size();
    Assert.assertEquals("Unexpected number of goals", 14, goals);
    // sub-optimal due to hashcode observer
    Assert.assertEquals("Unexpected coverage: ", 0.888d, best.getCoverage(), 0.001);
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) MethodReturnsObject(com.examples.with.different.packagename.coverage.MethodReturnsObject) TestFitnessFactory(org.evosuite.coverage.TestFitnessFactory)

Example 9 with TestFitnessFactory

use of org.evosuite.coverage.TestFitnessFactory in project evosuite by EvoSuite.

the class CoverageAnalysis method analyzeCoverage.

/**
 * Identify all JUnit tests starting with the given name prefix, instrument
 * and run tests
 */
public static void analyzeCoverage() {
    Sandbox.goingToExecuteSUTCode();
    TestGenerationContext.getInstance().goingToExecuteSUTCode();
    Sandbox.goingToExecuteUnsafeCodeOnSameThread();
    ExecutionTracer.setCheckCallerThread(false);
    try {
        String cp = ClassPathHandler.getInstance().getTargetProjectClasspath();
        if (Properties.TARGET_CLASS.endsWith(".jar") || Properties.TARGET_CLASS.contains(File.separator)) {
            targetClasses = DependencyAnalysis.analyzeTarget(Properties.TARGET_CLASS, Arrays.asList(cp.split(File.pathSeparator)));
        } else {
            targetClasses.add(Properties.TARGET_CLASS);
            DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, Arrays.asList(cp.split(File.pathSeparator)));
        }
        LoggingUtils.getEvoLogger().info("* Finished analyzing classpath");
    } catch (Throwable e) {
        LoggingUtils.getEvoLogger().error("* Error while initializing target class: " + (e.getMessage() != null ? e.getMessage() : e.toString()));
        logger.error("Problem for " + Properties.TARGET_CLASS + ". Full stack:", e);
        return;
    } finally {
        Sandbox.doneWithExecutingUnsafeCodeOnSameThread();
        Sandbox.doneWithExecutingSUTCode();
        TestGenerationContext.getInstance().doneWithExecutingSUTCode();
    }
    // TestCluster.getInstance();
    List<Class<?>> testClasses = getTestClasses();
    LoggingUtils.getEvoLogger().info("* Found " + testClasses.size() + " test class(es)");
    if (testClasses.isEmpty())
        return;
    /*
         * sort them in a deterministic way, in case there are 
         * static state dependencies
         */
    sortTestClasses(testClasses);
    Class<?>[] tests = testClasses.toArray(new Class<?>[testClasses.size()]);
    LoggingUtils.getEvoLogger().info("* Executing test(s)");
    if (Properties.SELECTED_JUNIT == null) {
        boolean origUseAgent = EvoRunner.useAgent;
        boolean origUseClassLoader = EvoRunner.useClassLoader;
        try {
            // avoid double instrumentation
            EvoRunner.useAgent = false;
            // avoid double instrumentation
            EvoRunner.useClassLoader = false;
            List<JUnitResult> results = executeTests(tests);
            printReport(results);
        } finally {
            EvoRunner.useAgent = origUseAgent;
            EvoRunner.useClassLoader = origUseClassLoader;
        }
    } else {
        // instead of just running junit tests, carve them
        JUnitTestCarvedChromosomeFactory carvedFactory = new JUnitTestCarvedChromosomeFactory(null);
        TestSuiteChromosome testSuite = carvedFactory.getCarvedTestSuite();
        int goals = 0;
        for (Properties.Criterion pc : Properties.CRITERION) {
            LoggingUtils.getEvoLogger().info("* Coverage analysis for criterion " + pc);
            TestFitnessFactory ffactory = FitnessFunctions.getFitnessFactory(pc);
            goals += ffactory.getCoverageGoals().size();
            FitnessFunction ffunction = FitnessFunctions.getFitnessFunction(pc);
            ffunction.getFitness(testSuite);
            CoverageCriteriaAnalyzer.analyzeCoverage(testSuite, pc);
        }
        // Generate test suite
        TestSuiteGenerator.writeJUnitTestsAndCreateResult(testSuite);
        StatisticsSender.executedAndThenSendIndividualToMaster(testSuite);
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, goals);
        if (Properties.COVERAGE_MATRIX)
            throw new IllegalArgumentException("Coverage matrix not yet available when measuring coverage of a carved test suite");
    }
}
Also used : JUnitTestCarvedChromosomeFactory(org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory) TestFitnessFactory(org.evosuite.coverage.TestFitnessFactory) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) FitnessFunction(org.evosuite.ga.FitnessFunction) Properties(org.evosuite.Properties) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestClass(org.junit.runners.model.TestClass) Criterion(org.evosuite.Properties.Criterion)

Example 10 with TestFitnessFactory

use of org.evosuite.coverage.TestFitnessFactory in project evosuite by EvoSuite.

the class WholeTestSuiteStrategy method getGoals.

private List<TestFitnessFunction> getGoals(boolean verbose) {
    List<TestFitnessFactory<? extends TestFitnessFunction>> goalFactories = getFitnessFactories();
    List<TestFitnessFunction> goals = new ArrayList<>();
    if (goalFactories.size() == 1) {
        TestFitnessFactory<? extends TestFitnessFunction> factory = goalFactories.iterator().next();
        goals.addAll(factory.getCoverageGoals());
        if (verbose) {
            LoggingUtils.getEvoLogger().info("* Total number of test goals: {}", factory.getCoverageGoals().size());
            if (Properties.PRINT_GOALS) {
                for (TestFitnessFunction goal : factory.getCoverageGoals()) LoggingUtils.getEvoLogger().info("" + goal.toString());
            }
        }
    } else {
        if (verbose) {
            LoggingUtils.getEvoLogger().info("* Total number of test goals: ");
        }
        for (TestFitnessFactory<? extends TestFitnessFunction> goalFactory : goalFactories) {
            goals.addAll(goalFactory.getCoverageGoals());
            if (verbose) {
                LoggingUtils.getEvoLogger().info("  - " + goalFactory.getClass().getSimpleName().replace("CoverageFactory", "") + " " + goalFactory.getCoverageGoals().size());
                if (Properties.PRINT_GOALS) {
                    for (TestFitnessFunction goal : goalFactory.getCoverageGoals()) LoggingUtils.getEvoLogger().info("" + goal.toString());
                }
            }
        }
    }
    return goals;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ArrayList(java.util.ArrayList) TestFitnessFactory(org.evosuite.coverage.TestFitnessFactory)

Aggregations

TestFitnessFactory (org.evosuite.coverage.TestFitnessFactory)16 ArrayList (java.util.ArrayList)10 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)9 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)9 EvoSuite (org.evosuite.EvoSuite)5 MethodReturnsObject (com.examples.with.different.packagename.coverage.MethodReturnsObject)4 Test (org.junit.Test)4 FlagExample1 (com.examples.with.different.packagename.FlagExample1)3 Method (java.lang.reflect.Method)3 BranchCoverageFactory (org.evosuite.coverage.branch.BranchCoverageFactory)3 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)3 DefUseCoverageFactory (org.evosuite.coverage.dataflow.DefUseCoverageFactory)3 DefUseCoverageSuiteFitness (org.evosuite.coverage.dataflow.DefUseCoverageSuiteFitness)3 TestChromosome (org.evosuite.testcase.TestChromosome)3 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)3 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)3 VariableReference (org.evosuite.testcase.variable.VariableReference)3 GenericClass (org.evosuite.utils.generic.GenericClass)3 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)3 GenericMethod (org.evosuite.utils.generic.GenericMethod)3