use of org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy in project evosuite by EvoSuite.
the class TestCoverageGoalNameGeneration method testOverloadedMethod.
@Test
public void testOverloadedMethod() {
TestCase test1 = new DefaultTestCase();
MethodCoverageTestFitness goal1 = new MethodCoverageTestFitness("FooClass", "foo()");
test1.addCoveredGoal(goal1);
TestCase test2 = new DefaultTestCase();
// Need to add statements to change hashCode
test2.addStatement(new IntPrimitiveStatement(test2, 0));
MethodCoverageTestFitness goal2 = new MethodCoverageTestFitness("FooClass", "foo(I)");
test2.addCoveredGoal(goal2);
TestCase test3 = new DefaultTestCase();
// Need to add statements to change hashCode
test3.addStatement(new IntPrimitiveStatement(test3, 0));
MethodCoverageTestFitness goal3 = new MethodCoverageTestFitness("FooClass", "foo(II)");
test3.addCoveredGoal(goal3);
List<TestCase> tests = new ArrayList<>();
tests.add(test1);
tests.add(test2);
tests.add(test3);
CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests);
assertEquals("testFooTakingNoArguments", naming.getName(test1));
assertEquals("testFooTakingInt", naming.getName(test2));
assertEquals("testFooTaking2Arguments", naming.getName(test3));
}
use of org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy in project evosuite by EvoSuite.
the class TestCoverageGoalNameGeneration method testTwoUniqueMethods.
@Test
public void testTwoUniqueMethods() {
TestCase test = new DefaultTestCase();
MethodCoverageTestFitness goal1 = new MethodCoverageTestFitness("FooClass", "foo");
test.addCoveredGoal(goal1);
MethodCoverageTestFitness goal2 = new MethodCoverageTestFitness("FooClass", "bar");
test.addCoveredGoal(goal2);
List<TestCase> tests = new ArrayList<>();
tests.add(test);
CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests);
String generatedName = naming.getName(test);
assertEquals("testBarAndFoo", generatedName);
}
use of org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy in project evosuite by EvoSuite.
the class TestSuiteWriter method writeTestSuite.
/**
* Create JUnit test suite for class
*
* @param name Name of the class
* @param directory Output directory
*/
public List<File> writeTestSuite(String name, String directory, List<ExecutionResult> cachedResults) throws IllegalArgumentException {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("Empty test class name");
}
if (!name.endsWith("Test")) {
/*
* This is VERY important, as otherwise tests can get ignored by "mvn test"
*/
throw new IllegalArgumentException("Test classes should have name ending with 'Test'. Invalid input name: " + name);
}
List<File> generated = new ArrayList<File>();
String dir = TestSuiteWriterUtils.makeDirectory(directory);
String content = "";
// Execute all tests
executor.newObservers();
// be sure it is active here, as JUnit checks might have left it to false
LoopCounter.getInstance().setActive(true);
List<ExecutionResult> results = new ArrayList<>();
for (int i = 0; i < testCases.size(); i++) {
TestCase test = testCases.get(i);
boolean added = false;
if (!TimeController.getInstance().hasTimeToExecuteATestCase()) {
logger.info("Using cached result");
for (ExecutionResult result : cachedResults) {
if (result != null && result.test == test) {
results.add(result);
added = true;
break;
}
}
}
if (!added) {
ExecutionResult result = runTest(test);
results.add(result);
}
}
if (Properties.TEST_NAMING_STRATEGY == Properties.TestNamingStrategy.NUMBERED) {
nameGenerator = new NumberedTestNameGenerationStrategy(testCases, results);
} else if (Properties.TEST_NAMING_STRATEGY == Properties.TestNamingStrategy.COVERAGE) {
nameGenerator = new CoverageGoalTestNameGenerationStrategy(testCases, results);
} else {
throw new RuntimeException("Unsupported naming strategy: " + Properties.TEST_NAMING_STRATEGY);
}
// Avoid downcasts that could break
removeUnnecessaryDownCasts(results);
// Sometimes some timeouts lead to assertions being attached to statements
// related to exceptions. This is not currently handled, so as a workaround
// let's try to remove any remaining assertions. TODO: Better solution
removeAssertionsAfterException(results);
if (Properties.OUTPUT_GRANULARITY == OutputGranularity.MERGED || testCases.size() == 0) {
File file = new File(dir + "/" + name + ".java");
// executor.newObservers();
content = getUnitTestsAllInSameFile(name, results);
FileIOUtils.writeFile(content, file);
generated.add(file);
} else {
for (int i = 0; i < testCases.size(); i++) {
// e.g., dir/Foo_ESTest_0.java
File file = new File(dir + "/" + name + "_" + i + ".java");
// executor.newObservers();
String testCode = getOneUnitTestInAFile(name, i, results);
FileIOUtils.writeFile(testCode, file);
content += testCode;
generated.add(file);
}
}
if (Properties.TEST_SCAFFOLDING && !Properties.NO_RUNTIME_DEPENDENCY) {
String scaffoldingName = Scaffolding.getFileName(name);
File file = new File(dir + "/" + scaffoldingName + ".java");
String scaffoldingContent = Scaffolding.getScaffoldingFileContent(name, results, TestSuiteWriterUtils.hasAnySecurityException(results));
FileIOUtils.writeFile(scaffoldingContent, file);
generated.add(file);
content += scaffoldingContent;
}
writeCoveredGoalsFile();
TestGenerationResultBuilder.getInstance().setTestSuiteCode(content);
return generated;
}
Aggregations