Search in sources :

Example 6 with TestCase

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

the class SimpleMutationAssertionGenerator method addAssertions.

@Override
public void addAssertions(TestSuiteChromosome suite) {
    setupClassLoader(suite);
    if (!Properties.hasTargetClassBeenLoaded()) {
        // Need to load class explicitly since it was re-instrumented
        Properties.getTargetClassAndDontInitialise();
        if (!Properties.hasTargetClassBeenLoaded()) {
            logger.warn("Could not initialize SUT before Assertion generation");
        }
    }
    Set<Integer> tkilled = new HashSet<>();
    int numTest = 0;
    boolean timeIsShort = false;
    for (TestCase test : suite.getTests()) {
        if (!TimeController.getInstance().isThereStillTimeInThisPhase()) {
            logger.warn("Reached maximum time to generate assertions, aborting assertion generation");
            break;
        }
        // If at 50% of the time we have only done X% of the tests, then don't minimise
        if (!timeIsShort && TimeController.getInstance().getPhasePercentage() > Properties.ASSERTION_MINIMIZATION_FALLBACK_TIME) {
            if (numTest < Properties.ASSERTION_MINIMIZATION_FALLBACK * suite.size()) {
                logger.warn("Assertion minimization is taking too long ({}% of time used, but only {}/{} tests minimized), falling back to using all assertions", TimeController.getInstance().getPhasePercentage(), numTest, suite.size());
                timeIsShort = true;
            }
        }
        if (timeIsShort) {
            CompleteAssertionGenerator generator = new CompleteAssertionGenerator();
            generator.addAssertions(test);
            numTest++;
        } else {
            // Set<Integer> killed = new HashSet<Integer>();
            addAssertions(test, tkilled);
            // progressMonitor.updateStatus((100 * numTest++) / tests.size());
            ClientState state = ClientState.ASSERTION_GENERATION;
            ClientStateInformation information = new ClientStateInformation(state);
            information.setProgress((100 * numTest++) / suite.size());
            ClientServices.getInstance().getClientNode().changeState(state, information);
        }
    }
    calculateMutationScore(tkilled);
    restoreCriterion(suite);
}
Also used : ClientState(org.evosuite.rmi.service.ClientState) TestCase(org.evosuite.testcase.TestCase) ClientStateInformation(org.evosuite.rmi.service.ClientStateInformation) HashSet(java.util.HashSet)

Example 7 with TestCase

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

the class TestSuiteGenerator method writeJUnitTestsAndCreateResult.

/**
 * <p>
 * If Properties.JUNIT_TESTS is set, this method writes the given test cases
 * to the default directory Properties.TEST_DIR.
 *
 * <p>
 * The name of the test will be equal to the SUT followed by the given
 * suffix
 *
 * @param testSuite
 *            a test suite.
 */
public static TestGenerationResult writeJUnitTestsAndCreateResult(TestSuiteChromosome testSuite, String suffix) {
    List<TestCase> tests = testSuite.getTests();
    if (Properties.JUNIT_TESTS) {
        ClientServices.getInstance().getClientNode().changeState(ClientState.WRITING_TESTS);
        TestSuiteWriter suiteWriter = new TestSuiteWriter();
        suiteWriter.insertTests(tests);
        String name = Properties.TARGET_CLASS.substring(Properties.TARGET_CLASS.lastIndexOf(".") + 1);
        String testDir = Properties.TEST_DIR;
        LoggingUtils.getEvoLogger().info("* Writing JUnit test case '" + (name + suffix) + "' to " + testDir);
        suiteWriter.writeTestSuite(name + suffix, testDir, testSuite.getLastExecutionResults());
    }
    return TestGenerationResultBuilder.buildSuccessResult();
}
Also used : TestSuiteWriter(org.evosuite.junit.writer.TestSuiteWriter) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase)

Example 8 with TestCase

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

the class TestSuiteGenerator method writeJUnitFailingTests.

public void writeJUnitFailingTests() {
    if (!Properties.CHECK_CONTRACTS)
        return;
    FailingTestSet.sendStatistics();
    if (Properties.JUNIT_TESTS) {
        TestSuiteWriter suiteWriter = new TestSuiteWriter();
        // suiteWriter.insertTests(FailingTestSet.getFailingTests());
        TestSuiteChromosome suite = new TestSuiteChromosome();
        for (TestCase test : FailingTestSet.getFailingTests()) {
            test.setFailing();
            suite.addTest(test);
        }
        String name = Properties.TARGET_CLASS.substring(Properties.TARGET_CLASS.lastIndexOf(".") + 1);
        String testDir = Properties.TEST_DIR;
        LoggingUtils.getEvoLogger().info("* Writing failing test cases '" + (name + Properties.JUNIT_SUFFIX) + "' to " + testDir);
        suiteWriter.insertAllTests(suite.getTests());
        FailingTestSet.writeJUnitTestSuite(suiteWriter);
        suiteWriter.writeTestSuite(name + Properties.JUNIT_FAILED_SUFFIX, testDir, suite.getLastExecutionResults());
    }
}
Also used : TestSuiteWriter(org.evosuite.junit.writer.TestSuiteWriter) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase)

Example 9 with TestCase

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

the class EqualsSymmetricContract method addAssertionAndComments.

@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
    TestCase test = statement.getTestCase();
    VariableReference a = variables.get(0);
    VariableReference b = variables.get(1);
    try {
        Method equalsMethod = a.getGenericClass().getRawClass().getMethod("equals", new Class<?>[] { Object.class });
        GenericMethod method = new GenericMethod(equalsMethod, a.getGenericClass());
        // Create x = a.equals(b)
        Statement st1 = new MethodStatement(test, method, a, Arrays.asList(new VariableReference[] { b }));
        VariableReference x = test.addStatement(st1, statement.getPosition() + 1);
        // Create y = b.equals(a);
        Statement st2 = new MethodStatement(test, method, b, Arrays.asList(new VariableReference[] { a }));
        VariableReference y = test.addStatement(st2, statement.getPosition() + 2);
        Statement newStatement = test.getStatement(y.getStPosition());
        // Create assertEquals(x, y)
        EqualsAssertion assertion = new EqualsAssertion();
        assertion.setStatement(newStatement);
        assertion.setSource(x);
        assertion.setDest(y);
        assertion.setValue(true);
        newStatement.addAssertion(assertion);
        newStatement.addComment("Violates contract a.equals(b) <-> b.equals(a)");
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) TestCase(org.evosuite.testcase.TestCase) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) EqualsAssertion(org.evosuite.assertion.EqualsAssertion)

Example 10 with TestCase

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

the class AssertionGenerator method filterFailingAssertions.

public void filterFailingAssertions(List<TestCase> testCases) {
    List<TestCase> tests = new ArrayList<TestCase>();
    tests.addAll(testCases);
    for (TestCase test : tests) {
        filterFailingAssertions(test);
    }
    // Execute again in different order
    Randomness.shuffle(tests);
    for (TestCase test : tests) {
        filterFailingAssertions(test);
    }
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) ArrayList(java.util.ArrayList)

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