From 4892d2d23c73a27ca63f98f43453889598a37583 Mon Sep 17 00:00:00 2001 From: Aleksei Tiurin Date: Sat, 30 Nov 2024 19:34:40 +0200 Subject: [PATCH 1/3] 2.5.1 --- .../src/commonTest/kotlin/UltronTestFlowTest.kt | 12 +++++------- .../src/commonTest/kotlin/UltronTestFlowTest2.kt | 14 ++++++++------ gradle.properties | 2 +- .../tests/testlifecycle/UltronTestFlowTest.kt | 11 +++++------ .../com/atiurin/ultron/core/test/UltronTest.kt | 2 +- 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/composeApp/src/commonTest/kotlin/UltronTestFlowTest.kt b/composeApp/src/commonTest/kotlin/UltronTestFlowTest.kt index bba2037..e66f919 100644 --- a/composeApp/src/commonTest/kotlin/UltronTestFlowTest.kt +++ b/composeApp/src/commonTest/kotlin/UltronTestFlowTest.kt @@ -8,16 +8,15 @@ import kotlin.test.assertTrue class UltronTestFlowTest : UltronTest() { companion object { var order = 0 - var beforeAllTestCounter = -1 + var beforeFirstTestCounter = 0 var commonBeforeOrder = -1 var commonAfterOrder = -1 var afterOrder = -1 - } @OptIn(ExperimentalUltronApi::class) override val beforeFirstTest = { - beforeAllTestCounter = order + beforeFirstTestCounter++ UltronLog.info("Before Class") } @@ -39,6 +38,7 @@ class UltronTestFlowTest : UltronTest() { var goOrder = -1 order++ before { + assertTrue(beforeFirstTestCounter == 1, message = "beforeFirstTest block should run before all test") beforeOrder = order order++ UltronLog.info("Before TestMethod 1") @@ -49,8 +49,6 @@ class UltronTestFlowTest : UltronTest() { }.after { afterOrder = order order++ - assertTrue(beforeAllTestCounter == 0, message = "beforeAllTests block should run before all test") - assertTrue(beforeAllTestCounter < commonBeforeOrder, message = "beforeAllTests block should run before commonBefore block") assertTrue(commonBeforeOrder < beforeOrder, message = "beforeOrder block should run after commonBefore block") assertTrue(beforeOrder < goOrder, message = "Before block should run before 'go'") assertTrue(goOrder < afterOrder, message = "After block should run after 'go'") @@ -64,14 +62,14 @@ class UltronTestFlowTest : UltronTest() { }.after { UltronLog.info("After TestMethod 2") }.go { - assertTrue(beforeAllTestCounter == 0, message = "beforeAllTests block should run only once") + assertTrue(beforeFirstTestCounter == 1, message = "beforeFirstTest block should run only once") UltronLog.info("Run TestMethod 2") } } @Test fun simpleTest() = test { - assertTrue(beforeAllTestCounter == 0, message = "beforeAllTests block should run only once") + assertTrue(beforeFirstTestCounter == 1, message = "beforeFirstTest block should run only once") UltronLog.info("UltronTest simpleTest") } } \ No newline at end of file diff --git a/composeApp/src/commonTest/kotlin/UltronTestFlowTest2.kt b/composeApp/src/commonTest/kotlin/UltronTestFlowTest2.kt index 36afd43..6a03df5 100644 --- a/composeApp/src/commonTest/kotlin/UltronTestFlowTest2.kt +++ b/composeApp/src/commonTest/kotlin/UltronTestFlowTest2.kt @@ -2,15 +2,18 @@ import com.atiurin.ultron.annotations.ExperimentalUltronApi import com.atiurin.ultron.core.test.UltronTest import com.atiurin.ultron.log.UltronLog import kotlin.test.Test +import kotlin.test.assertEquals import kotlin.test.assertTrue class UltronTestFlowTest2 : UltronTest() { - var order = 0 - var beforeAllTestCounter = 0 + companion object { + var order = 0 + var beforeFirstTestCounter = 0 + } @OptIn(ExperimentalUltronApi::class) override val beforeFirstTest = { - beforeAllTestCounter = order + beforeFirstTestCounter++ order++ UltronLog.info("Before Class") } @@ -22,6 +25,7 @@ class UltronTestFlowTest2 : UltronTest() { var goOrder = -1 order++ before { + assertEquals(1, beforeFirstTestCounter, message = "beforeFirstTest block should run before all test") beforeOrder = order order++ UltronLog.info("Before TestMethod 1") @@ -31,8 +35,6 @@ class UltronTestFlowTest2 : UltronTest() { UltronLog.info("Run TestMethod 1") }.after { afterOrder = order - assertTrue(beforeAllTestCounter == 0, message = "beforeAllTests block should run before all test") - assertTrue(beforeOrder > beforeAllTestCounter, message = "Before block should run after 'Before All'") assertTrue(beforeOrder < goOrder, message = "Before block should run before 'go'") assertTrue(goOrder < afterOrder, message = "After block should run after 'go'") } @@ -45,7 +47,7 @@ class UltronTestFlowTest2 : UltronTest() { }.after { UltronLog.info("After TestMethod 2") }.go { - assertTrue(beforeAllTestCounter == 0, message = "beforeAllTests block should run only once") + assertEquals(1, beforeFirstTestCounter, message = "beforeFirstTest block should run before all test") UltronLog.info("Run TestMethod 2") } } diff --git a/gradle.properties b/gradle.properties index c49ebe1..898b741 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,4 +14,4 @@ kotlin.mpp.enableCInteropCommonization=true GROUP=com.atiurin POM_ARTIFACT_ID=ultron -VERSION_NAME=2.5.0-alpha16 +VERSION_NAME=2.5.1 diff --git a/sample-app/src/androidTest/java/com/atiurin/sampleapp/tests/testlifecycle/UltronTestFlowTest.kt b/sample-app/src/androidTest/java/com/atiurin/sampleapp/tests/testlifecycle/UltronTestFlowTest.kt index 09c2734..7251673 100644 --- a/sample-app/src/androidTest/java/com/atiurin/sampleapp/tests/testlifecycle/UltronTestFlowTest.kt +++ b/sample-app/src/androidTest/java/com/atiurin/sampleapp/tests/testlifecycle/UltronTestFlowTest.kt @@ -8,7 +8,7 @@ import org.junit.Test class UltronTestFlowTest : BaseTest() { companion object { var order = 0 - var beforeAllTestCounter = -1 + var isBeforeFirstTestCounter = 0 var commonBeforeOrder = -1 var commonAfterOrder = -1 var afterOrder = -1 @@ -17,7 +17,7 @@ class UltronTestFlowTest : BaseTest() { @OptIn(ExperimentalUltronApi::class) override val beforeFirstTest = { - beforeAllTestCounter = order + isBeforeFirstTestCounter++ UltronLog.info("Before Class") } @@ -39,6 +39,7 @@ class UltronTestFlowTest : BaseTest() { var goOrder = -1 order++ before { + assert(isBeforeFirstTestCounter == 1, lazyMessage = { "beforeFirstTest block should run before commonBefore block" }) beforeOrder = order order++ UltronLog.info("Before TestMethod 1") @@ -49,8 +50,6 @@ class UltronTestFlowTest : BaseTest() { }.after { afterOrder = order order++ - assert(beforeAllTestCounter == 0, lazyMessage = { "beforeAllTests block should run before all test" }) - assert(beforeAllTestCounter < commonBeforeOrder, lazyMessage = { "beforeAllTests block should run before commonBefore block" }) assert(commonBeforeOrder < beforeOrder, lazyMessage = { "beforeOrder block should run after commonBefore block" }) assert(beforeOrder < goOrder, lazyMessage = { "Before block should run before 'go'" }) assert(goOrder < afterOrder, lazyMessage = { "After block should run after 'go'" }) @@ -64,14 +63,14 @@ class UltronTestFlowTest : BaseTest() { }.after { UltronLog.info("After TestMethod 2") }.go { - assert(beforeAllTestCounter == 0, lazyMessage = { "beforeAllTests block should run only once" }) + assert(isBeforeFirstTestCounter == 1, lazyMessage = { "beforeFirstTest block should run only once" }) UltronLog.info("Run TestMethod 2") } } @Test fun simpleTest() = test { - assert(beforeAllTestCounter == 0, lazyMessage = { "beforeAllTests block should run only once" }) + assert(isBeforeFirstTestCounter == 1, lazyMessage = { "beforeAllTests block should run only once" }) UltronLog.info("UltronTest simpleTest") } } \ No newline at end of file diff --git a/ultron-common/src/commonMain/kotlin/com/atiurin/ultron/core/test/UltronTest.kt b/ultron-common/src/commonMain/kotlin/com/atiurin/ultron/core/test/UltronTest.kt index 1700417..3f5f047 100644 --- a/ultron-common/src/commonMain/kotlin/com/atiurin/ultron/core/test/UltronTest.kt +++ b/ultron-common/src/commonMain/kotlin/com/atiurin/ultron/core/test/UltronTest.kt @@ -26,7 +26,7 @@ open class UltronTest( * * @throws UltronException if the test class is anonymous. */ - private val className = this::class.qualifiedName + private val className = this::class.simpleName ?: throw UltronException("Don't use anonymous class for UltronTest") /** From 2df396d524100822058778cf6552507e215e7d39 Mon Sep 17 00:00:00 2001 From: Aleksei Tiurin Date: Sun, 1 Dec 2024 14:04:37 +0200 Subject: [PATCH 2/3] Fix failed tests flow --- .../commonTest/kotlin/UltronTestFlowTest.kt | 36 ++++++++++++++++++- .../tests/testlifecycle/UltronTestFlowTest.kt | 23 ++++++++++-- .../atiurin/ultron/core/test/TestMethod.kt | 11 ++++-- .../atiurin/ultron/core/test/UltronTest.kt | 18 +++++++--- 4 files changed, 79 insertions(+), 9 deletions(-) diff --git a/composeApp/src/commonTest/kotlin/UltronTestFlowTest.kt b/composeApp/src/commonTest/kotlin/UltronTestFlowTest.kt index e66f919..8040808 100644 --- a/composeApp/src/commonTest/kotlin/UltronTestFlowTest.kt +++ b/composeApp/src/commonTest/kotlin/UltronTestFlowTest.kt @@ -1,8 +1,8 @@ - import com.atiurin.ultron.annotations.ExperimentalUltronApi import com.atiurin.ultron.core.test.UltronTest import com.atiurin.ultron.log.UltronLog import kotlin.test.Test +import kotlin.test.assertEquals import kotlin.test.assertTrue class UltronTestFlowTest : UltronTest() { @@ -72,4 +72,38 @@ class UltronTestFlowTest : UltronTest() { assertTrue(beforeFirstTestCounter == 1, message = "beforeFirstTest block should run only once") UltronLog.info("UltronTest simpleTest") } + + + @Test + fun afterBlockExecutedOnFailedTest() { + var isAfterExecuted = false + runCatching { + test { + go { + throw RuntimeException("test exception") + } + after { + isAfterExecuted = true + } + } + } + assertTrue(isAfterExecuted) + } + + @Test + fun testExceptionMessageThrownOnFailedTest() { + val testExceptionMessage = "test exception" + runCatching { + test { + go { + throw RuntimeException(testExceptionMessage) + } + after { + throw RuntimeException("Another after exception") + } + } + }.onFailure { ex -> + assertEquals(ex.message, testExceptionMessage) + } + } } \ No newline at end of file diff --git a/sample-app/src/androidTest/java/com/atiurin/sampleapp/tests/testlifecycle/UltronTestFlowTest.kt b/sample-app/src/androidTest/java/com/atiurin/sampleapp/tests/testlifecycle/UltronTestFlowTest.kt index 7251673..5829496 100644 --- a/sample-app/src/androidTest/java/com/atiurin/sampleapp/tests/testlifecycle/UltronTestFlowTest.kt +++ b/sample-app/src/androidTest/java/com/atiurin/sampleapp/tests/testlifecycle/UltronTestFlowTest.kt @@ -3,6 +3,7 @@ package com.atiurin.sampleapp.tests.testlifecycle import com.atiurin.sampleapp.tests.BaseTest import com.atiurin.ultron.annotations.ExperimentalUltronApi import com.atiurin.ultron.log.UltronLog +import org.junit.Assert import org.junit.Test class UltronTestFlowTest : BaseTest() { @@ -60,9 +61,11 @@ class UltronTestFlowTest : BaseTest() { fun someTest2() = test(suppressCommonBefore = true) { before { UltronLog.info("Before TestMethod 2") - }.after { + } + after { UltronLog.info("After TestMethod 2") - }.go { + } + go { assert(isBeforeFirstTestCounter == 1, lazyMessage = { "beforeFirstTest block should run only once" }) UltronLog.info("Run TestMethod 2") } @@ -73,4 +76,20 @@ class UltronTestFlowTest : BaseTest() { assert(isBeforeFirstTestCounter == 1, lazyMessage = { "beforeAllTests block should run only once" }) UltronLog.info("UltronTest simpleTest") } + + @Test + fun afterBlockExecutedOnFailedTest() { + var isAfterExecuted = false + runCatching { + test { + go{ + throw RuntimeException("test exception") + } + after { + isAfterExecuted = true + } + } + } + Assert.assertTrue(isAfterExecuted) + } } \ No newline at end of file diff --git a/ultron-common/src/commonMain/kotlin/com/atiurin/ultron/core/test/TestMethod.kt b/ultron-common/src/commonMain/kotlin/com/atiurin/ultron/core/test/TestMethod.kt index 8d4c678..8507528 100644 --- a/ultron-common/src/commonMain/kotlin/com/atiurin/ultron/core/test/TestMethod.kt +++ b/ultron-common/src/commonMain/kotlin/com/atiurin/ultron/core/test/TestMethod.kt @@ -13,9 +13,16 @@ class TestMethod(testContext: UltronTestContext) { private var test: TestMethod.() -> Unit = {} internal fun attack() { + var throwable: Throwable? = null beforeTest() - test() - afterTest() + runCatching(test).onFailure { ex -> + throwable = ex + } + runCatching(afterTest).onFailure { ex -> + throwable?.let { throw it } + throw ex + } + throwable?.let { throw it } } fun before(block: TestMethod.() -> Unit) = apply { diff --git a/ultron-common/src/commonMain/kotlin/com/atiurin/ultron/core/test/UltronTest.kt b/ultron-common/src/commonMain/kotlin/com/atiurin/ultron/core/test/UltronTest.kt index 3f5f047..b178fdb 100644 --- a/ultron-common/src/commonMain/kotlin/com/atiurin/ultron/core/test/UltronTest.kt +++ b/ultron-common/src/commonMain/kotlin/com/atiurin/ultron/core/test/UltronTest.kt @@ -72,15 +72,25 @@ open class UltronTest( if (!suppressCommonBefore) { beforeTest() } - + var throwable: Throwable? = null // Configure and execute the test block - configureTestBlock() - attack() + runCatching { + configureTestBlock() + attack() + }.onFailure { ex -> + throwable = ex + } // Execute common `afterTest` logic if not suppressed if (!suppressCommonAfter) { - afterTest() + runCatching(afterTest).onFailure { ex -> + throwable?.let { throw it } + throw ex + } } + throwable?.let { throw it } } } + + } From a9bc5a3130637f7f5f2cc2817e3ec2ad88b946f2 Mon Sep 17 00:00:00 2001 From: Aleksei Tiurin Date: Sun, 1 Dec 2024 15:02:47 +0200 Subject: [PATCH 3/3] 2.5.2 --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 898b741..349b5fc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,4 +14,4 @@ kotlin.mpp.enableCInteropCommonization=true GROUP=com.atiurin POM_ARTIFACT_ID=ultron -VERSION_NAME=2.5.1 +VERSION_NAME=2.5.2