Search in sources :

Example 46 with CoverageGoalTestNameGenerationStrategy

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));
}
Also used : TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) CoverageGoalTestNameGenerationStrategy(org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy) ArrayList(java.util.ArrayList) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Example 47 with CoverageGoalTestNameGenerationStrategy

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);
}
Also used : TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) CoverageGoalTestNameGenerationStrategy(org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy) ArrayList(java.util.ArrayList) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Example 48 with CoverageGoalTestNameGenerationStrategy

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;
}
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

CoverageGoalTestNameGenerationStrategy (org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy)48 ArrayList (java.util.ArrayList)47 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)47 TestCase (org.evosuite.testcase.TestCase)47 Test (org.junit.Test)47 MethodCoverageTestFitness (org.evosuite.coverage.method.MethodCoverageTestFitness)36 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)27 OutputCoverageGoal (org.evosuite.coverage.io.output.OutputCoverageGoal)18 OutputCoverageTestFitness (org.evosuite.coverage.io.output.OutputCoverageTestFitness)18 InputCoverageGoal (org.evosuite.coverage.io.input.InputCoverageGoal)9 InputCoverageTestFitness (org.evosuite.coverage.io.input.InputCoverageTestFitness)9 MethodNoExceptionCoverageTestFitness (org.evosuite.coverage.method.MethodNoExceptionCoverageTestFitness)9 ExceptionCoverageTestFitness (org.evosuite.coverage.exception.ExceptionCoverageTestFitness)8 ClassWithOverloadedConstructor (com.examples.with.different.packagename.ClassWithOverloadedConstructor)2 File (java.io.File)1 Set (java.util.Set)1 Branch (org.evosuite.coverage.branch.Branch)1 BranchCoverageGoal (org.evosuite.coverage.branch.BranchCoverageGoal)1 BranchCoverageTestFitness (org.evosuite.coverage.branch.BranchCoverageTestFitness)1 LineCoverageTestFitness (org.evosuite.coverage.line.LineCoverageTestFitness)1