项目作者: interviewstreet

项目描述 :
Utility for Ordered Test Execution
高级语言: Java
项目地址: git://github.com/interviewstreet/junit-ordered-test-runner.git


JUnit Ordered Test Runner

An application to provide support for executing tests in the specific order and generate the customized XML report.

Installation

  • Apache Maven

    1. <dependency>
    2. <groupId>com.hackerrank.applications</groupId>
    3. <artifactId>junit-ordered-test-runner</artifactId>
    4. <version>1.0.2</version>
    5. </dependency>
  • Gradle Groovy DSL

    1. compile 'com.hackerrank.applications:junit-ordered-test-runner:1.0.2'
  • Gradle Kotlin DSL

    1. compile(group = "com.hackerrank.applications", name = "junit-ordered-test-runner", version = "1.0.2")
  • Scala SBT

    1. libraryDependencies += "com.hackerrank.applications" % "junit-ordered-test-runner" % "1.0.2"
  • Apache Ivy

    1. <dependency org="com.hackerrank.applications" name="junit-ordered-test-runner" rev="1.0.2" ></dependency>
  • Groovy Grape

    1. @Grapes(
    2. @Grab(group='com.hackerrank.applications', module='junit-ordered-test-runner', version='1.0.2')
    3. )
  • Apache Builder

    1. 'com.hackerrank.applications:junit-ordered-test-runner:jar:1.0.2'

Sample Usage

  • The OrderedTestRunner should be used to run the test. The order of each test can be set by the @Order annotation. The test with lower order value is run first.
  • Run the tests with TestWatcher rule using @Rule annotation.
  • Register the test class using the registerClass method of TestWatcher in the @BeforeClass setup.
  • Finally, invoke the createReport method of TestWatcher in the @AfterClass setup.

For example,

  1. @RunWith(OrderedTestRunner.class)
  2. public class SampleOrderedTest {
  3. @Rule
  4. public TestWatcher watchman = TestWatchman.watchman;
  5. public SampleOrderedTest() {
  6. }
  7. @BeforeClass
  8. public static void setUpClass() {
  9. TestWatchman.watchman.registerClass(SampleOrderedTest.class);
  10. }
  11. @AfterClass
  12. public static void tearDownClass() {
  13. TestWatchman.watchman.createReport(SampleOrderedTest.class);
  14. }
  15. @Test
  16. @Order(1)
  17. public void firstTest() {
  18. assertTrue(0 == 0);
  19. }
  20. @Test
  21. @Order(2)
  22. public void secondTest() {
  23. assertEquals(1, 1);
  24. }
  25. @Test
  26. @Ordered(3)
  27. public void thirdTest() {
  28. assertNotEquals(1, null);
  29. }
  30. }

Also,

  • If a Runner is already being used to run the tests, then, an inner class can be used to run with OrderedTestRunner. Tests can be triggered using the JUnitCore.runClasses method. In this case, the test is always passing, so optional check can be performed using allTestSucceeded.

For example,

  1. public class SampleOrderedTest {
  2. public SampleOrderedTest() {
  3. }
  4. @BeforeClass
  5. public static void setUpClass() {
  6. TestWatchman.watchman.registerClass(SampleOrderedTest.class);
  7. }
  8. @AfterClass
  9. public static void tearDownClass() {
  10. TestWatchman.watchman.createReport(SampleOrderedTest.class);
  11. }
  12. @Test
  13. public void startTest() {
  14. JUnitCore.runClasses(TestHelper.class);
  15. assertTrue(TestWatchman.watchman.allTestsSucceeded());
  16. }
  17. @RunWith(OrderedTestRunner.class)
  18. public static class TestHelper {
  19. @Rule
  20. public TestWatcher watchman = TestWatchman.watchman;
  21. @Test
  22. @Order(1)
  23. public void firstTest() {
  24. assertTrue(0 == 0);
  25. }
  26. @Test
  27. @Order(3)
  28. public void thirdTest() {
  29. assertNotEquals(1, null);
  30. }
  31. @Test
  32. @Order(2)
  33. public void secondTest() {
  34. assertEquals(1, 1);
  35. }
  36. }
  37. }

And,

  • When some tests are not assigned an order, then, these are run after the tests with an assigned order.

You can refer the given test examples for better understanding of writing tests with OrderedTestRunner.

Report Generation

  • The TestWatcher generates an XML report in the target/hackerrank-report directory. The filename is TEST-{test-class-canonical-name}.xml.
  • When running tests in a suite, suite report, as well as individual test reports, will be generated.

Building Project

  • Use mvn clean build to build the project.
  • Use mvn clean test to run the tests.
  • Use mvn clean test -Dtest=TestSuite to run the suite.