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