Search in sources :

Example 31 with TestResult

use of junit.framework.TestResult in project cucumber-jvm by cucumber.

the class SanityChecker method run.

static void run(Class<?> testClass, boolean debug) {
    JUnit4TestAdapter testAdapter = new JUnit4TestAdapter(testClass);
    TestResult result = new TestResult();
    SanityChecker listener = new SanityChecker();
    result.addListener(listener);
    testAdapter.run(result);
    String output = listener.getOutput();
    if (output.contains(INSANITY)) {
        throw new RuntimeException("Something went wrong\n" + output);
    }
    if (debug) {
        System.out.println("===== " + testClass.getName());
        System.out.println(output);
        System.out.println("=====");
    }
}
Also used : TestResult(junit.framework.TestResult) JUnit4TestAdapter(junit.framework.JUnit4TestAdapter)

Example 32 with TestResult

use of junit.framework.TestResult in project cucumber-jvm by cucumber.

the class SanityChecker method run.

public static void run(Class<?> testClass, boolean debug) {
    JUnit4TestAdapter testAdapter = new JUnit4TestAdapter(testClass);
    TestResult result = new TestResult();
    SanityChecker listener = new SanityChecker();
    result.addListener(listener);
    testAdapter.run(result);
    String output = listener.getOutput();
    if (output.contains(INSANITY)) {
        throw new RuntimeException("Something went wrong\n" + output);
    }
    if (debug) {
        System.out.println("===== " + testClass.getName());
        System.out.println(output);
        System.out.println("=====");
    }
}
Also used : TestResult(junit.framework.TestResult) JUnit4TestAdapter(junit.framework.JUnit4TestAdapter)

Example 33 with TestResult

use of junit.framework.TestResult in project nutz by nutzam.

the class AdvancedTestAll method main.

public static void main(String[] args) {
    // 得到所有带@Test的方法
    List<Class<?>> list = Scans.me().scanPackage("org.nutz");
    List<Request> reqs = new ArrayList<Request>();
    Map<Request, Method> reqMap = new HashMap<Request, Method>();
    for (Class<?> clazz : list) {
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            if (method.getAnnotation(Test.class) != null) {
                Request req = Request.method(clazz, method.getName());
                reqs.add(req);
                reqMap.put(req, method);
            }
        }
    }
    System.out.printf("Found %d Test Class\n", reqs.size());
    // 先按普通顺序跑一次
    TestResult simpleTestResult = test(reqs, "normal", reqMap);
    // 倒序跑一次
    Collections.reverse(reqs);
    TestResult reverseTestResult = test(reqs, "reverse", reqMap);
    // 乱序跑3次
    Collections.shuffle(reqs);
    TestResult shuffleTestResult_A = test(reqs, "shuffle_A", reqMap);
    Collections.shuffle(reqs);
    TestResult shuffleTestResult_B = test(reqs, "shuffle_B", reqMap);
    Collections.shuffle(reqs);
    TestResult shuffleTestResult_C = test(reqs, "shuffle_C", reqMap);
    System.out.println("正常顺序------------------------------------------------");
    printResult(simpleTestResult);
    System.out.println("反序------------------------------------------------");
    printResult(reverseTestResult);
    System.out.println("乱序A------------------------------------------------");
    printResult(shuffleTestResult_A);
    System.out.println("乱序B------------------------------------------------");
    printResult(shuffleTestResult_B);
    System.out.println("乱序C------------------------------------------------");
    printResult(shuffleTestResult_C);
    System.out.println("-------------------------------------------------------Done");
}
Also used : HashMap(java.util.HashMap) Test(org.junit.Test) Request(org.junit.runner.Request) ArrayList(java.util.ArrayList) TestResult(junit.framework.TestResult) Method(java.lang.reflect.Method)

Example 34 with TestResult

use of junit.framework.TestResult in project jmeter by apache.

the class JUnitSampler method sample.

/**
 * {@inheritDoc}
 */
@Override
@SuppressWarnings("JdkObsolete")
public SampleResult sample(Entry entry) {
    if (getCreateOneInstancePerSample()) {
        initializeTestObject();
    }
    SampleResult sresult = new SampleResult();
    // Bug 41522 - don't use rlabel here
    sresult.setSampleLabel(getName());
    sresult.setSamplerData(className + "." + methodName);
    sresult.setDataType(SampleResult.TEXT);
    // Assume success
    sresult.setSuccessful(true);
    sresult.setResponseMessage(getSuccess());
    sresult.setResponseCode(getSuccessCode());
    if (this.testCase != null) {
        // create a new TestResult
        TestResult tr = new TestResult();
        final TestCase theClazz = this.testCase;
        try {
            if (setUpMethod != null) {
                setUpMethod.invoke(this.testObject, new Object[0]);
            }
            sresult.sampleStart();
            tr.startTest(this.testCase);
            // Do not use TestCase.run(TestResult) method, since it will
            // call setUp and tearDown. Doing that will result in calling
            // the setUp and tearDown method twice and the elapsed time
            // will include setup and teardown.
            tr.runProtected(theClazz, protectable);
            tr.endTest(this.testCase);
            sresult.sampleEnd();
            if (tearDownMethod != null) {
                tearDownMethod.invoke(testObject, new Object[0]);
            }
        } catch (InvocationTargetException e) {
            Throwable cause = e.getCause();
            if (cause instanceof AssertionFailedError) {
                tr.addFailure(theClazz, (AssertionFailedError) cause);
            } else if (cause instanceof AssertionError) {
                // Convert JUnit4 failure to Junit3 style
                AssertionFailedError afe = new AssertionFailedError(cause.toString());
                // copy the original stack trace
                afe.setStackTrace(cause.getStackTrace());
                tr.addFailure(theClazz, afe);
            } else if (cause != null) {
                tr.addError(theClazz, cause);
                log.info("caught exception", e);
            } else {
                tr.addError(theClazz, e);
                log.info("caught exception", e);
            }
        } catch (IllegalAccessException | IllegalArgumentException e) {
            tr.addError(theClazz, e);
        }
        if (!tr.wasSuccessful()) {
            sresult.setSuccessful(false);
            StringBuilder buf = new StringBuilder();
            StringBuilder buftrace = new StringBuilder();
            Enumeration<TestFailure> en;
            if (getAppendError()) {
                en = tr.failures();
                if (en.hasMoreElements()) {
                    sresult.setResponseCode(getFailureCode());
                    buf.append(getFailure());
                    buf.append("\n");
                }
                while (en.hasMoreElements()) {
                    TestFailure item = en.nextElement();
                    buf.append("Failure -- ");
                    buf.append(item.toString());
                    buf.append("\n");
                    buftrace.append("Failure -- ");
                    buftrace.append(item.toString());
                    buftrace.append("\n");
                    buftrace.append("Trace -- ");
                    buftrace.append(item.trace());
                }
                en = tr.errors();
                if (en.hasMoreElements()) {
                    sresult.setResponseCode(getErrorCode());
                    buf.append(getError());
                    buf.append("\n");
                }
                while (en.hasMoreElements()) {
                    TestFailure item = en.nextElement();
                    buf.append("Error -- ");
                    buf.append(item.toString());
                    buf.append("\n");
                    buftrace.append("Error -- ");
                    buftrace.append(item.toString());
                    buftrace.append("\n");
                    buftrace.append("Trace -- ");
                    buftrace.append(item.trace());
                }
            }
            sresult.setResponseMessage(buf.toString());
            sresult.setResponseData(buftrace.toString(), null);
        }
    } else {
        // we should log a warning, but allow the test to keep running
        sresult.setSuccessful(false);
        // this should be externalized to the properties
        sresult.setResponseMessage("Failed to create an instance of the class:" + getClassname() + ", reasons may be missing both empty constructor and one " + "String constructor or failure to instantiate constructor," + " check warning messages in jmeter log file");
        sresult.setResponseCode(getErrorCode());
    }
    return sresult;
}
Also used : TestFailure(junit.framework.TestFailure) TestResult(junit.framework.TestResult) InvocationTargetException(java.lang.reflect.InvocationTargetException) TestCase(junit.framework.TestCase) SampleResult(org.apache.jmeter.samplers.SampleResult) AssertionFailedError(junit.framework.AssertionFailedError)

Example 35 with TestResult

use of junit.framework.TestResult in project groovy by apache.

the class SecurityTestSupport method executeTest.

protected void executeTest(Class test, Permission missingPermission) {
    TestSuite suite = new TestSuite();
    suite.addTestSuite(test);
    TestResult result = new TestResult();
    suite.run(result);
    if (result.wasSuccessful()) {
        if (missingPermission == null) {
            return;
        } else {
            fail("Security test expected an AccessControlException on " + missingPermission + ", but did not receive one");
        }
    } else {
        if (missingPermission == null) {
            new SecurityTestResultPrinter(System.out).print(result);
            fail("Security test was expected to run successfully, but failed (results on System.out)");
        } else {
            // There may be more than 1 failure:  iterate to ensure that they all match the missingPermission.
            boolean otherFailure = false;
            for (Enumeration e = result.errors(); e.hasMoreElements(); ) {
                TestFailure failure = (TestFailure) e.nextElement();
                if (failure.thrownException() instanceof AccessControlException) {
                    AccessControlException ace = (AccessControlException) failure.thrownException();
                    if (missingPermission.implies(ace.getPermission())) {
                        continue;
                    }
                }
                otherFailure = true;
                break;
            }
            if (otherFailure) {
                new SecurityTestResultPrinter(System.out).print(result);
                fail("Security test expected an AccessControlException on " + missingPermission + ", but failed for other reasons (results on System.out)");
            }
        }
    }
}
Also used : Enumeration(java.util.Enumeration) TestSuite(junit.framework.TestSuite) TestFailure(junit.framework.TestFailure) AccessControlException(java.security.AccessControlException) TestResult(junit.framework.TestResult)

Aggregations

TestResult (junit.framework.TestResult)108 TestSuite (junit.framework.TestSuite)33 Test (junit.framework.Test)19 JUnit4TestAdapter (junit.framework.JUnit4TestAdapter)17 TestCase (junit.framework.TestCase)14 TestFailure (junit.framework.TestFailure)12 Test (org.junit.Test)10 TestListener (junit.framework.TestListener)9 ArrayList (java.util.ArrayList)8 PrintStream (java.io.PrintStream)7 RepeatedTest (junit.extensions.RepeatedTest)7 Enumeration (java.util.Enumeration)6 Bundle (android.os.Bundle)5 HandlerThread (android.os.HandlerThread)5 ShellUiAutomatorBridge (com.android.uiautomator.core.ShellUiAutomatorBridge)5 Tracer (com.android.uiautomator.core.Tracer)5 UiAutomationShellWrapper (com.android.uiautomator.core.UiAutomationShellWrapper)5 AssertionFailedError (junit.framework.AssertionFailedError)5 IOException (java.io.IOException)4 Iterator (java.util.Iterator)4