use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class SimpleMutationAssertionGenerator method addAssertions.
@Override
public void addAssertions(TestSuiteChromosome suite) {
setupClassLoader(suite);
if (!Properties.hasTargetClassBeenLoaded()) {
// Need to load class explicitly since it was re-instrumented
Properties.getTargetClassAndDontInitialise();
if (!Properties.hasTargetClassBeenLoaded()) {
logger.warn("Could not initialize SUT before Assertion generation");
}
}
Set<Integer> tkilled = new HashSet<>();
int numTest = 0;
boolean timeIsShort = false;
for (TestCase test : suite.getTests()) {
if (!TimeController.getInstance().isThereStillTimeInThisPhase()) {
logger.warn("Reached maximum time to generate assertions, aborting assertion generation");
break;
}
// If at 50% of the time we have only done X% of the tests, then don't minimise
if (!timeIsShort && TimeController.getInstance().getPhasePercentage() > Properties.ASSERTION_MINIMIZATION_FALLBACK_TIME) {
if (numTest < Properties.ASSERTION_MINIMIZATION_FALLBACK * suite.size()) {
logger.warn("Assertion minimization is taking too long ({}% of time used, but only {}/{} tests minimized), falling back to using all assertions", TimeController.getInstance().getPhasePercentage(), numTest, suite.size());
timeIsShort = true;
}
}
if (timeIsShort) {
CompleteAssertionGenerator generator = new CompleteAssertionGenerator();
generator.addAssertions(test);
numTest++;
} else {
// Set<Integer> killed = new HashSet<Integer>();
addAssertions(test, tkilled);
// progressMonitor.updateStatus((100 * numTest++) / tests.size());
ClientState state = ClientState.ASSERTION_GENERATION;
ClientStateInformation information = new ClientStateInformation(state);
information.setProgress((100 * numTest++) / suite.size());
ClientServices.getInstance().getClientNode().changeState(state, information);
}
}
calculateMutationScore(tkilled);
restoreCriterion(suite);
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class TestSuiteGenerator method writeJUnitTestsAndCreateResult.
/**
* <p>
* If Properties.JUNIT_TESTS is set, this method writes the given test cases
* to the default directory Properties.TEST_DIR.
*
* <p>
* The name of the test will be equal to the SUT followed by the given
* suffix
*
* @param testSuite
* a test suite.
*/
public static TestGenerationResult writeJUnitTestsAndCreateResult(TestSuiteChromosome testSuite, String suffix) {
List<TestCase> tests = testSuite.getTests();
if (Properties.JUNIT_TESTS) {
ClientServices.getInstance().getClientNode().changeState(ClientState.WRITING_TESTS);
TestSuiteWriter suiteWriter = new TestSuiteWriter();
suiteWriter.insertTests(tests);
String name = Properties.TARGET_CLASS.substring(Properties.TARGET_CLASS.lastIndexOf(".") + 1);
String testDir = Properties.TEST_DIR;
LoggingUtils.getEvoLogger().info("* Writing JUnit test case '" + (name + suffix) + "' to " + testDir);
suiteWriter.writeTestSuite(name + suffix, testDir, testSuite.getLastExecutionResults());
}
return TestGenerationResultBuilder.buildSuccessResult();
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class TestSuiteGenerator method writeJUnitFailingTests.
public void writeJUnitFailingTests() {
if (!Properties.CHECK_CONTRACTS)
return;
FailingTestSet.sendStatistics();
if (Properties.JUNIT_TESTS) {
TestSuiteWriter suiteWriter = new TestSuiteWriter();
// suiteWriter.insertTests(FailingTestSet.getFailingTests());
TestSuiteChromosome suite = new TestSuiteChromosome();
for (TestCase test : FailingTestSet.getFailingTests()) {
test.setFailing();
suite.addTest(test);
}
String name = Properties.TARGET_CLASS.substring(Properties.TARGET_CLASS.lastIndexOf(".") + 1);
String testDir = Properties.TEST_DIR;
LoggingUtils.getEvoLogger().info("* Writing failing test cases '" + (name + Properties.JUNIT_SUFFIX) + "' to " + testDir);
suiteWriter.insertAllTests(suite.getTests());
FailingTestSet.writeJUnitTestSuite(suiteWriter);
suiteWriter.writeTestSuite(name + Properties.JUNIT_FAILED_SUFFIX, testDir, suite.getLastExecutionResults());
}
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class EqualsSymmetricContract method addAssertionAndComments.
@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
TestCase test = statement.getTestCase();
VariableReference a = variables.get(0);
VariableReference b = variables.get(1);
try {
Method equalsMethod = a.getGenericClass().getRawClass().getMethod("equals", new Class<?>[] { Object.class });
GenericMethod method = new GenericMethod(equalsMethod, a.getGenericClass());
// Create x = a.equals(b)
Statement st1 = new MethodStatement(test, method, a, Arrays.asList(new VariableReference[] { b }));
VariableReference x = test.addStatement(st1, statement.getPosition() + 1);
// Create y = b.equals(a);
Statement st2 = new MethodStatement(test, method, b, Arrays.asList(new VariableReference[] { a }));
VariableReference y = test.addStatement(st2, statement.getPosition() + 2);
Statement newStatement = test.getStatement(y.getStPosition());
// Create assertEquals(x, y)
EqualsAssertion assertion = new EqualsAssertion();
assertion.setStatement(newStatement);
assertion.setSource(x);
assertion.setDest(y);
assertion.setValue(true);
newStatement.addAssertion(assertion);
newStatement.addComment("Violates contract a.equals(b) <-> b.equals(a)");
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class AssertionGenerator method filterFailingAssertions.
public void filterFailingAssertions(List<TestCase> testCases) {
List<TestCase> tests = new ArrayList<TestCase>();
tests.addAll(testCases);
for (TestCase test : tests) {
filterFailingAssertions(test);
}
// Execute again in different order
Randomness.shuffle(tests);
for (TestCase test : tests) {
filterFailingAssertions(test);
}
}
Aggregations