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