Skip to content

Commit 30e915c

Browse files
authored
Merge pull request #70 from rubensousa/rule-fix
Fix retry rule in default case
2 parents 54696d6 + 843e97e commit 30e915c

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

carioca-junit4-rules/src/main/kotlin/com/rubensousa/carioca/junit4/rules/RetryTestRule.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,13 @@ class RetryTestRule : TestRule {
7474
return object : Statement() {
7575
override fun evaluate() {
7676
var lastError: Throwable? = null
77-
repeat(times + 1) {
77+
var passed = false
78+
while (!passed && currentExecution < times + 1) {
7879
try {
7980
base.evaluate()
8081
// Clear the error, since the test now passed
8182
lastError = null
82-
return@repeat
83+
passed = true
8384
} catch (error: Throwable) {
8485
lastError = error
8586
}

carioca-junit4-rules/src/test/java/com/rubensousa/carioca/junit4/rules/RetryTestRuleTest.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,34 @@ class RetryTestRuleFailureTest {
128128

129129
}
130130

131+
@RetryTest(times = 10)
132+
class RetryTestRuleSuccessfulTest {
133+
134+
@get:Rule
135+
val retryRule = RetryTestRule()
136+
137+
@Test
138+
fun `test is executed once because it passes`() {
139+
assertThat(retryRule.currentExecution).isEqualTo(0)
140+
SingletonState.iteration++
141+
}
142+
143+
companion object {
144+
145+
@BeforeClass
146+
@JvmStatic
147+
fun before() {
148+
SingletonState.iteration = 0
149+
}
150+
151+
@AfterClass
152+
@JvmStatic
153+
fun after() {
154+
SingletonState.assertIterations(1)
155+
}
156+
}
157+
}
158+
131159
class RetryTestRuleEmptyTest {
132160

133161
@get:Rule

docs/changelog/junit4-rules.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
## Version 1.0.0
44

5+
### 1.0.0-beta01
6+
7+
2024-10-01
8+
9+
#### Bug fixes
10+
11+
- Fixed `RetryTestRule` not finishing up correctly if test passes [#70](https://github.com/rubensousa/Carioca/pull/70)
12+
513
### 1.0.0-alpha02
614

715
2024-09-30

docs/junit4-rules.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This rule can be useful for end-to-end tests that have some degree of tolerable
88

99
Avoid using it for all sorts of tests!
1010

11-
Total executions = `max(1, 1 + times)`
11+
Total executions = `[1, 1 + times]`, depends on which execution the test actually passes
1212

1313
```kotlin linenums="1"
1414
@RetryTest(times = 9)

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extra:
2626
report:
2727
version: '1.0.0-alpha03'
2828
junit4_rules:
29-
version: '1.0.0-alpha02'
29+
version: '1.0.0-beta01'
3030
allure_plugin:
3131
version: '1.0.0-alpha04'
3232
hilt:

0 commit comments

Comments
 (0)