Search in sources :

Example 6 with Assertion

use of org.evosuite.assertion.Assertion in project evosuite by EvoSuite.

the class TestGenerationResultBuilder method setTestCase.

public void setTestCase(String name, String code, TestCase testCase, String comment, ExecutionResult result) {
    testCode.put(name, code);
    testCases.put(name, testCase);
    Set<Failure> failures = new LinkedHashSet<Failure>();
    for (ContractViolation violation : testCase.getContractViolations()) {
        failures.add(new Failure(violation));
    }
    if (!Properties.CHECK_CONTRACTS && result.hasUndeclaredException()) {
        int position = result.getFirstPositionOfThrownException();
        Throwable exception = result.getExceptionThrownAtPosition(position);
        failures.add(new Failure(exception, position, testCase));
    }
    contractViolations.put(name, failures);
    testComments.put(name, comment);
    testLineCoverage.put(name, result.getTrace().getCoveredLines());
    uncoveredLines.removeAll(result.getTrace().getCoveredLines());
    Set<BranchInfo> branchCoverage = new LinkedHashSet<BranchInfo>();
    for (int branchId : result.getTrace().getCoveredFalseBranches()) {
        Branch branch = BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getBranch(branchId);
        if (branch == null) {
            LoggingUtils.getEvoLogger().warn("Branch is null: " + branchId);
            continue;
        }
        BranchInfo info = new BranchInfo(branch.getClassName(), branch.getMethodName(), branch.getInstruction().getLineNumber(), false);
        branchCoverage.add(info);
    }
    for (int branchId : result.getTrace().getCoveredTrueBranches()) {
        Branch branch = BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getBranch(branchId);
        if (branch == null) {
            LoggingUtils.getEvoLogger().warn("Branch is null: " + branchId);
            continue;
        }
        BranchInfo info = new BranchInfo(branch.getClassName(), branch.getMethodName(), branch.getInstruction().getLineNumber(), true);
        branchCoverage.add(info);
    }
    testBranchCoverage.put(name, branchCoverage);
    uncoveredBranches.removeAll(branchCoverage);
    Set<MutationInfo> mutationCoverage = new LinkedHashSet<MutationInfo>();
    for (Assertion assertion : testCase.getAssertions()) {
        for (Mutation m : assertion.getKilledMutations()) {
            mutationCoverage.add(new MutationInfo(m));
        }
    }
    testMutantCoverage.put(name, mutationCoverage);
    uncoveredMutants.removeAll(mutationCoverage);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Assertion(org.evosuite.assertion.Assertion) ContractViolation(org.evosuite.contracts.ContractViolation) Branch(org.evosuite.coverage.branch.Branch) Mutation(org.evosuite.coverage.mutation.Mutation)

Example 7 with Assertion

use of org.evosuite.assertion.Assertion in project evosuite by EvoSuite.

the class RegressionSuiteMinimizer method numFailingAssertions.

private int numFailingAssertions(RegressionTestChromosome test) {
    int count = 0;
    Set<Assertion> invalidAssertions = new HashSet<>();
    ExecutionResult resultA = test.getLastExecutionResult();
    ExecutionResult resultB = test.getLastRegressionExecutionResult();
    for (Assertion assertion : test.getTheSameTestForTheOtherClassLoader().getTestCase().getAssertions()) {
        for (OutputTrace<?> outputTrace : resultA.getTraces()) {
            if (outputTrace.isDetectedBy(assertion)) {
                logger.error("shouldn't be happening: assertion was failing on original version");
                invalidAssertions.add(assertion);
                break;
            }
        }
        for (OutputTrace<?> outputTrace : resultB.getTraces()) {
            if (outputTrace.isDetectedBy(assertion) && !invalidAssertions.contains(assertion)) {
                count++;
                break;
            }
        }
    }
    if (invalidAssertions.size() != 0) {
        logger.warn("{} invalid assertion(s) to be removed", invalidAssertions);
        for (Assertion assertion : invalidAssertions) {
            test.getTheTest().getTestCase().removeAssertion(assertion);
            test.getTheTest().setChanged(true);
            test.updateClassloader();
        }
    }
    int exDiffCount = 0;
    if (resultA != null && resultB != null) {
        exDiffCount = (int) RegressionExceptionHelper.compareExceptionDiffs(resultA.getCopyOfExceptionMapping(), resultB.getCopyOfExceptionMapping());
        // logger.warn("exDiffCount: {}: \nmap1:{}\nmap2:{}\n",exDiffCount,resultA.getCopyOfExceptionMapping(),
        // resultB.getCopyOfExceptionMapping());
        count += exDiffCount;
        if (exDiffCount > 0 && !test.exCommentsAdded) {
            // logger.warn("Adding Exception Comments for test: \nmap1:{}\nmap2:{}\n",resultA.getCopyOfExceptionMapping(),
            // resultB.getCopyOfExceptionMapping());
            RegressionExceptionHelper.addExceptionAssertionComments(test, resultA.getCopyOfExceptionMapping(), resultB.getCopyOfExceptionMapping());
            test.exCommentsAdded = true;
        }
    } else {
        logger.error("resultA: {} | resultB: {}", resultA, resultB);
    }
    return count;
}
Also used : Assertion(org.evosuite.assertion.Assertion) InspectorAssertion(org.evosuite.assertion.InspectorAssertion) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) HashSet(java.util.HashSet)

Aggregations

Assertion (org.evosuite.assertion.Assertion)7 InspectorAssertion (org.evosuite.assertion.InspectorAssertion)4 ArrayList (java.util.ArrayList)3 Method (java.lang.reflect.Method)2 LinkedHashSet (java.util.LinkedHashSet)2 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)2 MethodStatement (org.evosuite.testcase.statements.MethodStatement)2 TypeToken (com.googlecode.gentyref.TypeToken)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Inspector (org.evosuite.assertion.Inspector)1 PrimitiveFieldAssertion (org.evosuite.assertion.PrimitiveFieldAssertion)1 ContractViolation (org.evosuite.contracts.ContractViolation)1 Branch (org.evosuite.coverage.branch.Branch)1 Mutation (org.evosuite.coverage.mutation.Mutation)1 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)1 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)1 TestChromosome (org.evosuite.testcase.TestChromosome)1 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)1