Search in sources :

Example 11 with TestListener

use of fr.inria.stamp.test.listener.TestListener in project dspot by STAMP-project.

the class DefaultTestRunnerTest method testRunTestMethod.

@Test
public void testRunTestMethod() throws Exception {
    TestRunner runner = new DefaultTestRunner(new String[] { "src/test/resources/example-0.0.1-SNAPSHOT.jar", "src/test/resources/example-0.0.1-SNAPSHOT-tests.jar" });
    TestListener results = runner.run("example.TestSuiteExample", "test3");
    assertEquals(1, results.getRunningTests().size());
    assertEquals(0, results.getFailingTests().size());
    assertEquals(1, results.getPassingTests().size());
    assertEquals(0, results.getAssumptionFailingTests().size());
    assertEquals(0, results.getIgnoredTests().size());
}
Also used : TestListener(fr.inria.stamp.test.listener.TestListener) Test(org.junit.Test)

Example 12 with TestListener

use of fr.inria.stamp.test.listener.TestListener in project dspot by STAMP-project.

the class MockitoTestRunnerTest method testRunTestMethod.

@Test
public void testRunTestMethod() throws Exception {
    TestRunner runner = new DefaultTestRunner(new String[] { "src/test/resources/MockitoDemo-1.0-SNAPSHOT.jar", "src/test/resources/MockitoDemo-1.0-SNAPSHOT-tests.jar" });
    TestListener results = runner.run("info.sanaulla.dal.BookDALTest", "testGetAllBooks");
    assertEquals(1, results.getRunningTests().size());
    assertEquals(0, results.getFailingTests().size());
    assertEquals(1, results.getPassingTests().size());
    assertEquals(0, results.getAssumptionFailingTests().size());
    assertEquals(0, results.getIgnoredTests().size());
}
Also used : TestListener(fr.inria.stamp.test.listener.TestListener) Test(org.junit.Test)

Example 13 with TestListener

use of fr.inria.stamp.test.listener.TestListener in project dspot by STAMP-project.

the class MockitoTestRunnerTest method testRunTestMethods.

@Test
public void testRunTestMethods() throws Exception {
    TestRunner runner = new DefaultTestRunner(new String[] { "src/test/resources/MockitoDemo-1.0-SNAPSHOT.jar", "src/test/resources/MockitoDemo-1.0-SNAPSHOT-tests.jar" });
    TestListener results = runner.run("info.sanaulla.dal.BookDALTest", Arrays.asList(new String[] { "testGetAllBooks", "testGetAllBooksFailing" }));
    assertEquals(2, results.getRunningTests().size());
    assertEquals(1, results.getFailingTests().size());
    assertEquals(1, results.getPassingTests().size());
    assertEquals(0, results.getAssumptionFailingTests().size());
    assertEquals(0, results.getIgnoredTests().size());
}
Also used : TestListener(fr.inria.stamp.test.listener.TestListener) Test(org.junit.Test)

Example 14 with TestListener

use of fr.inria.stamp.test.listener.TestListener in project dspot by STAMP-project.

the class MethodsAssertGenerator method generateAsserts.

/**
 * Generates assertions and try/catch/fail blocks for multiple tests.
 *
 * <p>Assertion Amplification process.
 * <ol>
 *   <li>Instrumentation to collect the state of the program after execution (but before assertions).</li>
 *   <li>Collection of actual values by running the tests.</li>
 *   <li>Generation of new assertions in place of observation points.
 *       Generation of catch blocks if a test raises an exception.</li>
 * </ol>
 * The details of the first two points are in {@link #addAssertions(CtType, List)}.
 *
 * @param testClass Test class
 * @param tests Test methods
 * @return New tests with new assertions
 * @throws IOException
 * @throws ClassNotFoundException
 */
public List<CtMethod<?>> generateAsserts(CtType testClass, List<CtMethod<?>> tests) throws IOException, ClassNotFoundException {
    LOGGER.info("Run tests. ({})", tests.size());
    final TestListener result = TestCompiler.compileAndRun(testClass, this.compiler, tests, this.configuration);
    if (result == null) {
        return Collections.emptyList();
    } else {
        final List<String> failuresMethodName = result.getFailingTests().stream().map(Failure::getDescription).map(Description::getMethodName).collect(Collectors.toList());
        final List<String> passingMethodName = result.getPassingTests().stream().map(Description::getMethodName).collect(Collectors.toList());
        final List<CtMethod<?>> generatedTestWithAssertion = new ArrayList<>();
        // add assertion on passing tests
        if (!passingMethodName.isEmpty()) {
            LOGGER.info("{} test pass, generating assertion...", passingMethodName.size());
            List<CtMethod<?>> passingTests = addAssertions(testClass, tests.stream().filter(ctMethod -> passingMethodName.contains(ctMethod.getSimpleName())).collect(Collectors.toList())).stream().filter(Objects::nonNull).collect(Collectors.toList());
            if (passingTests != null) {
                generatedTestWithAssertion.addAll(passingTests);
            }
        }
        // add try/catch/fail on failing/error tests
        if (!failuresMethodName.isEmpty()) {
            LOGGER.info("{} test fail, generating try/catch/fail blocks...", failuresMethodName.size());
            final List<CtMethod<?>> failingTests = tests.stream().filter(ctMethod -> failuresMethodName.contains(ctMethod.getSimpleName())).map(ctMethod -> makeFailureTest(ctMethod, result.getFailureOf(ctMethod.getSimpleName()))).filter(Objects::nonNull).collect(Collectors.toList());
            if (!failingTests.isEmpty()) {
                generatedTestWithAssertion.addAll(failingTests);
            }
        }
        return generatedTestWithAssertion;
    }
}
Also used : IntStream(java.util.stream.IntStream) TestCompiler(fr.inria.diversify.utils.compilation.TestCompiler) java.util(java.util) DSpotUtils(fr.inria.diversify.utils.DSpotUtils) DSpotCompiler(fr.inria.diversify.utils.compilation.DSpotCompiler) LoggerFactory(org.slf4j.LoggerFactory) CtType(spoon.reflect.declaration.CtType) Counter(fr.inria.diversify.utils.Counter) Observation(fr.inria.diversify.compare.Observation) TestListener(fr.inria.stamp.test.listener.TestListener) Query(spoon.reflect.visitor.Query) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) Logger(org.slf4j.Logger) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) TestTimedOutException(org.junit.runners.model.TestTimedOutException) Description(org.junit.runner.Description) IOException(java.io.IOException) Factory(spoon.reflect.factory.Factory) Collectors(java.util.stream.Collectors) Failure(org.junit.runner.notification.Failure) spoon.reflect.code(spoon.reflect.code) CtTypeReference(spoon.reflect.reference.CtTypeReference) ObjectLog(fr.inria.diversify.compare.ObjectLog) CtMethod(spoon.reflect.declaration.CtMethod) TestListener(fr.inria.stamp.test.listener.TestListener) Failure(org.junit.runner.notification.Failure) CtMethod(spoon.reflect.declaration.CtMethod)

Example 15 with TestListener

use of fr.inria.stamp.test.listener.TestListener in project dspot by STAMP-project.

the class MethodsAssertGenerator method addAssertions.

/**
 * Adds new assertions in multiple tests.
 *
 * <p>Instruments the tests to have observation points.
 * Details in {@link AssertGeneratorHelper#createTestWithLog(CtMethod, String)}.
 *
 * <p>Details of the assertion generation in {@link #buildTestWithAssert(CtMethod, Map)}.
 *
 * @param testClass Test class
 * @param testCases Passing test methods
 * @return New tests with new assertions generated from observation points values
 * @throws IOException
 * @throws ClassNotFoundException
 */
private List<CtMethod<?>> addAssertions(CtType<?> testClass, List<CtMethod<?>> testCases) throws IOException, ClassNotFoundException {
    CtType clone = testClass.clone();
    testClass.getPackage().addType(clone);
    LOGGER.info("Add observations points in passing tests.");
    LOGGER.info("Instrumentation...");
    final List<CtMethod<?>> testCasesWithLogs = testCases.stream().map(ctMethod -> {
        DSpotUtils.printProgress(testCases.indexOf(ctMethod), testCases.size());
        return AssertGeneratorHelper.createTestWithLog(ctMethod, this.originalClass.getPackage().getQualifiedName());
    }).collect(Collectors.toList());
    final List<CtMethod<?>> testsToRun = new ArrayList<>();
    IntStream.range(0, 3).forEach(i -> testsToRun.addAll(testCasesWithLogs.stream().map(CtMethod::clone).map(ctMethod -> {
        ctMethod.setSimpleName(ctMethod.getSimpleName() + i);
        return ctMethod;
    }).map(ctMethod -> {
        clone.addMethod(ctMethod);
        return ctMethod;
    }).collect(Collectors.toList())));
    ObjectLog.reset();
    LOGGER.info("Run instrumented tests. ({})", testsToRun.size());
    final TestListener result = TestCompiler.compileAndRun(clone, this.compiler, testsToRun, this.configuration);
    if (result == null || !result.getFailingTests().isEmpty()) {
        return Collections.emptyList();
    } else {
        Map<String, Observation> observations = ObjectLog.getObservations();
        LOGGER.info("Generating assertions...");
        return testCases.stream().map(ctMethod -> this.buildTestWithAssert(ctMethod, observations)).collect(Collectors.toList());
    }
}
Also used : IntStream(java.util.stream.IntStream) TestCompiler(fr.inria.diversify.utils.compilation.TestCompiler) java.util(java.util) DSpotUtils(fr.inria.diversify.utils.DSpotUtils) DSpotCompiler(fr.inria.diversify.utils.compilation.DSpotCompiler) LoggerFactory(org.slf4j.LoggerFactory) CtType(spoon.reflect.declaration.CtType) Counter(fr.inria.diversify.utils.Counter) Observation(fr.inria.diversify.compare.Observation) TestListener(fr.inria.stamp.test.listener.TestListener) Query(spoon.reflect.visitor.Query) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) Logger(org.slf4j.Logger) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) TestTimedOutException(org.junit.runners.model.TestTimedOutException) Description(org.junit.runner.Description) IOException(java.io.IOException) Factory(spoon.reflect.factory.Factory) Collectors(java.util.stream.Collectors) Failure(org.junit.runner.notification.Failure) spoon.reflect.code(spoon.reflect.code) CtTypeReference(spoon.reflect.reference.CtTypeReference) ObjectLog(fr.inria.diversify.compare.ObjectLog) CtMethod(spoon.reflect.declaration.CtMethod) CtType(spoon.reflect.declaration.CtType) Observation(fr.inria.diversify.compare.Observation) TestListener(fr.inria.stamp.test.listener.TestListener) CtMethod(spoon.reflect.declaration.CtMethod)

Aggregations

TestListener (fr.inria.stamp.test.listener.TestListener)30 Test (org.junit.Test)20 IOException (java.io.IOException)8 InputConfiguration (fr.inria.diversify.utils.sosiefier.InputConfiguration)7 File (java.io.File)7 Failure (org.junit.runner.notification.Failure)6 CtMethod (spoon.reflect.declaration.CtMethod)6 CtType (spoon.reflect.declaration.CtType)6 AmplificationHelper (fr.inria.diversify.utils.AmplificationHelper)5 DSpotUtils (fr.inria.diversify.utils.DSpotUtils)5 DSpotCompiler (fr.inria.diversify.utils.compilation.DSpotCompiler)5 TestCompiler (fr.inria.diversify.utils.compilation.TestCompiler)5 Collectors (java.util.stream.Collectors)5 Description (org.junit.runner.Description)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 CtClass (spoon.reflect.declaration.CtClass)5 Amplifier (fr.inria.diversify.dspot.amplifier.Amplifier)3 AssertGenerator (fr.inria.diversify.dspot.assertGenerator.AssertGenerator)3 TestSelector (fr.inria.diversify.dspot.selector.TestSelector)3