下面列出了org.junit.runner.Runner#run() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Run the tests in the supplied {@code testClass}, using the specified
* {@link Runner}, and assert the expectations of the test execution.
*
* <p>If the specified {@code runnerClass} is {@code null}, the tests
* will be run with the runner that the test class is configured with
* (i.e., via {@link RunWith @RunWith}) or the default JUnit runner.
*
* @param runnerClass the explicit runner class to use or {@code null}
* if the implicit runner should be used
* @param testClass the test class to run with JUnit
* @param expectedStartedCount the expected number of tests that started
* @param expectedFailedCount the expected number of tests that failed
* @param expectedFinishedCount the expected number of tests that finished
* @param expectedIgnoredCount the expected number of tests that were ignored
* @param expectedAssumptionFailedCount the expected number of tests that
* resulted in a failed assumption
*/
public static void runTestsAndAssertCounters(Class<? extends Runner> runnerClass, Class<?> testClass,
int expectedStartedCount, int expectedFailedCount, int expectedFinishedCount, int expectedIgnoredCount,
int expectedAssumptionFailedCount) throws Exception {
TrackingRunListener listener = new TrackingRunListener();
if (runnerClass != null) {
Constructor<?> constructor = runnerClass.getConstructor(Class.class);
Runner runner = (Runner) BeanUtils.instantiateClass(constructor, testClass);
RunNotifier notifier = new RunNotifier();
notifier.addListener(listener);
runner.run(notifier);
}
else {
JUnitCore junit = new JUnitCore();
junit.addListener(listener);
junit.run(testClass);
}
assertEquals("tests started for [" + testClass + "]:", expectedStartedCount, listener.getTestStartedCount());
assertEquals("tests failed for [" + testClass + "]:", expectedFailedCount, listener.getTestFailureCount());
assertEquals("tests finished for [" + testClass + "]:", expectedFinishedCount, listener.getTestFinishedCount());
assertEquals("tests ignored for [" + testClass + "]:", expectedIgnoredCount, listener.getTestIgnoredCount());
assertEquals("failed assumptions for [" + testClass + "]:", expectedAssumptionFailedCount, listener.getTestAssumptionFailureCount());
}
/**
* Run the tests in the supplied {@code testClass}, using the specified
* {@link Runner}, and assert the expectations of the test execution.
*
* <p>If the specified {@code runnerClass} is {@code null}, the tests
* will be run with the runner that the test class is configured with
* (i.e., via {@link RunWith @RunWith}) or the default JUnit runner.
*
* @param runnerClass the explicit runner class to use or {@code null}
* if the default JUnit runner should be used
* @param testClass the test class to run with JUnit
* @param expectedStartedCount the expected number of tests that started
* @param expectedFailedCount the expected number of tests that failed
* @param expectedFinishedCount the expected number of tests that finished
* @param expectedIgnoredCount the expected number of tests that were ignored
* @param expectedAssumptionFailedCount the expected number of tests that
* resulted in a failed assumption
*/
public static void runTestsAndAssertCounters(Class<? extends Runner> runnerClass, Class<?> testClass,
int expectedStartedCount, int expectedFailedCount, int expectedFinishedCount, int expectedIgnoredCount,
int expectedAssumptionFailedCount) throws Exception {
TrackingRunListener listener = new TrackingRunListener();
if (runnerClass != null) {
Constructor<?> constructor = runnerClass.getConstructor(Class.class);
Runner runner = (Runner) BeanUtils.instantiateClass(constructor, testClass);
RunNotifier notifier = new RunNotifier();
notifier.addListener(listener);
runner.run(notifier);
}
else {
JUnitCore junit = new JUnitCore();
junit.addListener(listener);
junit.run(testClass);
}
// @formatter:off
assertAll(
() -> assertEquals(expectedStartedCount, listener.getTestStartedCount(), "tests started for [" + testClass + "]"),
() -> assertEquals(expectedFailedCount, listener.getTestFailureCount(), "tests failed for [" + testClass + "]"),
() -> assertEquals(expectedFinishedCount, listener.getTestFinishedCount(), "tests finished for [" + testClass + "]"),
() -> assertEquals(expectedIgnoredCount, listener.getTestIgnoredCount(), "tests ignored for [" + testClass + "]"),
() -> assertEquals(expectedAssumptionFailedCount, listener.getTestAssumptionFailureCount(), "failed assumptions for [" + testClass + "]")
);
// @formatter:on
}
/**
* Run the tests in the supplied {@code testClass}, using the specified
* {@link Runner}, and assert the expectations of the test execution.
*
* <p>If the specified {@code runnerClass} is {@code null}, the tests
* will be run with the runner that the test class is configured with
* (i.e., via {@link RunWith @RunWith}) or the default JUnit runner.
*
* @param runnerClass the explicit runner class to use or {@code null}
* if the default JUnit runner should be used
* @param testClass the test class to run with JUnit
* @param expectedStartedCount the expected number of tests that started
* @param expectedFailedCount the expected number of tests that failed
* @param expectedFinishedCount the expected number of tests that finished
* @param expectedIgnoredCount the expected number of tests that were ignored
* @param expectedAssumptionFailedCount the expected number of tests that
* resulted in a failed assumption
*/
public static void runTestsAndAssertCounters(Class<? extends Runner> runnerClass, Class<?> testClass,
int expectedStartedCount, int expectedFailedCount, int expectedFinishedCount, int expectedIgnoredCount,
int expectedAssumptionFailedCount) throws Exception {
TrackingRunListener listener = new TrackingRunListener();
if (runnerClass != null) {
Constructor<?> constructor = runnerClass.getConstructor(Class.class);
Runner runner = (Runner) BeanUtils.instantiateClass(constructor, testClass);
RunNotifier notifier = new RunNotifier();
notifier.addListener(listener);
runner.run(notifier);
}
else {
JUnitCore junit = new JUnitCore();
junit.addListener(listener);
junit.run(testClass);
}
// @formatter:off
assertAll(
() -> assertEquals(expectedStartedCount, listener.getTestStartedCount(), "tests started for [" + testClass + "]"),
() -> assertEquals(expectedFailedCount, listener.getTestFailureCount(), "tests failed for [" + testClass + "]"),
() -> assertEquals(expectedFinishedCount, listener.getTestFinishedCount(), "tests finished for [" + testClass + "]"),
() -> assertEquals(expectedIgnoredCount, listener.getTestIgnoredCount(), "tests ignored for [" + testClass + "]"),
() -> assertEquals(expectedAssumptionFailedCount, listener.getTestAssumptionFailureCount(), "failed assumptions for [" + testClass + "]")
);
// @formatter:on
}
private void runEnabledTests(RunNotifier nested) {
if (enabledTests.isEmpty()) {
return;
}
Runner runner;
try {
runner = createExecutionRunner();
} catch (Throwable t) {
runner = new CannotExecuteRunner(getDisplayName(), target, t);
}
try {
if (!disabledTests.isEmpty()) {
((Filterable) runner).filter(new Filter() {
@Override
public boolean shouldRun(Description description) {
return !disabledTests.contains(description);
}
@Override
public String describe() {
return "disabled tests";
}
});
}
} catch (NoTestsRemainException e) {
return;
}
runner.run(nested);
}
private void runTestClass(String testClassName) throws ClassNotFoundException {
final Class<?> testClass = Class.forName(testClassName, false, applicationClassLoader);
Request request = Request.aClass(testClass);
if (options.hasCategoryConfiguration()) {
Transformer<Class<?>, String> transformer = new Transformer<Class<?>, String>() {
public Class<?> transform(final String original) {
try {
return applicationClassLoader.loadClass(original);
} catch (ClassNotFoundException e) {
throw new InvalidUserDataException(String.format("Can't load category class [%s].", original), e);
}
}
};
request = request.filterWith(new CategoryFilter(
CollectionUtils.collect(options.getIncludeCategories(), transformer),
CollectionUtils.collect(options.getExcludeCategories(), transformer)
));
}
if (!options.getIncludedTests().isEmpty()) {
request = request.filterWith(new MethodNameFilter(options.getIncludedTests()));
}
Runner runner = request.getRunner();
//In case of no matching methods junit will return a ErrorReportingRunner for org.junit.runner.manipulation.Filter.class.
//Will be fixed with adding class filters
if (!org.junit.runner.manipulation.Filter.class.getName().equals(runner.getDescription().getDisplayName())) {
RunNotifier notifier = new RunNotifier();
notifier.addListener(listener);
runner.run(notifier);
}
}
private void runEnabledTests(RunNotifier nested) {
if (enabledTests.isEmpty()) {
return;
}
Runner runner;
try {
runner = createExecutionRunner();
} catch (Throwable t) {
runner = new CannotExecuteRunner(getDisplayName(), target, t);
}
try {
if (!disabledTests.isEmpty()) {
((Filterable) runner).filter(new Filter() {
@Override
public boolean shouldRun(Description description) {
return !disabledTests.contains(description);
}
@Override
public String describe() {
return "disabled tests";
}
});
}
} catch (NoTestsRemainException e) {
return;
}
runner.run(nested);
}
private void runTestClass(String testClassName) throws ClassNotFoundException {
final Class<?> testClass = Class.forName(testClassName, true, applicationClassLoader);
Request request = Request.aClass(testClass);
if (options.hasCategoryConfiguration()) {
Transformer<Class<?>, String> transformer = new Transformer<Class<?>, String>() {
public Class<?> transform(final String original) {
try {
return applicationClassLoader.loadClass(original);
} catch (ClassNotFoundException e) {
throw new InvalidUserDataException(String.format("Can't load category class [%s].", original), e);
}
}
};
request = request.filterWith(new CategoryFilter(
CollectionUtils.collect(options.getIncludeCategories(), transformer),
CollectionUtils.collect(options.getExcludeCategories(), transformer)
));
}
if (!options.getIncludedTests().isEmpty()) {
request = request.filterWith(new MethodNameFilter(options.getIncludedTests()));
}
Runner runner = request.getRunner();
//In case of no matching methods junit will return a ErrorReportingRunner for org.junit.runner.manipulation.Filter.class.
//Will be fixed with adding class filters
if (!org.junit.runner.manipulation.Filter.class.getName().equals(runner.getDescription().getDisplayName())) {
RunNotifier notifier = new RunNotifier();
notifier.addListener(listener);
runner.run(notifier);
}
}
public static List<Failure> runTest(String fullQualifiedName, String testCaseName, String[] classpath) throws MalformedURLException, ClassNotFoundException {
ClassLoader classLoader = new URLClassLoader(
arrayStringToArrayUrl.apply(classpath),
ClassLoader.getSystemClassLoader()
);
Request request = Request.method(classLoader.loadClass(fullQualifiedName), testCaseName);
Runner runner = request.getRunner();
RunNotifier fNotifier = new RunNotifier();
final TestListener listener = new TestListener();
fNotifier.addFirstListener(listener);
fNotifier.fireTestRunStarted(runner.getDescription());
runner.run(fNotifier);
return listener.getTestFails();
}
public static List<Failure> runTest(String fullQualifiedName, String[] classpath) throws MalformedURLException, ClassNotFoundException {
ClassLoader classLoader = new URLClassLoader(
arrayStringToArrayUrl.apply(classpath),
ClassLoader.getSystemClassLoader()
);
Request request = Request.classes(classLoader.loadClass(fullQualifiedName));
Runner runner = request.getRunner();
RunNotifier fNotifier = new RunNotifier();
final TestListener listener = new TestListener();
fNotifier.addFirstListener(listener);
fNotifier.fireTestRunStarted(runner.getDescription());
runner.run(fNotifier);
return listener.getTestFails();
}
private void runEnabledTests(RunNotifier nested) {
if (enabledTests.isEmpty()) {
return;
}
Runner runner;
try {
runner = createExecutionRunner();
} catch (Throwable t) {
runner = new CannotExecuteRunner(getDisplayName(), target, t);
}
try {
if (!disabledTests.isEmpty()) {
((Filterable) runner).filter(new Filter() {
@Override
public boolean shouldRun(Description description) {
return !disabledTests.contains(description);
}
@Override
public String describe() {
return "disabled tests";
}
});
}
} catch (NoTestsRemainException e) {
return;
}
runner.run(nested);
}
private void runTestClass(String testClassName) throws ClassNotFoundException {
final Class<?> testClass = Class.forName(testClassName, false, applicationClassLoader);
Request request = Request.aClass(testClass);
if (options.hasCategoryConfiguration()) {
Transformer<Class<?>, String> transformer = new Transformer<Class<?>, String>() {
public Class<?> transform(final String original) {
try {
return applicationClassLoader.loadClass(original);
} catch (ClassNotFoundException e) {
throw new InvalidUserDataException(String.format("Can't load category class [%s].", original), e);
}
}
};
request = request.filterWith(new CategoryFilter(
CollectionUtils.collect(options.getIncludeCategories(), transformer),
CollectionUtils.collect(options.getExcludeCategories(), transformer)
));
}
if (!options.getIncludedTests().isEmpty()) {
request = request.filterWith(new MethodNameFilter(options.getIncludedTests()));
}
Runner runner = request.getRunner();
//In case of no matching methods junit will return a ErrorReportingRunner for org.junit.runner.manipulation.Filter.class.
//Will be fixed with adding class filters
if (!org.junit.runner.manipulation.Filter.class.getName().equals(runner.getDescription().getDisplayName())) {
RunNotifier notifier = new RunNotifier();
notifier.addListener(listener);
runner.run(notifier);
}
}
private void runEnabledTests(RunNotifier nested) {
if (enabledTests.isEmpty()) {
return;
}
Runner runner;
try {
runner = createExecutionRunner();
} catch (Throwable t) {
runner = new CannotExecuteRunner(getDisplayName(), target, t);
}
try {
if (!disabledTests.isEmpty()) {
((Filterable) runner).filter(new Filter() {
@Override
public boolean shouldRun(Description description) {
return !disabledTests.contains(description);
}
@Override
public String describe() {
return "disabled tests";
}
});
}
} catch (NoTestsRemainException e) {
return;
}
runner.run(nested);
}
private void runTestClass(String testClassName) throws ClassNotFoundException {
final Class<?> testClass = Class.forName(testClassName, true, applicationClassLoader);
Request request = Request.aClass(testClass);
if (options.hasCategoryConfiguration()) {
Transformer<Class<?>, String> transformer = new Transformer<Class<?>, String>() {
public Class<?> transform(final String original) {
try {
return applicationClassLoader.loadClass(original);
} catch (ClassNotFoundException e) {
throw new InvalidUserDataException(String.format("Can't load category class [%s].", original), e);
}
}
};
request = request.filterWith(new CategoryFilter(
CollectionUtils.collect(options.getIncludeCategories(), transformer),
CollectionUtils.collect(options.getExcludeCategories(), transformer)
));
}
if (!options.getIncludedTests().isEmpty()) {
request = request.filterWith(new MethodNameFilter(options.getIncludedTests()));
}
Runner runner = request.getRunner();
//In case of no matching methods junit will return a ErrorReportingRunner for org.junit.runner.manipulation.Filter.class.
//Will be fixed with adding class filters
if (!org.junit.runner.manipulation.Filter.class.getName().equals(runner.getDescription().getDisplayName())) {
RunNotifier notifier = new RunNotifier();
notifier.addListener(listener);
runner.run(notifier);
}
}
@Before
public void before() throws Exception {
final Runner runner = new Spectrum(Fixture.getSpecWithPassingAndFailingTests());
this.runNotifier = mock(RunNotifier.class);
runner.run(this.runNotifier);
}
@Override
protected void runChild(Runner runner, RunNotifier notifier) {
notifier.addListener(listener);
runner.run(notifier);
notifier.removeListener(listener);
}
@Override
protected void runChild(Runner child, RunNotifier notifier) {
child.run(notifier);
}
@Override
protected void runChild(final Runner child, final RunNotifier notifier) {
child.run(notifier);
}
@Override
protected void runChild(Runner child, RunNotifier notifier) {
child.run(notifier);
}
@Override
protected void runChild(Runner child, RunNotifier notifier) {
child.run(notifier);
}
/**
* Runs a child
*
* @param child
* The child you want to run
* @param notifier
* The notifier
*/
@Override
protected void runChild(Runner child, RunNotifier notifier) {
child.run(notifier);
}