use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class RandomLengthTestFactory method getRandomTestCase.
/**
* Create a random individual
*
* @param size
*/
private TestCase getRandomTestCase(int size) {
boolean tracerEnabled = ExecutionTracer.isEnabled();
if (tracerEnabled)
ExecutionTracer.disable();
TestCase test = getNewTestCase();
int num = 0;
// Choose a random length in 0 - size
int length = Randomness.nextInt(size);
while (length == 0) length = Randomness.nextInt(size);
TestFactory testFactory = TestFactory.getInstance();
// Then add random stuff
while (test.size() < length && num < Properties.MAX_ATTEMPTS) {
testFactory.insertRandomStatement(test, test.size() - 1);
num++;
}
if (logger.isDebugEnabled())
logger.debug("Randomized test case:" + test.toCode());
if (tracerEnabled)
ExecutionTracer.enable();
return test;
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class DiversityObserver method getSuiteSimilarity.
/**
* Naive similarity comparison between suites simply consists of merging all tests to a single test
* for each suite, and then comparing these tests
*
* @param suite1
* @param suite2
* @return
*/
public static double getSuiteSimilarity(TestSuiteChromosome suite1, TestSuiteChromosome suite2) {
TestCase test1 = new DefaultTestCase();
for (TestCase test : suite1.getTests()) {
for (Statement s : test) {
// These are not valid tests as the variables still point to the original test
// but that doesn't matter as we're not executing the test
test1.addStatement(s);
}
}
TestCase test2 = new DefaultTestCase();
for (TestCase test : suite2.getTests()) {
for (Statement s : test) {
test2.addStatement(s);
}
}
return getNeedlemanWunschScore(test1, test2);
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class JUnitTestCarvedChromosomeFactory method getChromosome.
@Override
public TestChromosome getChromosome() {
final int N_mutations = Properties.SEED_MUTATIONS;
final double P_clone = Properties.SEED_CLONE;
double r = Randomness.nextDouble();
if (r >= P_clone || junitTests.isEmpty()) {
logger.debug("Using random test");
return defaultFactory.getChromosome();
}
// Cloning
logger.info("Cloning user test");
TestCase test = Randomness.choice(junitTests);
TestChromosome chromosome = new TestChromosome();
chromosome.setTestCase(test.clone());
if (N_mutations > 0) {
int numMutations = Randomness.nextInt(N_mutations);
logger.debug("Mutations: " + numMutations);
// Delta
for (int i = 0; i < numMutations; i++) {
chromosome.mutate();
}
}
return chromosome;
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class DeprecatedTestSuiteDSE method applyDSE0.
/**
* Attempt to negate individual branches until budget is used up, or there
* are no further branches to negate
*
* @param individual
*/
public boolean applyDSE0(TestSuiteChromosome individual) {
logger.info("[DSE] Current test suite: " + individual.toString());
boolean wasSuccess = false;
// expansion already happens as part of LS
// TestSuiteChromosome expandedTests = expandTestSuite(individual);
TestSuiteChromosome expandedTests = individual.clone();
createPathConstraints(expandedTests);
// fitness.getFitness(expandedTests);
double originalFitness = getFitness(individual);
while (hasNextBranchCondition() && !LocalSearchBudget.getInstance().isFinished()) {
logger.info("Branches remaining: " + unsolvedBranchConditions.size());
TestBranchPair next = getNextBranchCondition();
BranchCondition branch = next.branch;
List<BranchCondition> pathCondition = next.pathCondition;
List<Constraint<?>> reachingConstraints = new LinkedList<Constraint<?>>();
for (BranchCondition b : pathCondition) {
reachingConstraints.addAll(b.getSupportingConstraints());
if (b == branch) {
break;
}
reachingConstraints.add(b.getConstraint());
}
List<Constraint<?>> constraints = new LinkedList<Constraint<?>>();
TestCase newTest = negateCondition(new HashSet<Constraint<?>>(reachingConstraints), branch.getConstraint(), next.test.getTestCase());
if (newTest != null) {
logger.info("Found new test: " + newTest.toCode());
TestChromosome newTestChromosome = new TestChromosome();
newTestChromosome.setTestCase(newTest);
expandedTests.addTest(newTestChromosome);
if (Properties.DSE_KEEP_ALL_TESTS) {
updatePathConstraints(newTestChromosome);
calculateUncoveredBranches();
individual.addTest(newTest);
wasSuccess = true;
} else {
if (getFitness(expandedTests) < originalFitness) {
logger.info("New test improves fitness to {}", getFitness(expandedTests));
DSEStats.getInstance().reportNewTestUseful();
wasSuccess = true;
// no need to clone so we can keep executionresult
updatePathConstraints(newTestChromosome);
calculateUncoveredBranches(newTestChromosome);
individual.addTest(newTest);
originalFitness = getFitness(expandedTests);
// TODO: Cancel on fitness 0 - would need to know if
// ZeroFitness is a stopping condition
} else {
logger.info("New test does not improve fitness");
DSEStats.getInstance().reportNewTestUnuseful();
expandedTests.deleteTest(newTest);
}
}
success++;
} else {
unsolvableBranchConditions.add(branch);
failed++;
logger.info("Failed to find new test.");
}
}
logger.info("Finished DSE");
// Ensure fitness values are up to date.
getFitness(individual);
LocalSearchBudget.getInstance().countLocalSearchOnTestSuite();
return wasSuccess;
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class MockJOptionPaneShowInternalOptionDialogTest method testShowInternalInputDialogs.
@Test
public void testShowInternalInputDialogs() throws Exception {
TestSuiteChromosome suite = new TestSuiteChromosome();
InstrumentingClassLoader cl = new InstrumentingClassLoader();
TestCase t1 = buildTestCase0(cl);
suite.addTest(t1);
BranchCoverageSuiteFitness ff = new BranchCoverageSuiteFitness(cl);
ff.getFitness(suite);
Set<TestFitnessFunction> coveredGoals = suite.getCoveredGoals();
Assert.assertEquals(2, coveredGoals.size());
}
Aggregations