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);
}
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);
}
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);
}
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");
}
}
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;
}
Aggregations