From 6e8d3e863f87375d98b27cc0564e7a4319ba5720 Mon Sep 17 00:00:00 2001 From: Aleksei Tiurin Date: Sun, 1 Dec 2024 15:17:52 +0200 Subject: [PATCH] Release/2.5.1 (#98) * 2.5.2 --- .../commonTest/kotlin/UltronTestFlowTest.kt | 48 +++++++++++++++---- .../commonTest/kotlin/UltronTestFlowTest2.kt | 14 +++--- gradle.properties | 2 +- .../tests/testlifecycle/UltronTestFlowTest.kt | 34 +++++++++---- .../atiurin/ultron/core/test/TestMethod.kt | 11 ++++- .../atiurin/ultron/core/test/UltronTest.kt | 20 ++++++-- 6 files changed, 99 insertions(+), 30 deletions(-) diff --git a/composeApp/src/commonTest/kotlin/UltronTestFlowTest.kt b/composeApp/src/commonTest/kotlin/UltronTestFlowTest.kt index bba2037..8040808 100644 --- a/composeApp/src/commonTest/kotlin/UltronTestFlowTest.kt +++ b/composeApp/src/commonTest/kotlin/UltronTestFlowTest.kt @@ -1,23 +1,22 @@ - 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() { 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,48 @@ 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") } + + + @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/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..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.0-alpha16 +VERSION_NAME=2.5.2 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..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,12 +3,13 @@ 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() { companion object { var order = 0 - var beforeAllTestCounter = -1 + var isBeforeFirstTestCounter = 0 var commonBeforeOrder = -1 var commonAfterOrder = -1 var afterOrder = -1 @@ -17,7 +18,7 @@ class UltronTestFlowTest : BaseTest() { @OptIn(ExperimentalUltronApi::class) override val beforeFirstTest = { - beforeAllTestCounter = order + isBeforeFirstTestCounter++ UltronLog.info("Before Class") } @@ -39,6 +40,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 +51,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'" }) @@ -61,17 +61,35 @@ class UltronTestFlowTest : BaseTest() { fun someTest2() = test(suppressCommonBefore = true) { before { UltronLog.info("Before TestMethod 2") - }.after { + } + after { UltronLog.info("After TestMethod 2") - }.go { - assert(beforeAllTestCounter == 0, lazyMessage = { "beforeAllTests block should run only once" }) + } + go { + 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") } + + @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 1700417..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 @@ -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") /** @@ -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 } } } + + }