Search in sources :

Example 1 with KanonizoTestResult

use of org.kanonizo.junit.KanonizoTestResult in project kanonizo by kanonizo.

the class JUnit4TestRunner method runTest.

@Override
public KanonizoTestResult runTest(TestCase tc) {
    Request request = getRequest(tc);
    Runner testRunner = request.getRunner();
    Result testResult = runner.run(testRunner);
    List<KanonizoTestFailure> failures = testResult.getFailures().stream().map(failure -> new KanonizoTestFailure(failure.getException(), failure.getTrace())).collect(Collectors.toList());
    return new KanonizoTestResult(tc.getTestClass(), tc.getMethod(), testResult.wasSuccessful(), failures, testResult.getRunTime());
}
Also used : Arrays(java.util.Arrays) JUnitCore(org.junit.runner.JUnitCore) Result(org.junit.runner.Result) Request(org.junit.runner.Request) RunWith(org.junit.runner.RunWith) ArrayList(java.util.ArrayList) TestCase(org.kanonizo.framework.objects.TestCase) BlockJUnit4ClassRunner(org.junit.runners.BlockJUnit4ClassRunner) Runner(org.junit.runner.Runner) ClassRequest(org.junit.internal.requests.ClassRequest) KanonizoTestFailure(org.kanonizo.junit.KanonizoTestFailure) Method(java.lang.reflect.Method) Parameterized(org.junit.runners.Parameterized) KanonizoTestResult(org.kanonizo.junit.KanonizoTestResult) ParameterisedTestCase(org.kanonizo.framework.objects.ParameterisedTestCase) AssumptionViolatedException(org.junit.AssumptionViolatedException) BlockJUnit4ClassRunnerWithParameters(org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters) FrameworkMethod(org.junit.runners.model.FrameworkMethod) Iterator(java.util.Iterator) EachTestNotifier(org.junit.internal.runners.model.EachTestNotifier) Filter(org.junit.runner.manipulation.Filter) Description(org.junit.runner.Description) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Ignore(org.junit.Ignore) RunNotifier(org.junit.runner.notification.RunNotifier) LogManager(org.apache.logging.log4j.LogManager) BlockJUnit4ClassRunner(org.junit.runners.BlockJUnit4ClassRunner) Runner(org.junit.runner.Runner) KanonizoTestFailure(org.kanonizo.junit.KanonizoTestFailure) Request(org.junit.runner.Request) ClassRequest(org.junit.internal.requests.ClassRequest) KanonizoTestResult(org.kanonizo.junit.KanonizoTestResult) Result(org.junit.runner.Result) KanonizoTestResult(org.kanonizo.junit.KanonizoTestResult)

Example 2 with KanonizoTestResult

use of org.kanonizo.junit.KanonizoTestResult in project kanonizo by kanonizo.

the class TestCase method run.

/**
 * Executes a single test method on the JUnitCore class, using default
 * Runners and configuration. This method must reload the class from the
 * class loader as it will have been instrumented since it is first created.
 * If the instrumented version is not loaded, code coverage goes a little
 * bit funky.
 *
 * @throws ClassNotFoundException if the ClassLoader can't find the {@link #testClass} by name
 */
public void run() {
    long startTime = System.currentTimeMillis();
    // reload testclass from memory class loader to get the instrumented
    // version
    Task timerTask = new TestCaseExecutionTimer(testClass.getName(), testMethod.getName());
    if (InstrumentationProperties.LOG) {
        TaskTimer.taskStart(timerTask);
    }
    File rootFolder;
    if (EXECUTE_IN_ROOT_FOLDER && (rootFolder = Framework.getInstance().getRootFolder()) != null) {
        System.setProperty("user.dir", rootFolder.getAbsolutePath());
    }
    KanonizoTestRunner testCaseRunner = TestingUtils.isJUnit4Class(testClass) ? new JUnit4TestRunner() : new JUnit3TestRunner();
    KanonizoTestResult result = null;
    if (USE_TIMEOUT) {
        ExecutorService service = Executors.newSingleThreadExecutor();
        Future<KanonizoTestResult> res = service.submit(() -> testCaseRunner.runTest(this));
        try {
            result = res.get(TIMEOUT, UNIT);
        } catch (TimeoutException e) {
            logger.debug("Test " + testMethod.getName() + " timed out.");
            return;
        } catch (InterruptedException e) {
            logger.error(e);
        } catch (ExecutionException e) {
            logger.error(e);
        }
    } else {
        result = testCaseRunner.runTest(this);
    }
    setResult(result);
    if (InstrumentationProperties.LOG) {
        TaskTimer.taskEnd(timerTask);
    }
}
Also used : Task(com.scythe.instrumenter.analysis.task.Task) AbstractTask(com.scythe.instrumenter.analysis.task.AbstractTask) KanonizoTestRunner(org.kanonizo.junit.runners.KanonizoTestRunner) JUnit3TestRunner(org.kanonizo.junit.runners.JUnit3TestRunner) JUnit4TestRunner(org.kanonizo.junit.runners.JUnit4TestRunner) ExecutorService(java.util.concurrent.ExecutorService) KanonizoTestResult(org.kanonizo.junit.KanonizoTestResult) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with KanonizoTestResult

use of org.kanonizo.junit.KanonizoTestResult in project kanonizo by kanonizo.

the class JUnit3TestRunner method runTest.

@Override
public KanonizoTestResult runTest(TestCase tc) {
    Test test = createMethodSuite(tc.getTestClass(), tc.getMethod());
    if (test == null) {
        logger.error("Unable to create test case");
        throw new RuntimeException();
    }
    long startTime = System.currentTimeMillis();
    TestResult result = doRun(test, false);
    long runTime = System.currentTimeMillis() - startTime;
    List<KanonizoTestFailure> failures = new ArrayList<>();
    Enumeration<TestFailure> testFailures = result.failures();
    Enumeration<TestFailure> errors = result.errors();
    List<TestFailure> allErrors = Util.combine(testFailures, errors);
    for (TestFailure f : allErrors) {
        failures.add(new KanonizoTestFailure(f.thrownException(), f.trace()));
    }
    return new KanonizoTestResult(tc.getTestClass(), tc.getMethod(), result.wasSuccessful(), failures, runTime);
}
Also used : Test(junit.framework.Test) KanonizoTestFailure(org.kanonizo.junit.KanonizoTestFailure) TestFailure(junit.framework.TestFailure) KanonizoTestFailure(org.kanonizo.junit.KanonizoTestFailure) ArrayList(java.util.ArrayList) TestResult(junit.framework.TestResult) KanonizoTestResult(org.kanonizo.junit.KanonizoTestResult) KanonizoTestResult(org.kanonizo.junit.KanonizoTestResult)

Aggregations

KanonizoTestResult (org.kanonizo.junit.KanonizoTestResult)3 ArrayList (java.util.ArrayList)2 KanonizoTestFailure (org.kanonizo.junit.KanonizoTestFailure)2 AbstractTask (com.scythe.instrumenter.analysis.task.AbstractTask)1 Task (com.scythe.instrumenter.analysis.task.Task)1 File (java.io.File)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Arrays (java.util.Arrays)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 TimeoutException (java.util.concurrent.TimeoutException)1 Collectors (java.util.stream.Collectors)1 Test (junit.framework.Test)1 TestFailure (junit.framework.TestFailure)1 TestResult (junit.framework.TestResult)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1