use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class JUnitAnalyzer method removeTestsThatDoNotCompile.
/**
* Try to compile each test separately, and remove the ones that cannot be
* compiled
*
* @param tests
*/
public static void removeTestsThatDoNotCompile(List<TestCase> tests) {
logger.info("Going to execute: removeTestsThatDoNotCompile");
if (tests == null || tests.isEmpty()) {
// nothing to do
return;
}
Iterator<TestCase> iter = tests.iterator();
while (iter.hasNext()) {
if (!TimeController.getInstance().hasTimeToExecuteATestCase()) {
break;
}
TestCase test = iter.next();
File dir = createNewTmpDir();
if (dir == null) {
logger.warn("Failed to create tmp dir");
return;
}
logger.debug("Created tmp folder: " + dir.getAbsolutePath());
try {
List<TestCase> singleList = new ArrayList<TestCase>();
singleList.add(test);
List<File> generated = compileTests(singleList, dir);
if (generated == null) {
iter.remove();
String code = test.toCode();
logger.error("Failed to compile test case:\n" + code);
}
} finally {
// let's be sure we clean up all what we wrote on disk
if (dir != null) {
try {
FileUtils.deleteDirectory(dir);
logger.debug("Deleted tmp folder: " + dir.getAbsolutePath());
} catch (Exception e) {
logger.error("Cannot delete tmp dir: " + dir.getAbsolutePath(), e);
}
}
}
}
// end of while
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class CoverageGoalTestNameGenerationStrategy method resolveAmbiguity.
private void resolveAmbiguity(Set<TestCase> tests) {
// Full list of goals for given tests
Map<TestCase, Set<TestFitnessFunction>> testToGoals = new LinkedHashMap<>();
for (TestCase test : tests) {
testToGoals.put(test, filterSupportedGoals(new LinkedHashSet<>(test.getCoveredGoals())));
}
// Find out what is unique about each one
findUniqueGoals(testToGoals);
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class CoverageGoalTestNameGenerationStrategy method generateNames.
/**
* Helper method that does the bulk of the work
* @param testToGoals
*/
private void generateNames(Map<TestCase, Set<TestFitnessFunction>> testToGoals) {
initializeMethodCoverageCount(testToGoals);
findUniqueGoals(testToGoals);
initializeNameGoals(testToGoals);
// Iteratively add goals as long as there are resolvable ambiguities
boolean changed = true;
while (changed) {
setTestNames(testToGoals);
Map<String, Set<TestCase>> dupTestNameMap = determineDuplicateNames();
// For each duplicate set, add new test goals
changed = false;
for (Map.Entry<String, Set<TestCase>> entry : dupTestNameMap.entrySet()) {
if (entry.getKey().length() >= MAX_CHARS) {
continue;
}
// Try adding something unique for the given test set
if (resolveAmbiguity(testToGoals, entry.getValue(), true))
changed = true;
else {
// the number of tests in this ambiguity group
if (resolveAmbiguity(testToGoals, entry.getValue(), false))
changed = true;
}
}
}
// If there is absolutely nothing unique, add the top goals so that the test at least has a name
for (Map.Entry<TestCase, Set<TestFitnessFunction>> entry : testToGoals.entrySet()) {
if (entry.getValue().isEmpty()) {
List<TestFitnessFunction> goals = new ArrayList<>(getUniqueNonMethodGoals(entry.getKey(), testToGoals));
if (goals.isEmpty()) {
goals.addAll(filterSupportedGoals(entry.getKey().getCoveredGoals()));
}
Collections.sort(goals, new GoalComparator());
if (!goals.isEmpty()) {
TestFitnessFunction goal = goals.iterator().next();
entry.getValue().add(goal);
String name = getTestName(entry.getKey(), entry.getValue());
testToName.put(entry.getKey(), name);
}
}
}
// Add numbers to remaining duplicate names
fixAmbiguousTestNames();
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class TestSuiteWriterUtils method getNameOfTest.
public static String getNameOfTest(List<TestCase> tests, int position) {
TestCase test = tests.get(position);
String testName = null;
if (test instanceof CarvedTestCase) {
testName = ((CarvedTestCase) test).getName();
} else {
int totalNumberOfTests = tests.size();
String totalNumberOfTestsString = String.valueOf(totalNumberOfTests - 1);
String testNumber = StringUtils.leftPad(String.valueOf(position), totalNumberOfTestsString.length(), "0");
testName = "test" + testNumber;
}
return testName;
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class FixedLengthTestChromosomeFactory method getRandomTestCase.
/**
* Create a random individual
*
* @param size
*/
private TestCase getRandomTestCase(int size) {
TestCase test = new DefaultTestCase();
int num = 0;
TestFactory testFactory = TestFactory.getInstance();
// Then add random stuff
while (test.size() < size && num < Properties.MAX_ATTEMPTS) {
testFactory.insertRandomStatement(test, test.size() - 1);
num++;
}
return test;
}
Aggregations