Search in sources :

Example 66 with TestCase

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
}
Also used : TestCase(org.evosuite.testcase.TestCase) File(java.io.File) IOException(java.io.IOException)

Example 67 with TestCase

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);
}
Also used : TestCase(org.evosuite.testcase.TestCase)

Example 68 with TestCase

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();
}
Also used : TestCase(org.evosuite.testcase.TestCase) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction)

Example 69 with TestCase

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;
}
Also used : CarvedTestCase(org.evosuite.testcarver.testcase.CarvedTestCase) TestCase(org.evosuite.testcase.TestCase) CarvedTestCase(org.evosuite.testcarver.testcase.CarvedTestCase)

Example 70 with TestCase

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;
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) TestFactory(org.evosuite.testcase.TestFactory) DefaultTestCase(org.evosuite.testcase.DefaultTestCase)

Aggregations

TestCase (org.evosuite.testcase.TestCase)192 Test (org.junit.Test)119 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)90 ArrayList (java.util.ArrayList)67 CoverageGoalTestNameGenerationStrategy (org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy)47 MethodCoverageTestFitness (org.evosuite.coverage.method.MethodCoverageTestFitness)45 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)43 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)39 VariableReference (org.evosuite.testcase.variable.VariableReference)26 OutputCoverageGoal (org.evosuite.coverage.io.output.OutputCoverageGoal)25 OutputCoverageTestFitness (org.evosuite.coverage.io.output.OutputCoverageTestFitness)25 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)24 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)17 TestChromosome (org.evosuite.testcase.TestChromosome)17 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)15 Ignore (org.junit.Ignore)15 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)14 GenericMethod (org.evosuite.utils.generic.GenericMethod)12 ExceptionCoverageTestFitness (org.evosuite.coverage.exception.ExceptionCoverageTestFitness)11 InputCoverageGoal (org.evosuite.coverage.io.input.InputCoverageGoal)10