Search in sources :

Example 51 with Request

use of org.junit.runner.Request in project junit4 by junit-team.

the class MaxStarterTest method preferNewTestsOverTestsThatFailed.

// This covers a seemingly-unlikely case, where you had a test that failed
// on the
// last run and you also introduced new tests. In such a case it pretty much
// doesn't matter
// which order they run, you just want them both to be early in the sequence
@Test
public void preferNewTestsOverTestsThatFailed() {
    Request one = Request.method(TwoTests.class, "dontSucceed");
    fMax.run(one);
    Request two = Request.aClass(TwoTests.class);
    List<Description> things = fMax.sortedLeavesForTest(two);
    Description succeed = Description.createTestDescription(TwoTests.class, "succeed");
    assertEquals(succeed, things.get(0));
    assertEquals(2, things.size());
}
Also used : Description(org.junit.runner.Description) Request(org.junit.runner.Request) Test(org.junit.Test)

Example 52 with Request

use of org.junit.runner.Request in project nutz by nutzam.

the class AdvancedTestAll method test.

public static TestResult test(List<Request> reqs, String name, Map<Request, Method> reqMap) {
    // TODO 根据order文件还原测试顺序
    try {
        FileWriter fw = new FileWriter("./test_order_" + name + ".txt");
        for (Request request : reqs) {
            fw.write(reqMap.get(request).toString());
            fw.write("\n");
        }
        fw.flush();
        fw.close();
    } catch (IOException e) {
    }
    final TestResult result = new TestResult();
    RunNotifier notifier = new RunNotifier();
    notifier.addListener(new RunListener() {

        public void testFailure(Failure failure) throws Exception {
            result.addError(asTest(failure.getDescription()), failure.getException());
        }

        public void testFinished(Description description) throws Exception {
            result.endTest(asTest(description));
        }

        public void testStarted(Description description) throws Exception {
            result.startTest(asTest(description));
        }

        public junit.framework.Test asTest(Description description) {
            return new junit.framework.Test() {

                public void run(TestResult result) {
                    throw Lang.noImplement();
                }

                public int countTestCases() {
                    return 1;
                }
            };
        }
    });
    for (Request request : reqs) {
        request.getRunner().run(notifier);
    }
    return result;
}
Also used : RunNotifier(org.junit.runner.notification.RunNotifier) Description(org.junit.runner.Description) FileWriter(java.io.FileWriter) Request(org.junit.runner.Request) TestResult(junit.framework.TestResult) IOException(java.io.IOException) IOException(java.io.IOException) RunListener(org.junit.runner.notification.RunListener) Test(org.junit.Test) TestFailure(junit.framework.TestFailure) Failure(org.junit.runner.notification.Failure)

Example 53 with Request

use of org.junit.runner.Request in project junit-interface by sbt.

the class JUnitRunner method createTask.

private Task createTask(final TaskDef taskDef) {
    return new Task() {

        @Override
        public String[] tags() {
            // no tags yet
            return new String[0];
        }

        @Override
        public TaskDef taskDef() {
            return taskDef;
        }

        @Override
        public Task[] execute(EventHandler eventHandler, Logger[] loggers) {
            Fingerprint fingerprint = taskDef.fingerprint();
            String testClassName = taskDef.fullyQualifiedName();
            boolean quiet = false, verbose = false, nocolor = false, decodeScalaNames = false, logAssert = true, logExceptionClass = true;
            HashMap<String, String> sysprops = new HashMap<String, String>();
            ArrayList<String> globPatterns = new ArrayList<String>();
            Set<String> includeCategories = new HashSet<String>();
            Set<String> excludeCategories = new HashSet<String>();
            String testFilter = "";
            String ignoreRunners = "org.junit.runners.Suite";
            String runListener = null;
            for (String s : args) {
                if ("-q".equals(s))
                    quiet = true;
                else if ("-v".equals(s))
                    verbose = true;
                else if ("-n".equals(s))
                    nocolor = true;
                else if ("-s".equals(s))
                    decodeScalaNames = true;
                else if ("-a".equals(s))
                    logAssert = true;
                else if ("-c".equals(s))
                    logExceptionClass = false;
                else if (s.startsWith("-tests=")) {
                    for (Logger l : loggers) l.warn("junit-interface option \"-tests\" is deprecated. Use \"--tests\" instead.");
                    testFilter = s.substring(7);
                } else if (s.startsWith("--tests="))
                    testFilter = s.substring(8);
                else if (s.startsWith("--ignore-runners="))
                    ignoreRunners = s.substring(17);
                else if (s.startsWith("--run-listener="))
                    runListener = s.substring(15);
                else if (s.startsWith("--include-categories="))
                    includeCategories.addAll(Arrays.asList(s.substring(21).split(",")));
                else if (s.startsWith("--exclude-categories="))
                    excludeCategories.addAll(Arrays.asList(s.substring(21).split(",")));
                else if (s.startsWith("-D") && s.contains("=")) {
                    int sep = s.indexOf('=');
                    sysprops.put(s.substring(2, sep), s.substring(sep + 1));
                } else if (!s.startsWith("-") && !s.startsWith("+"))
                    globPatterns.add(s);
            }
            for (String s : args) {
                if ("+q".equals(s))
                    quiet = false;
                else if ("+v".equals(s))
                    verbose = false;
                else if ("+n".equals(s))
                    nocolor = false;
                else if ("+s".equals(s))
                    decodeScalaNames = false;
                else if ("+a".equals(s))
                    logAssert = false;
                else if ("+c".equals(s))
                    logExceptionClass = true;
            }
            RunSettings settings = new RunSettings(!nocolor, decodeScalaNames, quiet, verbose, logAssert, ignoreRunners, logExceptionClass);
            RichLogger logger = new RichLogger(loggers, settings, testClassName);
            EventDispatcher ed = new EventDispatcher(logger, eventHandler, settings, fingerprint);
            JUnitCore ju = new JUnitCore();
            ju.addListener(ed);
            if (runListener != null) {
                ju.addListener(createRunListener(runListener));
            }
            HashMap<String, Object> oldprops = new HashMap<String, Object>();
            try {
                synchronized (System.getProperties()) {
                    for (Map.Entry<String, String> me : sysprops.entrySet()) {
                        String old = System.getProperty(me.getKey());
                        oldprops.put(me.getKey(), old == null ? NULL : old);
                    }
                    for (Map.Entry<String, String> me : sysprops.entrySet()) System.setProperty(me.getKey(), me.getValue());
                }
                try {
                    Class<?> cl = testClassLoader.loadClass(testClassName);
                    if (shouldRun(fingerprint, cl, settings)) {
                        Request request = Request.classes(cl);
                        if (globPatterns.size() > 0)
                            request = new SilentFilterRequest(request, new GlobFilter(settings, globPatterns));
                        if (testFilter.length() > 0)
                            request = new SilentFilterRequest(request, new TestFilter(testFilter, ed));
                        if (!includeCategories.isEmpty() || !excludeCategories.isEmpty()) {
                            request = new SilentFilterRequest(request, CategoryFilter.categoryFilter(true, loadClasses(testClassLoader, includeCategories), true, loadClasses(testClassLoader, excludeCategories)));
                        }
                        ju.run(request);
                    }
                } catch (Exception ex) {
                    ed.testExecutionFailed(testClassName, ex);
                }
            } finally {
                synchronized (System.getProperties()) {
                    for (Map.Entry<String, Object> me : oldprops.entrySet()) {
                        if (me.getValue() == NULL)
                            System.clearProperty(me.getKey());
                        else
                            System.setProperty(me.getKey(), (String) me.getValue());
                    }
                }
            }
            // junit tests do not nest
            return new Task[0];
        }
    };
}
Also used : Task(sbt.testing.Task) JUnitCore(org.junit.runner.JUnitCore) EventHandler(sbt.testing.EventHandler) Logger(sbt.testing.Logger) Fingerprint(sbt.testing.Fingerprint) Request(org.junit.runner.Request) Fingerprint(sbt.testing.Fingerprint)

Example 54 with Request

use of org.junit.runner.Request in project randomizedtesting by randomizedtesting.

the class SlaveMain method execute.

/**
   * Execute tests.
   */
private void execute(Iterator<String> classNames) throws Throwable {
    final RunNotifier fNotifier = new OrderedRunNotifier();
    final Result result = new Result();
    final Writer debug = debugMessagesFile == null ? new NullWriter() : new OutputStreamWriter(new FileOutputStream(debugMessagesFile), "UTF-8");
    fNotifier.addListener(result.createListener());
    fNotifier.addListener(new StreamFlusherDecorator(new NoExceptionRunListenerDecorator(new RunListenerEmitter(serializer)) {

        @Override
        protected void exception(Throwable t) {
            warn("Event serializer exception.", t);
        }
    }));
    fNotifier.addListener(new RunListener() {

        public void testRunFinished(Result result) throws Exception {
            debug(debug, "testRunFinished(T:" + result.getRunCount() + ";F:" + result.getFailureCount() + ";I:" + result.getIgnoreCount() + ")");
            serializer.flush();
        }

        @Override
        public void testRunStarted(Description description) throws Exception {
            debug(debug, "testRunStarted(" + description + ")");
            serializer.flush();
        }

        @Override
        public void testStarted(Description description) throws Exception {
            debug(debug, "testStarted(" + description + ")");
            serializer.flush();
        }

        public void testFinished(Description description) throws Exception {
            debug(debug, "testFinished(" + description + ")");
            serializer.flush();
        }

        @Override
        public void testIgnored(Description description) throws Exception {
            debug(debug, "testIgnored(T:" + description + ")");
        }

        @Override
        public void testFailure(Failure failure) throws Exception {
            debug(debug, "testFailure(T:" + failure + ")");
        }

        @Override
        public void testAssumptionFailure(Failure failure) {
            try {
                debug(debug, "testAssumptionFailure(T:" + failure + ")");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
    /*
     * Instantiate method filter if any.
     */
    String methodFilterGlob = Strings.emptyToNull(System.getProperty(SysGlobals.SYSPROP_TESTMETHOD()));
    Filter methodFilter = Filter.ALL;
    if (methodFilterGlob != null) {
        methodFilter = new MethodGlobFilter(methodFilterGlob);
    }
    /*
     * Important. Run each class separately so that we get separate 
     * {@link RunListener} callbacks for the top extracted description.
     */
    debug(debug, "Entering main suite loop.");
    try {
        while (classNames.hasNext()) {
            final String clName = classNames.next();
            debug(debug, "Instantiating: " + clName);
            Class<?> clazz = instantiate(clName);
            if (clazz == null)
                continue;
            Request request = Request.aClass(clazz);
            try {
                Runner runner = request.getRunner();
                methodFilter.apply(runner);
                fNotifier.fireTestRunStarted(runner.getDescription());
                debug(debug, "Runner.run(" + clName + ")");
                runner.run(fNotifier);
                debug(debug, "Runner.done(" + clName + ")");
                fNotifier.fireTestRunFinished(result);
            } catch (NoTestsRemainException e) {
            // Don't complain if all methods have been filtered out. 
            // I don't understand the reason why this exception has been
            // built in to filters at all.
            }
        }
    } catch (Throwable t) {
        debug(debug, "Main suite loop error: " + t);
        throw t;
    } finally {
        debug(debug, "Leaving main suite loop.");
        debug.close();
    }
}
Also used : Runner(org.junit.runner.Runner) Description(org.junit.runner.Description) MethodGlobFilter(com.carrotsearch.randomizedtesting.MethodGlobFilter) NoTestsRemainException(org.junit.runner.manipulation.NoTestsRemainException) Result(org.junit.runner.Result) Failure(org.junit.runner.notification.Failure) RunNotifier(org.junit.runner.notification.RunNotifier) Request(org.junit.runner.Request) IOException(java.io.IOException) IOException(java.io.IOException) NoTestsRemainException(org.junit.runner.manipulation.NoTestsRemainException) RunListener(org.junit.runner.notification.RunListener) MethodGlobFilter(com.carrotsearch.randomizedtesting.MethodGlobFilter) Filter(org.junit.runner.manipulation.Filter) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer)

Example 55 with Request

use of org.junit.runner.Request in project geode by apache.

the class CategoryWithParameterizedRunnerFactoryTest method testBrokenCategoryAndParameterized.

@Test
public void testBrokenCategoryAndParameterized() {
    Request request = Request.aClass(BrokenCategoryClass.class);
    ExposedParameterized runner = (ExposedParameterized) request.getRunner();
    request = request.filterWith(new CategoryFilter((ExposedBlockJUnit4ClassRunnerWithParameters) runner.getChildren().get(0)));
    Result result = new JUnitCore().run(request);
    assertEquals("Yeah!! This might actually mean we've upgraded to JUnit 4.13. Hurry up already and delete this hack.", 1, result.getRunCount());
}
Also used : JUnitCore(org.junit.runner.JUnitCore) Request(org.junit.runner.Request) Result(org.junit.runner.Result) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Aggregations

Request (org.junit.runner.Request)56 Test (org.junit.Test)37 JUnitCore (org.junit.runner.JUnitCore)33 Result (org.junit.runner.Result)23 Description (org.junit.runner.Description)20 Runner (org.junit.runner.Runner)9 TestSuiteModel (com.google.testing.junit.runner.model.TestSuiteModel)8 PrintableResult.testResult (org.junit.experimental.results.PrintableResult.testResult)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Filter (org.junit.runner.manipulation.Filter)6 Failure (org.junit.runner.notification.Failure)6 RunListener (org.junit.runner.notification.RunListener)6 OutputStream (java.io.OutputStream)5 CategoryFilter (org.junit.experimental.categories.Categories.CategoryFilter)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 InOrder (org.mockito.InOrder)4 Computer (org.junit.runner.Computer)3 RunNotifier (org.junit.runner.notification.RunNotifier)3