use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class TestSuiteSerialization method loadTests.
public static List<TestChromosome> loadTests(File target) throws IllegalArgumentException {
Inputs.checkNull(target);
List<TestChromosome> list = new ArrayList<>();
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(target))) {
try {
Object obj = in.readObject();
while (obj != null) {
if (obj instanceof TestChromosome) {
// this check might fail if old version is used, and EvoSuite got updated
TestChromosome tc = (TestChromosome) obj;
for (Statement st : tc.getTestCase()) {
st.changeClassLoader(TestGenerationContext.getInstance().getClassLoaderForSUT());
}
list.add(tc);
}
obj = in.readObject();
}
} catch (EOFException e) {
// fine
} catch (Exception e) {
logger.warn("Problems when reading a serialized test from " + target.getAbsolutePath() + " : " + e.getMessage());
}
in.close();
} catch (FileNotFoundException e) {
logger.warn("Cannot load tests because file does not exist: " + target.getAbsolutePath());
} catch (IOException e) {
logger.error("Failed to open/handle " + target.getAbsolutePath() + " for reading: " + e.getMessage());
}
return list;
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class TestSuiteSerialization method saveTests.
public static boolean saveTests(List<TestSuiteChromosome> list, File target) throws IllegalArgumentException {
Inputs.checkNull(list, target);
File parent = target.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
try (ObjectOutputStream out = new DebuggingObjectOutputStream(new FileOutputStream(target))) {
for (TestSuiteChromosome ts : list) {
for (TestChromosome tc : ts.getTestChromosomes()) {
out.writeObject(tc);
}
}
out.flush();
out.close();
} catch (IOException e) {
logger.error("Failed to open/handle " + target.getAbsolutePath() + " for writing: " + e.getMessage());
return false;
}
return true;
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class TestSuiteLocalSearch method ensureDoubleExecution.
/**
* Ensure that all branches are executed twice For each branch such that
* exists only one test case in the suite that covers that branch, it
* creates a duplicate of that test case.
*
* By doing this, we avoid to incorrectly mark a new test case produced by
* the local search as an improving test case because it simply executes
* again a predicate.
*/
protected static void ensureDoubleExecution(TestSuiteChromosome individual, LocalSearchObjective<TestSuiteChromosome> objective) {
logger.debug("Ensuring double execution");
Set<TestChromosome> duplicates = new HashSet<TestChromosome>();
TestSuiteFitnessFunction defaultFitness = (TestSuiteFitnessFunction) objective.getFitnessFunctions().get(0);
Map<Integer, Integer> covered = new HashMap<Integer, Integer>();
Map<Integer, TestChromosome> testMap = new HashMap<Integer, TestChromosome>();
for (TestChromosome test : individual.getTestChromosomes()) {
// Make sure we have an execution result
if (test.getLastExecutionResult() == null || test.isChanged()) {
ExecutionResult result = test.executeForFitnessFunction(defaultFitness);
// .clone();
test.setLastExecutionResult(result);
test.setChanged(false);
}
for (Entry<Integer, Integer> entry : test.getLastExecutionResult().getTrace().getPredicateExecutionCount().entrySet()) {
if (!covered.containsKey(entry.getKey())) {
covered.put(entry.getKey(), 0);
}
covered.put(entry.getKey(), covered.get(entry.getKey()) + entry.getValue());
testMap.put(entry.getKey(), test);
}
}
for (Entry<Integer, Integer> entry : covered.entrySet()) {
int branchId = entry.getKey();
int count = entry.getValue();
if (count == 1) {
TestChromosome duplicate = (TestChromosome) testMap.get(branchId).clone();
ExecutionResult result = duplicate.executeForFitnessFunction(defaultFitness);
// .clone();
duplicate.setLastExecutionResult(result);
duplicate.setChanged(false);
duplicates.add(duplicate);
}
}
if (!duplicates.isEmpty()) {
logger.info("Adding " + duplicates.size() + " tests to cover branches sufficiently");
for (TestChromosome test : duplicates) {
individual.addTest(test);
}
individual.setChanged(true);
for (FitnessFunction<? extends Chromosome> ff : objective.getFitnessFunctions()) {
((TestSuiteFitnessFunction) ff).getFitness(individual);
}
}
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class TestSuiteLocalSearch method expandTestSuite.
/**
* Before applying DSE we expand test cases, such that each primitive value
* is used at only exactly one position as a parameter
*
* For example, given the following test case:
*
* <code>
* foo0.bar(1);
* foo1.bar(1);
* </code>
*
* is rewritten as:
*
* <code>
* int int0 = 1;
* int int1 = 1;
* foo0.bar(int0);
* foo1.bar(int1);
* </code>
*
* @param suite
* @return
*/
private static void expandTestSuite(TestSuiteChromosome suite, LocalSearchObjective<TestSuiteChromosome> objective) {
logger.debug("Expanding tests for local search");
TestSuiteChromosome newTestSuite = new TestSuiteChromosome();
for (TestChromosome test : suite.getTestChromosomes()) {
// First make sure we are up to date with the execution
if (test.getLastExecutionResult() == null || test.isChanged()) {
test.setLastExecutionResult(TestCaseExecutor.runTest(test.getTestCase()));
test.setChanged(false);
}
// We skip tests that have problems
if (test.getLastExecutionResult().hasTimeout() || test.getLastExecutionResult().hasTestException()) {
logger.info("Skipping test with timeout or exception");
continue;
}
// If local search has already been applied on the original test
// then we also set that flag on the expanded test
boolean hasLocalSearchBeenApplied = test.hasLocalSearchBeenApplied();
TestCase newTest = test.getTestCase().clone();
TestCase expandedTest = expandTestCase(newTest);
TestChromosome expandedTestChromosome = newTestSuite.addTest(expandedTest);
expandedTestChromosome.setLocalSearchApplied(hasLocalSearchBeenApplied);
}
List<TestChromosome> oldTests = suite.getTestChromosomes();
oldTests.clear();
oldTests.addAll(newTestSuite.getTestChromosomes());
suite.setChanged(true);
for (FitnessFunction<? extends Chromosome> ff : objective.getFitnessFunctions()) {
((TestSuiteFitnessFunction) ff).getFitness(suite);
}
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class SerializationSuiteChromosomeFactory method getChromosome.
/**
*/
@Override
public TestSuiteChromosome getChromosome() {
TestSuiteChromosome tsc = new TestSuiteChromosome(this.defaultFactory);
tsc.clearTests();
if (Randomness.nextDouble() <= Properties.SEED_CLONE && this.previousSuite.size() > 0) {
logger.debug("seeding previous test suite");
for (TestChromosome tc : this.previousSuite) {
TestChromosome clone = (TestChromosome) tc.clone();
// no assertions are used during search
clone.getTestCase().removeAssertions();
tsc.addTest(clone);
}
} else {
logger.debug("creating a random testsuite");
int numTests = Randomness.nextInt(Properties.MIN_INITIAL_TESTS, Properties.MAX_INITIAL_TESTS + 1);
for (int i = 0; i < numTests; i++) {
TestChromosome tc = (TestChromosome) this.defaultFactory.getChromosome();
tsc.addTest(tc);
}
}
assert (!tsc.getTestChromosomes().isEmpty());
return tsc;
}
Aggregations