@@ -21,6 +21,13 @@ import spock.lang.Issue
2121import javax.annotation.Nullable
2222import java.util.regex.Pattern
2323
24+ import static org.gradle.testretry.testframework.BaseTestNGFuncTest.TestNGLifecycleType.AFTER_CLASS
25+ import static org.gradle.testretry.testframework.BaseTestNGFuncTest.TestNGLifecycleType.AFTER_METHOD
26+ import static org.gradle.testretry.testframework.BaseTestNGFuncTest.TestNGLifecycleType.AFTER_TEST
27+ import static org.gradle.testretry.testframework.BaseTestNGFuncTest.TestNGLifecycleType.BEFORE_CLASS
28+ import static org.gradle.testretry.testframework.BaseTestNGFuncTest.TestNGLifecycleType.BEFORE_METHOD
29+ import static org.gradle.testretry.testframework.BaseTestNGFuncTest.TestNGLifecycleType.BEFORE_TEST
30+
2431abstract class BaseTestNGFuncTest extends AbstractFrameworkFuncTest {
2532 @Override
2633 String getLanguagePlugin () {
@@ -36,13 +43,30 @@ abstract class BaseTestNGFuncTest extends AbstractFrameworkFuncTest {
3643 """
3744 }
3845
39- abstract String reportedLifecycleMethodName (String methodName )
46+ enum TestNGLifecycleType {
47+ BEFORE_SUITE (' BeforeSuite' ),
48+ BEFORE_TEST (' BeforeTest' ),
49+ BEFORE_CLASS (' BeforeClass' ),
50+ BEFORE_METHOD (' BeforeMethod' ),
51+ AFTER_METHOD (' AfterMethod' ),
52+ AFTER_CLASS (' AfterClass' ),
53+ AFTER_TEST (' AfterTest' ),
54+ AFTER_SUITE (' AfterSuite' )
55+
56+ final String annotation
57+
58+ TestNGLifecycleType (String annotation ) {
59+ this . annotation = annotation
60+ }
61+ }
4062
41- abstract String reportedParameterizedMethodName (String methodName , String paramType , int invocationNumber , @Nullable String paramValueRepresentation )
63+ abstract String reportedLifecycleMethodName (String gradleVersion , TestNGLifecycleType lifecycleType , String methodName )
4264
43- abstract boolean reportsSuccessfulLifecycleExecutions (String lifecycleMethodType )
65+ abstract String reportedParameterizedMethodName (String gradleVersion , String methodName , String paramType , int invocationNumber , @Nullable String paramValueRepresentation )
4466
45- def " handles failure in #lifecycle (gradle version #gradleVersion)" () {
67+ abstract boolean reportsSuccessfulLifecycleExecutions (TestNGLifecycleType lifecycleType )
68+
69+ def " handles failure in #lifecycle (gradle version #gradleVersion)" (String gradleVersion, TestNGLifecycleType lifecycle) {
4670 given :
4771 buildFile << """
4872 test.retry.maxRetries = 1
@@ -52,8 +76,8 @@ abstract class BaseTestNGFuncTest extends AbstractFrameworkFuncTest {
5276 package acme;
5377
5478 public class SuccessfulTests {
55- @org.testng.annotations.${ lifecycle}
56- public ${ lifecycle.contains('Class') ? 'static ' : ''} void lifecycle() {
79+ @org.testng.annotations.${ lifecycle.annotation }
80+ public ${ lifecycle.annotation. contains('Class') ? 'static ' : ''} void lifecycle() {
5781 ${ flakyAssert()}
5882 }
5983
@@ -67,20 +91,20 @@ abstract class BaseTestNGFuncTest extends AbstractFrameworkFuncTest {
6791
6892 then :
6993 with(result. output) {
70- it. count(" ${ reportedLifecycleMethodName('lifecycle')} FAILED" ) == 1
71- it. count(" ${ reportedLifecycleMethodName('lifecycle')} PASSED" ) == (reportsSuccessfulLifecycleExecutions(lifecycle) ? 1 : 0 )
94+ it. count(" ${ reportedLifecycleMethodName(gradleVersion, lifecycle, 'lifecycle')} FAILED" ) == 1
95+ it. count(" ${ reportedLifecycleMethodName(gradleVersion, lifecycle, 'lifecycle')} PASSED" ) == (reportsSuccessfulLifecycleExecutions(lifecycle) ? 1 : 0 )
7296 ! it. contains(" The following test methods could not be retried" )
7397 }
7498
7599 where :
76100 [gradleVersion, lifecycle] << GroovyCollections . combinations((Iterable ) [
77101 GRADLE_VERSIONS_UNDER_TEST ,
78- [' BeforeTest ' , ' BeforeClass ' , ' BeforeMethod ' , ' AfterMethod ' , ' AfterClass ' , ' AfterTest ' ]
102+ [BEFORE_TEST , BEFORE_CLASS , BEFORE_METHOD , AFTER_METHOD , AFTER_CLASS , AFTER_TEST ]
79103 ])
80104 // Note: we don't handle BeforeSuite AfterSuite
81105 }
82106
83- def " correctly reports exhausted retries on failures in #lifecycle (gradle version #gradleVersion)" () {
107+ def " correctly reports exhausted retries on failures in #lifecycle (gradle version #gradleVersion)" (String gradleVersion, TestNGLifecycleType lifecycle ) {
84108 given :
85109 buildFile << """
86110 test.retry.maxRetries = 1
@@ -90,8 +114,8 @@ abstract class BaseTestNGFuncTest extends AbstractFrameworkFuncTest {
90114 package acme;
91115
92116 public class AlwaysFailingLifecycle {
93- @org.testng.annotations.${ lifecycle}
94- public ${ lifecycle.contains('Class') ? 'static ' : ''} void lifecycle() {
117+ @org.testng.annotations.${ lifecycle.annotation }
118+ public ${ lifecycle.annotation. contains('Class') ? 'static ' : ''} void lifecycle() {
95119 throw new RuntimeException("Lifecycle goes boom!");
96120 }
97121
@@ -106,52 +130,19 @@ abstract class BaseTestNGFuncTest extends AbstractFrameworkFuncTest {
106130 then :
107131 with(result. output) {
108132 // if BeforeTest fails, then methods won't be executed
109- it. count(' successTest SKIPPED' ) == (lifecycle. contains(' Before' ) ? 2 : 0 )
110- it. count(' successTest PASSED' ) == (lifecycle. contains(' Before' ) ? 0 : 2 )
111- it. count(" ${ reportedLifecycleMethodName('lifecycle')} FAILED" ) == 2
133+ it. count(' successTest SKIPPED' ) == (lifecycle. annotation . contains(' Before' ) ? 2 : 0 )
134+ it. count(' successTest PASSED' ) == (lifecycle. annotation . contains(' Before' ) ? 0 : 2 )
135+ it. count(" ${ reportedLifecycleMethodName(gradleVersion, lifecycle, 'lifecycle')} FAILED" ) == 2
112136 ! it. contains(" The following test methods could not be retried" )
113137 }
114138
115139 where :
116140 [gradleVersion, lifecycle] << GroovyCollections . combinations((Iterable ) [
117141 GRADLE_VERSIONS_UNDER_TEST ,
118- [' BeforeTest ' , ' BeforeClass ' , ' BeforeMethod ' , ' AfterMethod ' , ' AfterClass ' , ' AfterTest ' ]
142+ [BEFORE_TEST , BEFORE_CLASS , BEFORE_METHOD , AFTER_METHOD , AFTER_CLASS , AFTER_TEST ]
119143 ])
120144 }
121145
122- def "does not handle flaky static initializers (gradle version #gradleVersion )" () {
123- given:
124- buildFile << " " "
125- test. retry. maxRetries = 1
126- """
127-
128- writeJavaTestSource """
129- package acme;
130-
131- public class SomeTests {
132-
133- static {
134- ${flakyAssert()}
135- }
136-
137- @org.testng.annotations.Test
138- public void someTest () {}
139- }
140- """
141-
142- when:
143- def result = gradleRunner(gradleVersion as String).buildAndFail()
144-
145- then:
146- with(result.output) {
147- it.contains('There were failing tests. See the report')
148- !it.contains('The following test methods could not be retried')
149- }
150-
151- where:
152- gradleVersion << GRADLE_VERSIONS_UNDER_TEST
153- }
154-
155146 def "handles parameterized test in super class (gradle version #gradleVersion )" () {
156147 given:
157148 buildFile << " " "
@@ -191,8 +182,8 @@ abstract class BaseTestNGFuncTest extends AbstractFrameworkFuncTest {
191182 then:
192183 // we can't rerun just the failed parameter
193184 with(result.output) {
194- it.count("${ reportedParameterizedMethodName('test', 'int', 0, '0')} PASSED") == 2
195- it.count("${ reportedParameterizedMethodName('test', 'int', 1, '1')} FAILED") == 2
185+ it.count("${ reportedParameterizedMethodName(gradleVersion, 'test', 'int', 0, '0')} PASSED") == 2
186+ it.count("${ reportedParameterizedMethodName(gradleVersion, 'test', 'int', 1, '1')} FAILED") == 2
196187 }
197188
198189 where:
@@ -316,8 +307,8 @@ abstract class BaseTestNGFuncTest extends AbstractFrameworkFuncTest {
316307 then:
317308 // we can't rerun just the failed parameter
318309 with(result.output) {
319- it.count("${ reportedParameterizedMethodName('test', 'int', 0, '0')} PASSED") == 2
320- it.count("${ reportedParameterizedMethodName('test', 'int', 1, '1')} FAILED") == 2
310+ it.count("${ reportedParameterizedMethodName(gradleVersion, 'test', 'int', 0, '0')} PASSED") == 2
311+ it.count("${ reportedParameterizedMethodName(gradleVersion, 'test', 'int', 1, '1')} FAILED") == 2
321312 }
322313
323314 where:
@@ -369,8 +360,8 @@ abstract class BaseTestNGFuncTest extends AbstractFrameworkFuncTest {
369360 then:
370361 // we can't rerun just the failed parameter
371362 with(result.output.readLines()) {
372- it.findAll { line -> line.matches(/.*${ Pattern.quote(reportedParameterizedMethodName('test', 'acme.ParameterTest$Foo', 0, ''))} .* PASSED/) }.size() == 2
373- it.findAll { line -> line.matches(/.*${ Pattern.quote(reportedParameterizedMethodName('test', 'acme.ParameterTest$Foo', 1, ''))} .* FAILED/) }.size() == 2
363+ it.findAll { line -> line.matches(/.*${ Pattern.quote(reportedParameterizedMethodName(gradleVersion, 'test', 'acme.ParameterTest$Foo', 0, ''))} .* PASSED/) }.size() == 2
364+ it.findAll { line -> line.matches(/.*${ Pattern.quote(reportedParameterizedMethodName(gradleVersion, 'test', 'acme.ParameterTest$Foo', 1, ''))} .* FAILED/) }.size() == 2
374365 }
375366
376367 where:
0 commit comments