use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class CoverageCrossOver method crossOver.
/* (non-Javadoc)
* @see org.evosuite.ga.CrossOverFunction#crossOver(org.evosuite.ga.Chromosome, org.evosuite.ga.Chromosome)
*/
/**
* {@inheritDoc}
*/
@Override
public void crossOver(Chromosome parent1, Chromosome parent2) throws ConstructionFailedException {
assert (parent1 instanceof TestSuiteChromosome);
assert (parent2 instanceof TestSuiteChromosome);
TestSuiteChromosome suite1 = (TestSuiteChromosome) parent1;
TestSuiteChromosome suite2 = (TestSuiteChromosome) parent2;
// Determine coverage information
Map<TestFitnessFunction, Set<TestChromosome>> goalMap = new HashMap<TestFitnessFunction, Set<TestChromosome>>();
populateCoverageMap(goalMap, suite1);
populateCoverageMap(goalMap, suite2);
// Extract set of tests that have unique coverage
// We need all of these tests in both offspring
Set<TestChromosome> unique = removeUniqueCoveringTests(goalMap);
logger.debug("Uniquely covering tests: " + unique.size());
Set<TestChromosome> offspring1 = new HashSet<TestChromosome>();
Set<TestChromosome> offspring2 = new HashSet<TestChromosome>();
Set<TestChromosome> workingSet = new HashSet<TestChromosome>();
for (TestFitnessFunction goal : goalMap.keySet()) {
workingSet.addAll(goalMap.get(goal));
}
int targetSize = workingSet.size() / 2;
while (offspring2.size() < targetSize) {
logger.debug("Sizes: " + workingSet.size() + ", " + offspring1.size() + ", " + offspring2.size());
// Move a randomly selected redundant test case t from workingset to offspring2
TestChromosome choice = Randomness.choice(workingSet);
workingSet.remove(choice);
offspring2.add(choice);
// Move all tests with unique coverage to offspring 1?
offspring1.addAll(removeUniqueCoveringTests(goalMap));
}
offspring1.addAll(workingSet);
suite1.clearTests();
suite2.clearTests();
// Add unique tests
for (TestChromosome test : unique) {
suite1.addTest((TestChromosome) test.clone());
suite2.addTest((TestChromosome) test.clone());
}
// Add redundancy tests
suite1.addTests(offspring1);
suite2.addTests(offspring2);
logger.debug("Final sizes: " + suite1.size() + ", " + suite2.size());
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class RegressionSuiteStrategy method generateRandomRegressionTests.
private TestSuiteChromosome generateRandomRegressionTests() {
LoggingUtils.getEvoLogger().info("* Using RANDOM regression test generation");
if (Properties.KEEP_REGRESSION_ARCHIVE) {
Properties.TEST_ARCHIVE = true;
}
RegressionTestSuiteChromosome suite = new RegressionTestSuiteChromosome();
PropertiesSuiteGAFactory algorithmFactory = new PropertiesSuiteGAFactory();
GeneticAlgorithm<?> suiteGA = algorithmFactory.getSearchAlgorithm();
// statistics.searchStarted(suiteGA);
BranchCoverageSuiteFitness branchCoverageSuiteFitness = new BranchCoverageSuiteFitness(TestGenerationContext.getInstance().getClassLoaderForSUT());
// regressionMonitor.searchStarted(suiteGA);
RegressionTestChromosomeFactory factory = new RegressionTestChromosomeFactory();
LoggingUtils.getEvoLogger().warn("*** generating RANDOM regression tests");
// TODO: Shutdown hook?
List<TestFitnessFunction> goals = getGoals(true);
track(RuntimeVariable.Total_Goals, goals.size());
StoppingCondition stoppingCondition = getStoppingCondition();
// fitnessFunction.getFitness(suite);
int totalTestCount = 0;
int usefulTestCount = 0;
int simulatedAge = 0;
int numAssertions = 0;
int executedStatemets = 0;
boolean firstTry = true;
// Properties.REGRESSION_RANDOM_STRATEGY:
// 0: skip evaluation after first find, dont keep tests
// 1: dont skip evaluation after first find, dont keep tests
// 2: dont skip evaluation after first find, keep tests
// 3: skip evaluation after first find, keep tests [default]
long startTime = System.currentTimeMillis();
while (!stoppingCondition.isFinished() || (numAssertions != 0)) {
if (numAssertions == 0 || Properties.REGRESSION_RANDOM_STRATEGY == 1 || Properties.REGRESSION_RANDOM_STRATEGY == 2) {
RegressionTestChromosome test = factory.getChromosome();
RegressionTestSuiteChromosome clone = new RegressionTestSuiteChromosome();
clone.addTest(test);
List<TestCase> testCases = clone.getTests();
// fitnessFunction.getFitness(clone);
/*
* logger.debug("Old fitness: {}, new fitness: {}",
* suite.getFitness(), clone.getFitness());
*/
executedStatemets += test.size();
numAssertions = RegressionAssertionCounter.getNumAssertions(clone);
if (Properties.KEEP_REGRESSION_ARCHIVE) {
branchCoverageSuiteFitness.getFitness(clone.getTestSuite());
}
if (numAssertions > 0) {
LoggingUtils.getEvoLogger().warn("Generated test with {} assertions.", numAssertions);
}
totalTestCount++;
if (numAssertions > 0) {
numAssertions = 0;
// boolean compilable = JUnitAnalyzer.verifyCompilationAndExecution(testCases);
JUnitAnalyzer.removeTestsThatDoNotCompile(testCases);
JUnitAnalyzer.handleTestsThatAreUnstable(testCases);
if (testCases.size() > 0) {
clone = new RegressionTestSuiteChromosome();
for (TestCase t : testCases) {
RegressionTestChromosome rtc = new RegressionTestChromosome();
if (t.isUnstable()) {
continue;
}
TestChromosome tc = new TestChromosome();
tc.setTestCase(t);
rtc.setTest(tc);
clone.addTest(rtc);
}
// test.set
// clone.addTest(testCases);
numAssertions = RegressionAssertionCounter.getNumAssertions(clone, false, false);
LoggingUtils.getEvoLogger().warn("Keeping {} assertions.", numAssertions);
if (numAssertions > 0) {
usefulTestCount++;
suite.addTest(test);
}
} else {
LoggingUtils.getEvoLogger().warn("ignored assertions. tests were removed.");
}
}
} else {
if (numAssertions > 0) {
break;
}
/*
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
*/
}
// regressionMonitor.iteration(suiteGA);
if (firstTry || (System.currentTimeMillis() - startTime) >= 4000) {
startTime = System.currentTimeMillis();
simulatedAge++;
firstTry = false;
}
}
// regressionMonitor.searchFinished(suiteGA);
LoggingUtils.getEvoLogger().warn("*** Random test generation finished.");
LoggingUtils.getEvoLogger().warn("*=*=*=* Total tests: {} | Tests with assertion: {}", totalTestCount, usefulTestCount);
// statistics.searchFinished(suiteGA);
zero_fitness.setFinished();
LoggingUtils.getEvoLogger().info("* Generated " + suite.size() + " tests with total length " + suite.totalLengthOfTestCases());
goals = getGoals(false);
track(RuntimeVariable.Total_Goals, goals.size());
suiteGA.printBudget();
if (!(Properties.REGRESSION_RANDOM_STRATEGY == 2 || Properties.REGRESSION_RANDOM_STRATEGY == 3)) {
suite = new RegressionTestSuiteChromosome();
}
TestSuiteChromosome bestSuites = new TestSuiteChromosome();
for (TestCase t : suite.getTests()) {
bestSuites.addTest(t);
}
return bestSuites;
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class DSEStrategy method generateSuite.
private TestSuiteChromosome generateSuite() {
final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
final Set<Method> staticMethods = getStaticMethods(targetClass);
TestSuiteChromosome result = new TestSuiteChromosome();
for (Method staticMethod : staticMethods) {
List<TestChromosome> testCases = generateTests(staticMethod);
result.addTests(testCases);
}
return result;
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class StatisticsSender method sendExceptionInfo.
// -------- private methods ------------------------
private static void sendExceptionInfo(TestSuiteChromosome testSuite) {
List<ExecutionResult> results = new ArrayList<>();
for (TestChromosome testChromosome : testSuite.getTestChromosomes()) {
results.add(testChromosome.getLastExecutionResult());
}
/*
* for each method name, check the class of thrown exceptions in those methods
*/
Map<String, Set<Class<?>>> implicitTypesOfExceptions = new HashMap<>();
Map<String, Set<Class<?>>> explicitTypesOfExceptions = new HashMap<>();
Map<String, Set<Class<?>>> declaredTypesOfExceptions = new HashMap<>();
ExceptionCoverageSuiteFitness.calculateExceptionInfo(results, implicitTypesOfExceptions, explicitTypesOfExceptions, declaredTypesOfExceptions, null);
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Explicit_MethodExceptions, ExceptionCoverageSuiteFitness.getNumExceptions(explicitTypesOfExceptions));
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Explicit_TypeExceptions, ExceptionCoverageSuiteFitness.getNumClassExceptions(explicitTypesOfExceptions));
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Implicit_MethodExceptions, ExceptionCoverageSuiteFitness.getNumExceptions(implicitTypesOfExceptions));
ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Implicit_TypeExceptions, ExceptionCoverageSuiteFitness.getNumClassExceptions(implicitTypesOfExceptions));
/*
* NOTE: in old report generator, we were using Properties.SAVE_ALL_DATA
* to check if writing the full explicitTypesOfExceptions and implicitTypesOfExceptions
*/
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class StatisticsSender method executedAndThenSendIndividualToMaster.
/**
* First execute (if needed) the test cases to be sure to have latest correct data,
* and then send it to Master
*/
public static void executedAndThenSendIndividualToMaster(TestSuiteChromosome testSuite) throws IllegalArgumentException {
if (testSuite == null) {
throw new IllegalArgumentException("No defined test suite to send");
}
if (!Properties.NEW_STATISTICS)
return;
for (TestChromosome test : testSuite.getTestChromosomes()) {
if (test.getLastExecutionResult() == null) {
ExecutionResult result = TestCaseExecutor.runTest(test.getTestCase());
test.setLastExecutionResult(result);
}
}
sendCoveredInfo(testSuite);
sendExceptionInfo(testSuite);
sendIndividualToMaster(testSuite);
}
Aggregations