Search in sources :

Example 16 with TestCase

use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.

the class BIMutatedMethodSeedingTestSuiteChromosomeFactory method getChromosome.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.evosuite.ga.ChromosomeFactory#getChromosome()
	 */
/**
 * {@inheritDoc}
 */
@Override
public TestSuiteChromosome getChromosome() {
    /*
		 * double P_delta = 0.1d; double P_clone = 0.1d; int MAX_CHANGES = 10;
		 */
    TestSuiteChromosome chromosome = defaultFactory.getChromosome();
    int numTests = chromosome.getTests().size();
    // reduce seed probablility by number of tests to be generated
    final double SEED_CHANCE = Properties.SEED_PROBABILITY / numTests;
    for (int i = 0; i < numTests; i++) {
        if (bestIndividual != null && Randomness.nextDouble() < SEED_CHANCE && Properties.SEED_MUTATIONS > 0) {
            TestSuiteChromosome bi = bestIndividual.clone();
            int mutations = Randomness.nextInt(Properties.SEED_MUTATIONS) + 1;
            for (int j = 0; j < mutations; j++) {
                bi.mutate();
            }
            int testSize = bi.getTests().size();
            TestCase test = bi.getTests().get(Random.nextInt(testSize));
            if (test != null) {
                List<TestCase> tests = chromosome.getTests();
                tests.remove(i);
                tests.add(i, test);
                chromosome.clearTests();
                for (TestCase t : tests) {
                    chromosome.addTest(t);
                }
            }
        }
    }
    return chromosome;
}
Also used : TestCase(org.evosuite.testcase.TestCase) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome)

Example 17 with TestCase

use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.

the class CarvingManager method readTestCases.

private void readTestCases() throws IllegalStateException {
    ClientServices.getInstance().getClientNode().changeState(ClientState.CARVING);
    Collection<String> junitTestNames = getListOfJUnitClassNames();
    LoggingUtils.getEvoLogger().info("* Executing tests from {} test {} for carving", junitTestNames.size(), junitTestNames.size() == 1 ? "class" : "classes");
    final JUnitCore runner = new JUnitCore();
    final CarvingRunListener listener = new CarvingRunListener();
    runner.addListener(listener);
    final List<Class<?>> junitTestClasses = new ArrayList<Class<?>>();
    final org.evosuite.testcarver.extraction.CarvingClassLoader classLoader = new org.evosuite.testcarver.extraction.CarvingClassLoader();
    // TODO: This really needs to be done in a nicer way!
    FieldRegistry.carvingClassLoader = classLoader;
    try {
        // instrument target class
        classLoader.loadClass(Properties.TARGET_CLASS);
    } catch (final ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
    for (String className : junitTestNames) {
        String classNameWithDots = ResourceList.getClassNameFromResourcePath(className);
        try {
            final Class<?> junitClass = classLoader.loadClass(classNameWithDots);
            junitTestClasses.add(junitClass);
        } catch (ClassNotFoundException e) {
            logger.error("Failed to load JUnit test class {}: {}", classNameWithDots, e);
        }
    }
    final Class<?>[] classes = new Class<?>[junitTestClasses.size()];
    junitTestClasses.toArray(classes);
    Result result = runner.run(classes);
    logger.info("Result: {}/{}", result.getFailureCount(), result.getRunCount());
    for (Failure failure : result.getFailures()) {
        logger.info("Failure: {}", failure.getMessage());
        logger.info("Exception: {}", failure.getException());
    }
    Map<Class<?>, List<TestCase>> testMap = listener.getTestCases();
    for (Class<?> targetClass : testMap.keySet()) {
        List<TestCase> processedTests = new ArrayList<>();
        for (TestCase test : testMap.get(targetClass)) {
            String testName = ((CarvedTestCase) test).getName();
            if (test.isEmpty())
                continue;
            ExecutionResult executionResult = null;
            try {
                executionResult = TestCaseExecutor.runTest(test);
            } catch (Throwable t) {
                logger.info("Error while executing carved test {}: {}", testName, t);
                continue;
            }
            if (executionResult.noThrownExceptions()) {
                logger.info("Adding carved test without exception");
                logger.info(test.toCode());
                processedTests.add(test);
            } else {
                logger.info("Exception thrown in carved test {}: {}", testName, executionResult.getExceptionThrownAtPosition(executionResult.getFirstPositionOfThrownException()));
                for (StackTraceElement elem : executionResult.getExceptionThrownAtPosition(executionResult.getFirstPositionOfThrownException()).getStackTrace()) {
                    logger.info(elem.toString());
                }
                logger.info(test.toCode(executionResult.exposeExceptionMapping()));
                if (Properties.CHOP_CARVED_EXCEPTIONS) {
                    logger.info("Chopping exception of carved test");
                    chopException(test, executionResult);
                    if (test.hasObject(Properties.getTargetClassAndDontInitialise(), test.size())) {
                        processedTests.add(test);
                    } else {
                        logger.info("Chopped test is empty");
                    }
                } else {
                    logger.info("Not adding carved test with exception: ");
                }
            }
        }
        if (processedTests.size() > 0) {
            LoggingUtils.getEvoLogger().info(" -> Carved {} tests for class {} from existing JUnit tests", processedTests.size(), targetClass);
            if (logger.isDebugEnabled()) {
                for (TestCase test : processedTests) {
                    logger.debug("Carved Test {}: {}", ((CarvedTestCase) test).getName(), test.toCode());
                }
            }
        } else {
            // String outcome = "";
            // for (Failure failure : result.getFailures()) {
            // outcome += "(" + failure.getDescription() + ", " + failure.getTrace()
            // + ") ";
            // }
            logger.info("It was not possible to carve any test case for class {} from {}", targetClass.getName(), Arrays.toString(junitTestNames.toArray()));
        // + ". Test execution results: " + outcome);
        }
        carvedTests.put(targetClass, processedTests);
    }
    carvingDone = true;
    // TODO: Argh.
    FieldRegistry.carvingClassLoader = null;
// TODO:
// ClientNodeLocal client = ClientServices.getInstance().getClientNode();
// client.trackOutputVariable(RuntimeVariable.CarvedTests, totalNumberOfTestsCarved);
// client.trackOutputVariable(RuntimeVariable.CarvedCoverage,carvedCoverage);
}
Also used : JUnitCore(org.junit.runner.JUnitCore) CarvedTestCase(org.evosuite.testcarver.testcase.CarvedTestCase) ArrayList(java.util.ArrayList) Result(org.junit.runner.Result) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) ArrayList(java.util.ArrayList) List(java.util.List) ResourceList(org.evosuite.classpath.ResourceList) Failure(org.junit.runner.notification.Failure) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) CarvedTestCase(org.evosuite.testcarver.testcase.CarvedTestCase) TestCase(org.evosuite.testcase.TestCase)

Example 18 with TestCase

use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.

the class TestCoverageGoalNameGeneration method testConstructorExceptionWithFullyQualifiedClassName.

@Test
public void testConstructorExceptionWithFullyQualifiedClassName() {
    TestCase test = new DefaultTestCase();
    ExceptionCoverageTestFitness goal = new ExceptionCoverageTestFitness("org.package.name.FooClass", "<init>()", RuntimeException.class, ExceptionCoverageTestFitness.ExceptionType.EXPLICIT);
    test.addCoveredGoal(goal);
    List<TestCase> tests = new ArrayList<>();
    tests.add(test);
    CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests);
    String generatedName = naming.getName(test);
    assertEquals("testFailsToCreateFooClassThrowsRuntimeException", generatedName);
}
Also used : ExceptionCoverageTestFitness(org.evosuite.coverage.exception.ExceptionCoverageTestFitness) MethodNoExceptionCoverageTestFitness(org.evosuite.coverage.method.MethodNoExceptionCoverageTestFitness) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) CoverageGoalTestNameGenerationStrategy(org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy) ArrayList(java.util.ArrayList) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Example 19 with TestCase

use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.

the class TestCoverageGoalNameGeneration method testTwoTestsUniqueMethods.

@Test
public void testTwoTestsUniqueMethods() {
    TestCase test1 = new DefaultTestCase();
    MethodCoverageTestFitness goal1 = new MethodCoverageTestFitness("FooClass", "toString");
    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", "getSomeStuff");
    test2.addCoveredGoal(goal2);
    List<TestCase> tests = new ArrayList<>();
    tests.add(test1);
    tests.add(test2);
    CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests);
    assertEquals("testToString", naming.getName(test1));
    assertEquals("testGetSomeStuff", naming.getName(test2));
}
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 20 with TestCase

use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.

the class TestCoverageGoalNameGeneration method testTwoInputGoals.

@Test
public void testTwoInputGoals() {
    TestCase test = new DefaultTestCase();
    InputCoverageGoal inputGoal1 = new InputCoverageGoal("FooClass", "foo", 0, objectType(), REF_NONNULL);
    InputCoverageTestFitness goal1 = new InputCoverageTestFitness(inputGoal1);
    InputCoverageGoal inputGoal2 = new InputCoverageGoal("FooClass", "foo", 1, objectType(), REF_NULL);
    InputCoverageTestFitness goal2 = new InputCoverageTestFitness(inputGoal2);
    test.addCoveredGoal(goal1);
    test.addCoveredGoal(goal2);
    List<TestCase> tests = new ArrayList<>();
    tests.add(test);
    CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests);
    String generatedName = naming.getName(test);
    assertEquals("testFooWithNonNullAndNull", generatedName);
}
Also used : InputCoverageTestFitness(org.evosuite.coverage.io.input.InputCoverageTestFitness) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) InputCoverageGoal(org.evosuite.coverage.io.input.InputCoverageGoal) CoverageGoalTestNameGenerationStrategy(org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy) ArrayList(java.util.ArrayList) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

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