项目作者: artsok

项目描述 :
Extension for Junit 5. Re-run failed JUnit tests immediately.
高级语言: Java
项目地址: git://github.com/artsok/rerunner-jupiter.git
创建时间: 2017-09-15T12:49:52Z
项目社区:https://github.com/artsok/rerunner-jupiter

开源协议:Other

下载


rerunner-jupiter

Maven Central
Build Status
badge-jdk-8
License badge

rerunner-jupiter is a extension for Junit 5.
Re-run failed JUnit-Jupiter tests immediately. Very useful when your UI/API tests don’t stable.
This library is open source, released under the terms of [Apache 2.0 License].

How To Use

In order to include rerunner-jupiter in a Maven project, first add the following dependency to your pom.xml (Java 8 required):

  1. <dependency>
  2. <groupId>io.github.artsok</groupId>
  3. <artifactId>rerunner-jupiter</artifactId>
  4. <version>2.1.6</version>
  5. <scope>test</scope>
  6. </dependency>

Examples

  1. /**
  2. * Repeated three times if test failed.
  3. * By default Exception.class will be handled in test
  4. */
  5. @RepeatedIfExceptionsTest(repeats = 3)
  6. void reRunTest() throws IOException {
  7. throw new IOException("Error in Test");
  8. }
  9. /**
  10. * Repeated two times if test failed. Set IOException.class that will be handled in test
  11. * @throws IOException - error occurred
  12. */
  13. @RepeatedIfExceptionsTest(repeats = 2, exceptions = IOException.class)
  14. void reRunTest2() throws IOException {
  15. throw new IOException("Exception in I/O operation");
  16. }
  17. /**
  18. * Repeated ten times if test failed. Set IOException.class that will be handled in test
  19. * Set formatter for test. Like behavior as at {@link org.junit.jupiter.api.RepeatedTest}
  20. * @throws IOException - error occurred
  21. */
  22. @RepeatedIfExceptionsTest(repeats = 10, exceptions = IOException.class,
  23. name = "Rerun failed test. Attempt {currentRepetition} of {totalRepetitions}")
  24. void reRunTest3() throws IOException {
  25. throw new IOException("Exception in I/O operation");
  26. }
  27. /**
  28. * Repeated three times if selenium test failed. Use selenium-jupiter extension.
  29. */
  30. @RepeatedIfExceptionsTest(repeats = 3, exceptions = NoSuchElementException.class)
  31. void testWithChrome(ChromeDriver chrome) {
  32. chrome.get("http://yandex.ru");
  33. chrome.findElement(By.xpath("//span[@id='authors']"));
  34. }
  35. /**
  36. * Repeated 100 times with minimum success four times, then disabled all remaining repeats.
  37. * See image below how it works. Default exception is Exception.class
  38. */
  39. @DisplayName("Test Case Name")
  40. @RepeatedIfExceptionsTest(repeats = 100, minSuccess = 4)
  41. void reRunTest4() {
  42. if(random.nextInt() % 2 == 0) {
  43. throw new RuntimeException("Error in Test");
  44. }
  45. }
  46. /**
  47. * By default total repeats = 1 and minimum success = 1.
  48. * If the test failed by this way start to repeat it by one time with one minimum success.
  49. *
  50. * This example without exceptions.
  51. */
  52. @ParameterizedRepeatedIfExceptionsTest
  53. @ValueSource(ints = {14, 15, 100, -10})
  54. void successfulParameterizedTest(int argument) {
  55. System.out.println(argument);
  56. }
  57. /**
  58. * By default total repeats = 1 and minimum success = 1.
  59. * If the test failed by this way start to repeat it by one time with one minimum success.
  60. * This example with display name but without exceptions
  61. */
  62. @DisplayName("Example of parameterized repeated without exception")
  63. @ParameterizedRepeatedIfExceptionsTest
  64. @ValueSource(ints = {1, 2, 3, 1001})
  65. void successfulParameterizedTestWithDisplayName(int argument) {
  66. System.out.println(argument);
  67. }
  68. /**
  69. * By default total repeats = 1 and minimum success = 1.
  70. * If the test failed by this way start to repeat it by one time with one minimum success.
  71. *
  72. * This example with display name but with exception. Exception depends on random number generation.
  73. */
  74. @DisplayName("Example of parameterized repeated with exception")
  75. @ParameterizedRepeatedIfExceptionsTest
  76. @ValueSource(strings = {"Hi", "Hello", "Bonjour", "Privet"})
  77. void errorParameterizedTestWithDisplayName(String argument) {
  78. if (random.nextInt() % 2 == 0) {
  79. throw new RuntimeException("Exception " + argument);
  80. }
  81. }
  82. /**
  83. * By default total repeats = 1 and minimum success = 1.
  84. * If the test failed by this way start to repeat it by one time with one minimum success.
  85. *
  86. * This example with display name, repeated display name, 10 repeats and 2 minimum success with exceptions.
  87. * Exception depends on random number generation.
  88. */
  89. @ParameterizedRepeatedIfExceptionsTest(name = "Argument was {0}",
  90. repeatedName = " (Repeat {currentRepetition} of {totalRepetitions})",
  91. repeats = 10, exceptions = RuntimeException.class, minSuccess = 2)
  92. @ValueSource(ints = {4, 5, 6, 7})
  93. void errorParameterizedTestWithDisplayNameAndRepeatedName(int argument) {
  94. if (random.nextInt() % 2 == 0) {
  95. throw new RuntimeException("Exception in Test " + argument);
  96. }
  97. }
  98. /**
  99. * By default total repeats = 1 and minimum success = 1.
  100. * If the test failed by this way start to repeat it by one time with one minimum success.
  101. *
  102. * This example with display name, implicitly repeated display name, 4 repeats and 2 minimum success with exceptions.
  103. * Exception depends on random number generation. Also use {@link MethodSource}
  104. */
  105. @DisplayName("Display name of container")
  106. @ParameterizedRepeatedIfExceptionsTest(name = "Year {0} is a leap year.",
  107. repeats = 4, exceptions = RuntimeException.class, minSuccess = 2)
  108. @MethodSource("stringIntAndListProvider")
  109. void errorParameterizedTestWithMultiArgMethodSource(String str, int num, List<String> list) {
  110. assertEquals(5, str.length());
  111. assertTrue(num >= 1 && num <= 2);
  112. assertEquals(2, list.size());
  113. if (random.nextInt() % 2 == 0) {
  114. throw new RuntimeException("Exception in Test");
  115. }
  116. }
  117. static Stream<Arguments> stringIntAndListProvider() {
  118. return Stream.of(
  119. arguments("apple", 1, Arrays.asList("a", "b")),
  120. arguments("lemon", 2, Arrays.asList("x", "y"))
  121. );
  122. }
  123. /**
  124. * it's often caused by some infrastructure problems: network congestion, garbage collection etc.
  125. * These problems usually pass after some time. Use suspend option
  126. */
  127. @RepeatedIfExceptionsTest(repeats = 3, exceptions = IOException.class, suspend = 5000L)
  128. void reRunTestWithSuspendOption() throws IOException {
  129. throw new IOException("Exception in I/O operation");
  130. }
  131. /**
  132. * Example with suspend option for Parameterized Test
  133. * It matters, when you get some infrastructure problems and you want to run your tests through timeout.
  134. *
  135. * Set break to 5 seconds. If exception appeared for any arguments, repeating extension would runs tests with break.
  136. * If one result failed and other passed, does not matter we would wait 5 seconds throught each arguments of the repeated tests.
  137. *
  138. */
  139. @DisplayName("Example of parameterized repeated with exception")
  140. @ParameterizedRepeatedIfExceptionsTest(suspend = 5000L, minSuccess = 2, repeats = 3)
  141. @ValueSource(strings = {"Hi", "Hello", "Bonjour", "Privet"})
  142. void errorParameterizedTestWithSuspendOption(String argument) {
  143. if (random.nextInt() % 2 == 0) {
  144. throw new RuntimeException(argument);
  145. }
  146. }
  147. /**
  148. * Parameterized Test with the wrong exception.
  149. * Test throws AssertionError.class, but we wait for Exception.class.
  150. * In this case test with argument "1" runs ones without repeats.
  151. *
  152. * If you change 'exceptions = AssertionError.class', repeats will appear.
  153. */
  154. @ValueSource(ints = {1, 2})
  155. @ParameterizedRepeatedIfExceptionsTest(repeats = 2, exceptions = Exception.class)
  156. void testParameterizedRepeaterAssertionsFailure(int value) {
  157. assertThat(value, equalTo(2));
  158. }

More examples you can find here.

GitHub Star

Push to the star if you like this JUnit 5 Extension. By this way, I will get feedback from you!