org.junit.runner.Request Java Examples

The following examples show how to use org.junit.runner.Request. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AndroidJUnitRunner.java    From android-test with Apache License 2.0 6 votes vote down vote up
/** Builds a {@link Request} based on given input arguments. */
@VisibleForTesting
Request buildRequest(RunnerArgs runnerArgs, Bundle bundleArgs) {

  TestRequestBuilder builder = createTestRequestBuilder(this, bundleArgs);
  builder.addPathsToScan(runnerArgs.classpathToScan);
  if (runnerArgs.classpathToScan.isEmpty()) {
    // Only scan for tests for current apk aka testContext
    // Note that this represents a change from InstrumentationTestRunner where
    // getTargetContext().getPackageCodePath() aka app under test was also scanned
    // Only add the package classpath when no custom classpath is provided in order to
    // avoid duplicate class issues.
    builder.addPathToScan(getContext().getPackageCodePath());
  }
  builder.addFromRunnerArgs(runnerArgs);

  registerUserTracker();

  return builder.build();
}
 
Example #2
Source File: JUnit4TestModelBuilderTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateModel_simpleSuite() throws Exception {
  Class<?> suiteClass = SampleSuite.class;
  Request request = Request.classWithoutSuiteMethod(suiteClass);
  String suiteClassName = suiteClass.getCanonicalName();
  JUnit4TestModelBuilder modelBuilder = builder(
      request, suiteClassName, stubShardingEnvironment, null, xmlResultWriter);

  Description topSuite = request.getRunner().getDescription();
  Description innerSuite = topSuite.getChildren().get(0);
  Description testOne = innerSuite.getChildren().get(0);

  TestSuiteModel model = modelBuilder.get();
  TestNode topSuiteNode = Iterables.getOnlyElement(model.getTopLevelTestSuites());
  assertThat(topSuiteNode.getDescription()).isEqualTo(topSuite);
  TestNode innerSuiteNode = Iterables.getOnlyElement(topSuiteNode.getChildren());
  assertThat(innerSuiteNode.getDescription()).isEqualTo(innerSuite);
  TestNode testOneNode = Iterables.getOnlyElement(innerSuiteNode.getChildren());
  assertThat(testOneNode.getDescription()).isEqualTo(testOne);
  assertThat(testOneNode.getChildren()).isEmpty();
  assertThat(model.getNumTestCases()).isEqualTo(1);
}
 
Example #3
Source File: JUnit4TestModelBuilderTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateModel_singleTestClass() throws Exception {
  Class<?> testClass = SampleTestCaseWithTwoTests.class;
  Request request = Request.classWithoutSuiteMethod(testClass);
  String testClassName = testClass.getCanonicalName();
  JUnit4TestModelBuilder modelBuilder = builder(
      request, testClassName, stubShardingEnvironment, null, xmlResultWriter);

  Description suite = request.getRunner().getDescription();
  Description testOne = suite.getChildren().get(0);
  Description testTwo = suite.getChildren().get(1);

  TestSuiteModel model = modelBuilder.get();
  TestNode suiteNode = Iterables.getOnlyElement(model.getTopLevelTestSuites());
  assertThat(suiteNode.getDescription()).isEqualTo(suite);
  List<TestNode> testCases = suiteNode.getChildren();
  assertThat(testCases).hasSize(2);
  TestNode testOneNode = testCases.get(0);
  TestNode testTwoNode = testCases.get(1);
  assertThat(testOneNode.getDescription()).isEqualTo(testOne);
  assertThat(testTwoNode.getDescription()).isEqualTo(testTwo);
  assertThat(testOneNode.getChildren()).isEmpty();
  assertThat(testTwoNode.getChildren()).isEmpty();
  assertThat(model.getNumTestCases()).isEqualTo(2);
}
 
Example #4
Source File: TestExecutor.java    From android-test with Apache License 2.0 6 votes vote down vote up
/** Execute the tests */
public Bundle execute(Request request) {
  Bundle resultBundle = new Bundle();
  Result junitResults = new Result();
  try {
    JUnitCore testRunner = new JUnitCore();
    setUpListeners(testRunner);
    junitResults = testRunner.run(request);
  } catch (Throwable t) {
    final String msg = "Fatal exception when running tests";
    Log.e(LOG_TAG, msg, t);
    junitResults.getFailures().add(new Failure(Description.createSuiteDescription(msg), t));
  } finally {
    ByteArrayOutputStream summaryStream = new ByteArrayOutputStream();
    // create the stream used to output summary data to the user
    PrintStream summaryWriter = new PrintStream(summaryStream);
    reportRunEnded(listeners, summaryWriter, resultBundle, junitResults);
    summaryWriter.close();
    resultBundle.putString(
        Instrumentation.REPORT_KEY_STREAMRESULT, String.format("\n%s", summaryStream.toString()));
  }
  return resultBundle;
}
 
Example #5
Source File: JUnit4RunnerFactory.java    From bazel with Apache License 2.0 6 votes vote down vote up
public JUnit4RunnerFactory(
    Supplier<Request> requestSupplier,
    Supplier<CancellableRequestFactory> requestFactorySupplier,
    Supplier<Supplier<TestSuiteModel>> modelSupplierSupplier,
    Supplier<PrintStream> testRunnerOutSupplier,
    Supplier<JUnit4Config> configSupplier,
    Supplier<Set<RunListener>> runListenersSupplier,
    Supplier<Set<JUnit4Runner.Initializer>> initializersSupplier) {
  assert requestSupplier != null;
  this.requestSupplier = requestSupplier;
  assert requestFactorySupplier != null;
  this.requestFactorySupplier = requestFactorySupplier;
  assert modelSupplierSupplier != null;
  this.modelSupplierSupplier = modelSupplierSupplier;
  assert testRunnerOutSupplier != null;
  this.testRunnerOutSupplier = testRunnerOutSupplier;
  assert configSupplier != null;
  this.configSupplier = configSupplier;
  assert runListenersSupplier != null;
  this.runListenersSupplier = runListenersSupplier;
  assert initializersSupplier != null;
  this.initializersSupplier = initializersSupplier;
}
 
Example #6
Source File: JUnit4TestXmlListenerTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test
public void failuresAreReported() throws Exception {
  TestSuiteModelSupplier mockModelSupplier = mock(TestSuiteModelSupplier.class);
  TestSuiteModel mockModel = mock(TestSuiteModel.class);
  CancellableRequestFactory mockRequestFactory = mock(CancellableRequestFactory.class);
  OutputStream mockXmlStream = mock(OutputStream.class);
  JUnit4TestXmlListener listener = new JUnit4TestXmlListener(
      mockModelSupplier, mockRequestFactory, fakeSignalHandlers, mockXmlStream, errPrintStream);

  Request request = Request.classWithoutSuiteMethod(FailingTest.class);
  Description suiteDescription = request.getRunner().getDescription();
  Description testDescription = suiteDescription.getChildren().get(0);

  when(mockModelSupplier.get()).thenReturn(mockModel);

  JUnitCore core = new JUnitCore();
  core.addListener(listener);
  core.run(request);

  assertWithMessage("no output to stderr expected").that(errStream.size()).isEqualTo(0);
  InOrder inOrder = inOrder(mockModel);
  inOrder.verify(mockModel).testFailure(eq(testDescription), any(Throwable.class));
  inOrder.verify(mockModel).writeAsXml(mockXmlStream);
  verify(mockRequestFactory, never()).cancelRun();
}
 
Example #7
Source File: SingleJUnitTestRunner.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * .
 * @param args .
 * @throws ClassNotFoundException .
 */
public static void main(String... args) throws ClassNotFoundException {
    int retCode = 0;
    String resultMessage = "SUCCESS";
    String[] classAndMethod = args[0].split("#");
    Request request = Request.method(Class.forName(classAndMethod[0]),
            classAndMethod[1]);

    Result result = new JUnitCore().run(request);
    if (!result.wasSuccessful()) {
        retCode = 1;
        resultMessage = "FAILURE";
    }
    System.out.println(resultMessage);
    System.exit(retCode);
}
 
Example #8
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
/** Test provided multiple annotations to include. */
@Test
public void testTestSizeFilter_multipleAnnotation() {
  Request request =
      builder
          .addAnnotationInclusionFilter(SmallTest.class.getName())
          .addAnnotationInclusionFilter(FlakyTest.class.getName())
          .addTestClass(SampleRunnerFilterSizeTest.class.getName())
          .addTestClass(SampleMultipleAnnotation.class.getName())
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  // expect 1 test that failed
  Assert.assertEquals(1, result.getRunCount());
  Assert.assertEquals(1, result.getFailureCount());
  Assert.assertEquals(
      "testSmallSkipped", result.getFailures().get(0).getDescription().getMethodName());
}
 
Example #9
Source File: JUnit4TestXmlListenerTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test
public void assumptionViolationsAreReportedAsSkippedTests() throws Exception {
  TestSuiteModelSupplier mockModelSupplier = mock(TestSuiteModelSupplier.class);
  TestSuiteModel mockModel = mock(TestSuiteModel.class);
  CancellableRequestFactory mockRequestFactory = mock(CancellableRequestFactory.class);
  OutputStream mockXmlStream = mock(OutputStream.class);
  JUnit4TestXmlListener listener = new JUnit4TestXmlListener(
      mockModelSupplier, mockRequestFactory, fakeSignalHandlers, mockXmlStream, errPrintStream);

  Request request = Request.classWithoutSuiteMethod(TestWithAssumptionViolation.class);
  Description suiteDescription = request.getRunner().getDescription();
  Description testDescription = suiteDescription.getChildren().get(0);

  when(mockModelSupplier.get()).thenReturn(mockModel);

  JUnitCore core = new JUnitCore();
  core.addListener(listener);
  core.run(request);

  assertWithMessage("no output to stderr expected").that(errStream.size()).isEqualTo(0);
  InOrder inOrder = inOrder(mockModel);
  inOrder.verify(mockModel).testSkipped(testDescription);
  inOrder.verify(mockModel).writeAsXml(mockXmlStream);
  verify(mockRequestFactory, never()).cancelRun();
}
 
Example #10
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
/**
 * Verify that a JUnit 3 suite method that returns a TestCase is not executed when skipExecution =
 * true.
 */
@Test
public void testSkipExecution_JUnit3SuiteMethod_ReturnsTestCase() {
  Request request =
      builder
          .setSkipExecution(true)
          .addTestClass(JUnit3SuiteMethod_ReturnsTestCase.class.getName())
          .build();
  JUnitCore testRunner = new JUnitCore();
  // Differs from standard JUnit behavior; a suite() method can return any implementation of
  // junit.framework.Test not just TestSuite.
  ensureAllTestsFailed(
      testRunner.run(request),
      1,
      JUnit3SuiteMethod_ReturnsTestCase.class.getName() + "#suite() did not return a TestSuite");
}
 
Example #11
Source File: CancellableRequestFactoryTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Test
public void testFailingRun() {
  final AtomicBoolean testRan = new AtomicBoolean(false);
  final RuntimeException expectedFailure = new RuntimeException();

  // A runner that should run its test
  FakeRunner runner = new FakeRunner("shouldRun", new Runnable() {
    @Override
    public void run() {
      testRan.set(true);
      throw expectedFailure;
    }
  });

  Request request = cancellableRequestFactory.createRequest(Request.runner(runner));
  JUnitCore core = new JUnitCore();
  Result result = core.run(request);

  assertThat(testRan.get()).isTrue();
  assertThat(result.getRunCount()).isEqualTo(1);
  assertThat(result.getFailureCount()).isEqualTo(1);
  assertThat(result.getFailures().get(0).getException()).isSameInstanceAs(expectedFailure);
}
 
Example #12
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
/** Test that {@link SdkSuppress} filters tests as appropriate */
@Test
public void testSdkSuppress() throws Exception {
  MockitoAnnotations.initMocks(this);
  TestRequestBuilder b = createBuilder(mockDeviceBuild);
  when(mockDeviceBuild.getSdkVersionInt()).thenReturn(16);
  when(mockDeviceBuild.getCodeName()).thenReturn("REL");
  Request request = b.addTestClass(SampleSdkSuppress.class.getName()).build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);

  Set<String> expected =
      new HashSet<>(Arrays.asList("min15", "min16", "noSdkSuppress", "max19", "min14max16"));
  Assert.assertEquals(expected.size(), result.getRunCount());
  for (Failure f : result.getFailures()) {
    assertTrue(
        "Fail! " + expected + " doesn't contain \"" + f.getMessage() + "\" ",
        expected.contains(f.getMessage()));
  }
}
 
Example #13
Source File: JUnit4RunnerFactory.java    From bazel with Apache License 2.0 6 votes vote down vote up
public static Factory<JUnit4Runner> create(
    Supplier<Request> requestSupplier,
    Supplier<CancellableRequestFactory> requestFactorySupplier,
    Supplier<Supplier<TestSuiteModel>> modelSupplierSupplier,
    Supplier<PrintStream> testRunnerOutSupplier,
    Supplier<JUnit4Config> configSupplier,
    Supplier<Set<RunListener>> runListenersSupplier,
    Supplier<Set<JUnit4Runner.Initializer>> initializersSupplier) {
  return new JUnit4RunnerFactory(
      requestSupplier,
      requestFactorySupplier,
      modelSupplierSupplier,
      testRunnerOutSupplier,
      configSupplier,
      runListenersSupplier,
      initializersSupplier);
}
 
Example #14
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Test @Suppress with JUnit3 tests */
@Test
public void testSuppress_junit3Method() {
  Request request = builder.addTestClass(SampleJUnit3Suppressed.class.getName()).build();
  JUnitCore testRunner = new JUnitCore();
  Result r = testRunner.run(request);
  Assert.assertEquals(2, r.getRunCount());
}
 
Example #15
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Verify that filtering out all tests is not treated as an error */
@Test
public void testNoTests() {
  Request request =
      builder
          .addTestClass(SampleRunnerFilterSizeTest.class.getName())
          .addTestSizeFilter(TestSize.MEDIUM)
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  Assert.assertEquals(0, result.getRunCount());
}
 
Example #16
Source File: JUnitRunner.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public void run() throws Throwable {
  Level stdOutLogLevel = Level.INFO;
  Level stdErrLogLevel = Level.WARNING;

  String unparsedStdOutLogLevel = System.getProperty(STD_OUT_LOG_LEVEL_PROPERTY);
  String unparsedStdErrLogLevel = System.getProperty(STD_ERR_LOG_LEVEL_PROPERTY);

  if (unparsedStdOutLogLevel != null) {
    stdOutLogLevel = Level.parse(unparsedStdOutLogLevel);
  }

  if (unparsedStdErrLogLevel != null) {
    stdErrLogLevel = Level.parse(unparsedStdErrLogLevel);
  }

  for (String className : testClassNames) {
    Class<?> testClass = Class.forName(className);

    List<TestResult> results = new ArrayList<>();
    RecordingFilter filter = new RecordingFilter();
    if (mightBeATestClass(testClass)) {
      JUnitCore jUnitCore = new JUnitCore();
      Runner suite = new Computer().getSuite(createRunnerBuilder(), new Class<?>[] {testClass});
      Request request = Request.runner(suite);
      request = request.filterWith(filter);
      jUnitCore.addListener(new TestListener(results, stdOutLogLevel, stdErrLogLevel));
      jUnitCore.run(request);
    }
    // Combine the results with the tests we filtered out
    List<TestResult> actualResults = combineResults(results, filter.filteredOut);
    writeResult(className, actualResults);
  }
}
 
Example #17
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Verify method filter does not filter out initialization errors */
@Test
public void testJUnit4FilterWithInitError() {
  Request request =
      builder
          .addTestMethod(JUnit4TestInitFailure.class.getName(), "testWillFailOnClassInit")
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  Assert.assertEquals(1, result.getRunCount());
}
 
Example #18
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Runs the test request and gets list of test methods run */
private static ArrayList<String> runRequest(Request request) {
  JUnitCore testRunner = new JUnitCore();
  RecordingRunListener listener = new RecordingRunListener();
  testRunner.addListener(listener);
  testRunner.run(request);
  return listener.methods;
}
 
Example #19
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Test that {@link RequiresDevice} filters tests as appropriate */
@Test
public void testRequiresDevice() {
  MockitoAnnotations.initMocks(this);
  TestRequestBuilder b = createBuilder(mockDeviceBuild);
  when(mockDeviceBuild.getHardware())
      .thenReturn(EMULATOR_HARDWARE_GOLDFISH, EMULATOR_HARDWARE_RANCHU);
  Request request = b.addTestClass(SampleRequiresDevice.class.getName()).build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  Assert.assertEquals(2, result.getRunCount());
}
 
Example #20
Source File: JUnit4TestModelBuilderTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateModel_topLevelIgnore() throws Exception {
  Class<?> testClass = SampleTestCaseWithTopLevelIgnore.class;
  Request request = Request.classWithoutSuiteMethod(testClass);
  String testClassName = testClass.getCanonicalName();
  JUnit4TestModelBuilder modelBuilder =
      builder(request, testClassName, stubShardingEnvironment, null, xmlResultWriter);

  TestSuiteModel testSuiteModel = modelBuilder.get();
  assertThat(testSuiteModel.getNumTestCases()).isEqualTo(0);
}
 
Example #21
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Test that annotation filtering by class works */
@Test
public void testAddAnnotationExclusionFilter() {
  Request request =
      builder
          .addAnnotationExclusionFilter(SmallTest.class.getName())
          .addTestClass(SampleRunnerFilterSizeTest.class.getName())
          .addTestClass(SampleRunnerFilterClassSize.class.getName())
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  Assert.assertEquals(1, result.getRunCount());
}
 
Example #22
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Test size annotations with JUnit3 test methods */
@Test
public void testSize_junit3Method() {
  Request request =
      builder
          .addTestClass(SampleJUnit3Test.class.getName())
          .addTestClass(SampleNoSize.class.getName())
          .addTestSizeFilter(TestSize.SMALL)
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result r = testRunner.run(request);
  Assert.assertEquals(2, r.getRunCount());
}
 
Example #23
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Test that platform size annotation filtering by class works */
@Test
public void testRunnerSize_class() {
  Request request =
      builder
          .addTestClass(SamplePlatformSizeTest.class.getName())
          .addTestClass(SamplePlatformClassSize.class.getName())
          .addTestSizeFilter(TestSize.SMALL)
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  Assert.assertEquals(3, result.getRunCount());
}
 
Example #24
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Test that platform size annotation filtering by class works */
@Test
public void testPlatfromSize_class() {
  Request request =
      builder
          .addTestClass(SampleRunnerFilterSizeTest.class.getName())
          .addTestClass(SampleRunnerFilterClassSize.class.getName())
          .addTestSizeFilter(TestSize.SMALL)
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  Assert.assertEquals(3, result.getRunCount());
}
 
Example #25
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void testAnnotationSizeFilteringWorks() {
  Request request =
      builder
          .addTestClass(SamplePlatformSizeTest.class.getName())
          .addTestSizeFilter(TestSize.SMALL)
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  Assert.assertEquals(1, result.getRunCount());
}
 
Example #26
Source File: JUnitSingleTestResultRunner.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Result call() throws Exception {
	JUnitCore runner = new JUnitCore();
	runner.addListener(listener);
	Request request = Request.method(testClassFromCustomClassLoader(), testCaseName());
	return runner.run(request);
}
 
Example #27
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Verify that a JUnit 4 test class marked with @Ignore is not run when skipExecution = true. */
@Test
public void testSkipExecution_JUnit4Ignored_WithMethodFilter() {
  Request request =
      builder
          .setSkipExecution(true)
          .addTestClass(JUnit4Ignored.class.getName())
          .addTestMethod(JUnit4Ignored.class.getName(), "testBroken")
          .build();
  JUnitCore testRunner = new JUnitCore();
  ensureNoTestsFailed(testRunner.run(request), 0);
}
 
Example #28
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void testBothMethodInclusionAndExclusion() {
  Request request =
      builder
          .addTestClass(SampleTwoTestsClass.class.getName())
          .removeTestMethod(SampleTwoTestsClass.class.getName(), "test1of2")
          .removeTestMethod(SampleThreeTestsClass.class.getName(), "test1of3")
          .addTestMethod(SampleThreeTestsClass.class.getName(), "test3of3")
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  Assert.assertEquals(2, result.getRunCount());
}
 
Example #29
Source File: TimeoutTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void testTimeoutsInJUnit4WithNoRule() {
  Request request =
      builder
          .addTestClass(JUnit4NoRuleClass.class.getName())
          .setPerTestTimeout(JUnit3StyleTimeoutClass.GLOBAL_ARG_TIMEOUT)
          .build();
  JUnitCore junitCore = new JUnitCore();
  Result result = junitCore.run(request);
  assertThat(result.getFailures()).isEmpty();
  assertThat(result.getRunCount()).isEqualTo(2);
}
 
Example #30
Source File: TimeoutTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void testTimeoutsInJUnit4WithRule() {
  Request request =
      builder
          .addTestClass(JUnit4WithRuleClass.class.getName())
          .setPerTestTimeout(JUnit3StyleTimeoutClass.GLOBAL_ARG_TIMEOUT)
          .build();
  JUnitCore junitCore = new JUnitCore();
  Result result = junitCore.run(request);
  assertThat(result.getFailures()).isEmpty();
  assertThat(result.getRunCount()).isEqualTo(2);
}