Skip to content

Commit 6e8d3e8

Browse files
authored
Release/2.5.1 (#98)
* 2.5.2
1 parent 474e8ee commit 6e8d3e8

File tree

6 files changed

+99
-30
lines changed

6 files changed

+99
-30
lines changed

composeApp/src/commonTest/kotlin/UltronTestFlowTest.kt

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
21
import com.atiurin.ultron.annotations.ExperimentalUltronApi
32
import com.atiurin.ultron.core.test.UltronTest
43
import com.atiurin.ultron.log.UltronLog
54
import kotlin.test.Test
5+
import kotlin.test.assertEquals
66
import kotlin.test.assertTrue
77

88
class UltronTestFlowTest : UltronTest() {
99
companion object {
1010
var order = 0
11-
var beforeAllTestCounter = -1
11+
var beforeFirstTestCounter = 0
1212
var commonBeforeOrder = -1
1313
var commonAfterOrder = -1
1414
var afterOrder = -1
15-
1615
}
1716

1817
@OptIn(ExperimentalUltronApi::class)
1918
override val beforeFirstTest = {
20-
beforeAllTestCounter = order
19+
beforeFirstTestCounter++
2120
UltronLog.info("Before Class")
2221
}
2322

@@ -39,6 +38,7 @@ class UltronTestFlowTest : UltronTest() {
3938
var goOrder = -1
4039
order++
4140
before {
41+
assertTrue(beforeFirstTestCounter == 1, message = "beforeFirstTest block should run before all test")
4242
beforeOrder = order
4343
order++
4444
UltronLog.info("Before TestMethod 1")
@@ -49,8 +49,6 @@ class UltronTestFlowTest : UltronTest() {
4949
}.after {
5050
afterOrder = order
5151
order++
52-
assertTrue(beforeAllTestCounter == 0, message = "beforeAllTests block should run before all test")
53-
assertTrue(beforeAllTestCounter < commonBeforeOrder, message = "beforeAllTests block should run before commonBefore block")
5452
assertTrue(commonBeforeOrder < beforeOrder, message = "beforeOrder block should run after commonBefore block")
5553
assertTrue(beforeOrder < goOrder, message = "Before block should run before 'go'")
5654
assertTrue(goOrder < afterOrder, message = "After block should run after 'go'")
@@ -64,14 +62,48 @@ class UltronTestFlowTest : UltronTest() {
6462
}.after {
6563
UltronLog.info("After TestMethod 2")
6664
}.go {
67-
assertTrue(beforeAllTestCounter == 0, message = "beforeAllTests block should run only once")
65+
assertTrue(beforeFirstTestCounter == 1, message = "beforeFirstTest block should run only once")
6866
UltronLog.info("Run TestMethod 2")
6967
}
7068
}
7169

7270
@Test
7371
fun simpleTest() = test {
74-
assertTrue(beforeAllTestCounter == 0, message = "beforeAllTests block should run only once")
72+
assertTrue(beforeFirstTestCounter == 1, message = "beforeFirstTest block should run only once")
7573
UltronLog.info("UltronTest simpleTest")
7674
}
75+
76+
77+
@Test
78+
fun afterBlockExecutedOnFailedTest() {
79+
var isAfterExecuted = false
80+
runCatching {
81+
test {
82+
go {
83+
throw RuntimeException("test exception")
84+
}
85+
after {
86+
isAfterExecuted = true
87+
}
88+
}
89+
}
90+
assertTrue(isAfterExecuted)
91+
}
92+
93+
@Test
94+
fun testExceptionMessageThrownOnFailedTest() {
95+
val testExceptionMessage = "test exception"
96+
runCatching {
97+
test {
98+
go {
99+
throw RuntimeException(testExceptionMessage)
100+
}
101+
after {
102+
throw RuntimeException("Another after exception")
103+
}
104+
}
105+
}.onFailure { ex ->
106+
assertEquals(ex.message, testExceptionMessage)
107+
}
108+
}
77109
}

composeApp/src/commonTest/kotlin/UltronTestFlowTest2.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ import com.atiurin.ultron.annotations.ExperimentalUltronApi
22
import com.atiurin.ultron.core.test.UltronTest
33
import com.atiurin.ultron.log.UltronLog
44
import kotlin.test.Test
5+
import kotlin.test.assertEquals
56
import kotlin.test.assertTrue
67

78
class UltronTestFlowTest2 : UltronTest() {
8-
var order = 0
9-
var beforeAllTestCounter = 0
9+
companion object {
10+
var order = 0
11+
var beforeFirstTestCounter = 0
12+
}
1013

1114
@OptIn(ExperimentalUltronApi::class)
1215
override val beforeFirstTest = {
13-
beforeAllTestCounter = order
16+
beforeFirstTestCounter++
1417
order++
1518
UltronLog.info("Before Class")
1619
}
@@ -22,6 +25,7 @@ class UltronTestFlowTest2 : UltronTest() {
2225
var goOrder = -1
2326
order++
2427
before {
28+
assertEquals(1, beforeFirstTestCounter, message = "beforeFirstTest block should run before all test")
2529
beforeOrder = order
2630
order++
2731
UltronLog.info("Before TestMethod 1")
@@ -31,8 +35,6 @@ class UltronTestFlowTest2 : UltronTest() {
3135
UltronLog.info("Run TestMethod 1")
3236
}.after {
3337
afterOrder = order
34-
assertTrue(beforeAllTestCounter == 0, message = "beforeAllTests block should run before all test")
35-
assertTrue(beforeOrder > beforeAllTestCounter, message = "Before block should run after 'Before All'")
3638
assertTrue(beforeOrder < goOrder, message = "Before block should run before 'go'")
3739
assertTrue(goOrder < afterOrder, message = "After block should run after 'go'")
3840
}
@@ -45,7 +47,7 @@ class UltronTestFlowTest2 : UltronTest() {
4547
}.after {
4648
UltronLog.info("After TestMethod 2")
4749
}.go {
48-
assertTrue(beforeAllTestCounter == 0, message = "beforeAllTests block should run only once")
50+
assertEquals(1, beforeFirstTestCounter, message = "beforeFirstTest block should run before all test")
4951
UltronLog.info("Run TestMethod 2")
5052
}
5153
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ kotlin.mpp.enableCInteropCommonization=true
1414

1515
GROUP=com.atiurin
1616
POM_ARTIFACT_ID=ultron
17-
VERSION_NAME=2.5.0-alpha16
17+
VERSION_NAME=2.5.2

sample-app/src/androidTest/java/com/atiurin/sampleapp/tests/testlifecycle/UltronTestFlowTest.kt

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package com.atiurin.sampleapp.tests.testlifecycle
33
import com.atiurin.sampleapp.tests.BaseTest
44
import com.atiurin.ultron.annotations.ExperimentalUltronApi
55
import com.atiurin.ultron.log.UltronLog
6+
import org.junit.Assert
67
import org.junit.Test
78

89
class UltronTestFlowTest : BaseTest() {
910
companion object {
1011
var order = 0
11-
var beforeAllTestCounter = -1
12+
var isBeforeFirstTestCounter = 0
1213
var commonBeforeOrder = -1
1314
var commonAfterOrder = -1
1415
var afterOrder = -1
@@ -17,7 +18,7 @@ class UltronTestFlowTest : BaseTest() {
1718

1819
@OptIn(ExperimentalUltronApi::class)
1920
override val beforeFirstTest = {
20-
beforeAllTestCounter = order
21+
isBeforeFirstTestCounter++
2122
UltronLog.info("Before Class")
2223
}
2324

@@ -39,6 +40,7 @@ class UltronTestFlowTest : BaseTest() {
3940
var goOrder = -1
4041
order++
4142
before {
43+
assert(isBeforeFirstTestCounter == 1, lazyMessage = { "beforeFirstTest block should run before commonBefore block" })
4244
beforeOrder = order
4345
order++
4446
UltronLog.info("Before TestMethod 1")
@@ -49,8 +51,6 @@ class UltronTestFlowTest : BaseTest() {
4951
}.after {
5052
afterOrder = order
5153
order++
52-
assert(beforeAllTestCounter == 0, lazyMessage = { "beforeAllTests block should run before all test" })
53-
assert(beforeAllTestCounter < commonBeforeOrder, lazyMessage = { "beforeAllTests block should run before commonBefore block" })
5454
assert(commonBeforeOrder < beforeOrder, lazyMessage = { "beforeOrder block should run after commonBefore block" })
5555
assert(beforeOrder < goOrder, lazyMessage = { "Before block should run before 'go'" })
5656
assert(goOrder < afterOrder, lazyMessage = { "After block should run after 'go'" })
@@ -61,17 +61,35 @@ class UltronTestFlowTest : BaseTest() {
6161
fun someTest2() = test(suppressCommonBefore = true) {
6262
before {
6363
UltronLog.info("Before TestMethod 2")
64-
}.after {
64+
}
65+
after {
6566
UltronLog.info("After TestMethod 2")
66-
}.go {
67-
assert(beforeAllTestCounter == 0, lazyMessage = { "beforeAllTests block should run only once" })
67+
}
68+
go {
69+
assert(isBeforeFirstTestCounter == 1, lazyMessage = { "beforeFirstTest block should run only once" })
6870
UltronLog.info("Run TestMethod 2")
6971
}
7072
}
7173

7274
@Test
7375
fun simpleTest() = test {
74-
assert(beforeAllTestCounter == 0, lazyMessage = { "beforeAllTests block should run only once" })
76+
assert(isBeforeFirstTestCounter == 1, lazyMessage = { "beforeAllTests block should run only once" })
7577
UltronLog.info("UltronTest simpleTest")
7678
}
79+
80+
@Test
81+
fun afterBlockExecutedOnFailedTest() {
82+
var isAfterExecuted = false
83+
runCatching {
84+
test {
85+
go{
86+
throw RuntimeException("test exception")
87+
}
88+
after {
89+
isAfterExecuted = true
90+
}
91+
}
92+
}
93+
Assert.assertTrue(isAfterExecuted)
94+
}
7795
}

ultron-common/src/commonMain/kotlin/com/atiurin/ultron/core/test/TestMethod.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@ class TestMethod(testContext: UltronTestContext) {
1313
private var test: TestMethod.() -> Unit = {}
1414

1515
internal fun attack() {
16+
var throwable: Throwable? = null
1617
beforeTest()
17-
test()
18-
afterTest()
18+
runCatching(test).onFailure { ex ->
19+
throwable = ex
20+
}
21+
runCatching(afterTest).onFailure { ex ->
22+
throwable?.let { throw it }
23+
throw ex
24+
}
25+
throwable?.let { throw it }
1926
}
2027

2128
fun before(block: TestMethod.() -> Unit) = apply {

ultron-common/src/commonMain/kotlin/com/atiurin/ultron/core/test/UltronTest.kt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ open class UltronTest(
2626
*
2727
* @throws UltronException if the test class is anonymous.
2828
*/
29-
private val className = this::class.qualifiedName
29+
private val className = this::class.simpleName
3030
?: throw UltronException("Don't use anonymous class for UltronTest")
3131

3232
/**
@@ -72,15 +72,25 @@ open class UltronTest(
7272
if (!suppressCommonBefore) {
7373
beforeTest()
7474
}
75-
75+
var throwable: Throwable? = null
7676
// Configure and execute the test block
77-
configureTestBlock()
78-
attack()
77+
runCatching {
78+
configureTestBlock()
79+
attack()
80+
}.onFailure { ex ->
81+
throwable = ex
82+
}
7983

8084
// Execute common `afterTest` logic if not suppressed
8185
if (!suppressCommonAfter) {
82-
afterTest()
86+
runCatching(afterTest).onFailure { ex ->
87+
throwable?.let { throw it }
88+
throw ex
89+
}
8390
}
91+
throwable?.let { throw it }
8492
}
8593
}
94+
95+
8696
}

0 commit comments

Comments
 (0)