Search in sources :

Example 1 with NumberedTestNameGenerationStrategy

use of org.evosuite.junit.naming.methods.NumberedTestNameGenerationStrategy 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;
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) NumberedTestNameGenerationStrategy(org.evosuite.junit.naming.methods.NumberedTestNameGenerationStrategy) CoverageGoalTestNameGenerationStrategy(org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy) File(java.io.File)

Aggregations

File (java.io.File)1 CoverageGoalTestNameGenerationStrategy (org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy)1 NumberedTestNameGenerationStrategy (org.evosuite.junit.naming.methods.NumberedTestNameGenerationStrategy)1 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)1