Best Kotest code snippet using io.kotest.core.spec.style.AnnotationSpec.KClass.findRootTests
AnnotationSpec.kt
Source:AnnotationSpec.kt
1package io.kotest.core.spec.style2import io.kotest.core.extensions.Extension3import io.kotest.core.names.TestName4import io.kotest.core.source.sourceRef5import io.kotest.core.spec.RootTest6import io.kotest.core.spec.Spec7import io.kotest.core.test.TestCase8import io.kotest.core.test.TestResult9import io.kotest.core.test.TestScope10import io.kotest.core.test.TestType11import io.kotest.mpp.unwrapIfReflectionCall12import kotlin.reflect.KClass13import kotlin.reflect.KFunction14import kotlin.reflect.full.callSuspend15import kotlin.reflect.full.memberFunctions16import kotlin.reflect.jvm.isAccessible17typealias Test = AnnotationSpec.Test18abstract class AnnotationSpec : Spec() {19 override suspend fun beforeSpec(spec: Spec) {20 executeBeforeSpecFunctions()21 }22 private suspend fun executeBeforeSpecFunctions() = this::class.findBeforeSpecFunctions().forEach {23 if (it.isSuspend) it.callSuspend(this) else it.call(this)24 }25 override suspend fun beforeTest(testCase: TestCase) {26 executeBeforeTestFunctions()27 }28 private suspend fun executeBeforeTestFunctions() = this::class.findBeforeTestFunctions().forEach {29 if (it.isSuspend) it.callSuspend(this) else it.call(this)30 }31 override suspend fun afterTest(testCase: TestCase, result: TestResult) {32 executeAfterTestFunctions()33 }34 private suspend fun executeAfterTestFunctions() = this::class.findAfterTestFunctions().forEach {35 if (it.isSuspend) it.callSuspend(this) else it.call(this)36 }37 override suspend fun afterSpec(spec: Spec) {38 executeAfterSpecFunctions()39 }40 private suspend fun executeAfterSpecFunctions() = this::class.findAfterSpecFunctions().forEach {41 if (it.isSuspend) it.callSuspend(this) else it.call(this)42 }43 private fun KFunction<*>.toIgnoredRootTest(): RootTest {44 return deriveRootTest(true)45 }46 private fun KFunction<*>.toEnabledRootTest(): RootTest {47 return deriveRootTest(false)48 }49 private fun KFunction<*>.deriveRootTest(disabled: Boolean): RootTest {50 return if (this.isExpectingException()) {51 val expected = this.getExpectedException()52 RootTest(53 name = TestName(name),54 test = callExpectingException(expected),55 source = sourceRef(),56 type = TestType.Test,57 config = null,58 disabled = disabled,59 factoryId = null,60 )61 } else {62 RootTest(63 name = TestName(name),64 test = callNotExpectingException(),65 source = sourceRef(),66 type = TestType.Test,67 config = null,68 disabled = disabled,69 factoryId = null,70 )71 }72 }73 override fun rootTests(): List<RootTest> {74 val tests = this::class.findRootTests()75 val nested = this::class.findNestedTests()76 return tests + nested77 }78 override fun globalExtensions(): List<Extension> {79 return emptyList()80 }81 private fun KFunction<*>.isExpectingException(): Boolean {82 return annotations.filterIsInstance<Test>().first().expected != Test.None::class83 }84 private fun KFunction<*>.getExpectedException(): KClass<out Throwable> {85 return annotations.filterIsInstance<Test>().first().expected86 }87 private fun KClass<*>.findRootTests(): List<RootTest> {88 return findTestFunctions().map { f ->89 f.isAccessible = true90 if (f.isIgnoredTest()) {91 f.toIgnoredRootTest()92 } else {93 f.toEnabledRootTest()94 }95 }96 }97 private fun KClass<out AnnotationSpec>.findNestedTests(): List<RootTest> {98 return nestedClasses99 .filter { kclass -> kclass.annotations.map { it.annotationClass }.contains(Nested::class) }100 .flatMap { it.findRootTests() }101 }102 private fun KFunction<*>.callExpectingException(expected: KClass<out Throwable>): suspend TestScope.() -> Unit {103 return {104 val thrown = try {105 callSuspend(this@AnnotationSpec)106 null107 } catch (t: Throwable) {108 t.unwrapIfReflectionCall()109 } ?: failNoExceptionThrown(expected)110 if (thrown::class != expected) failWrongExceptionThrown(expected, thrown)111 }112 }113 private fun KFunction<*>.callNotExpectingException(): suspend TestScope.() -> Unit {114 return {115 try {116 callSuspend(this@AnnotationSpec)117 } catch (t: Throwable) {118 throw t.unwrapIfReflectionCall()119 }120 }121 }122 private fun failNoExceptionThrown(expected: KClass<out Throwable>): Nothing {123 throw AssertionError("Expected exception of class ${expected.simpleName}, but no exception was thrown.")124 }125 private fun failWrongExceptionThrown(expected: KClass<out Throwable>, thrown: Throwable): Nothing {126 throw AssertionError("Expected exception of class ${expected.simpleName}, but ${thrown::class.simpleName} was thrown instead.")127 }128 // All annotations should be kept inside this class, to avoid any usage outside of AnnotationSpec.129 // One can only use annotations to execute code inside AnnotationSpec.130 /**131 * Marks a function to be executed before each test132 *133 * This can be used in AnnotationSpec to mark a function to be executed before every test by Kotest Engine134 * @see BeforeAll135 * @see AfterEach136 */137 annotation class BeforeEach138 /**139 * Marks a function to be executed before each test140 *141 * This can be used in AnnotationSpec to mark a function to be executed before every test by Kotest Engine142 * @see BeforeClass143 * @see After144 */145 annotation class Before146 /**147 * Marks a function to be executed before each spec148 *149 * This can be used in AnnotationSpec to mark a function to be executed before a spec by Kotest Engine.150 * @see BeforeEach151 * @see AfterAll152 */153 annotation class BeforeAll154 /**155 * Marks a function to be executed before each spec156 *157 * This can be used in AnnotationSpec to mark a function to be executed before a spec by Kotest Engine.158 * @see Before159 * @see AfterClass160 */161 annotation class BeforeClass162 /**163 * Marks a function to be executed after each test164 *165 * This can be used in AnnotationSpec to mark a function to be executed before a test by Kotest Engine.166 * @see AfterAll167 * @see BeforeEach168 */169 annotation class AfterEach170 /**171 * Marks a function to be executed after each test172 *173 * This can be used in AnnotationSpec to mark a function to be executed before a test by Kotest Engine.174 * @see AfterClass175 * @see Before176 */177 annotation class After178 /**179 * Marks a function to be executed after each spec180 *181 * This can be used in AnnotationSpec to mark a function to be executed before a spec by Kotest Engine.182 * @see AfterEach183 * @see BeforeAll184 */185 annotation class AfterAll186 /**187 * Marks a function to be executed after each spec188 *189 * This can be used in AnnotationSpec to mark a function to be executed before a spec by Kotest Engine.190 * @see After191 * @see BeforeClass192 */193 annotation class AfterClass194 /**195 * Marks a function to be executed as a Test196 *197 * This can be used in AnnotationSpec to mark a function to be executed as a test by Kotest Engine.198 *199 *200 * [expected] can be used to mark a test to expect a specific exception.201 *202 * This is useful when moving from JUnit, in which you use expected to verify for an exception.203 *204 * ```205 * @Test(expected = FooException::class)206 * fun foo() {207 * throw FooException() // Pass208 * }209 *210 * @Test(expected = FooException::class211 * fun bar() {212 * throw BarException() // Fails, FooException was expected213 * }214 * ```215 */216 annotation class Test(val expected: KClass<out Throwable> = None::class) {217 object None : Throwable()218 }219 /**220 * Marks a Test to be ignored221 *222 * This can be used in AnnotationSpec to mark a Test as Ignored.223 */224 annotation class Ignore225 annotation class Nested226}227internal fun KClass<out AnnotationSpec>.findBeforeTestFunctions() =228 findFunctionAnnotatedWithAnyOf(AnnotationSpec.BeforeEach::class, AnnotationSpec.Before::class)229internal fun KClass<out AnnotationSpec>.findBeforeSpecFunctions() =230 findFunctionAnnotatedWithAnyOf(AnnotationSpec.BeforeAll::class, AnnotationSpec.BeforeClass::class)231internal fun KClass<out AnnotationSpec>.findAfterSpecFunctions() =232 findFunctionAnnotatedWithAnyOf(AnnotationSpec.AfterAll::class, AnnotationSpec.AfterClass::class)233internal fun KClass<out AnnotationSpec>.findAfterTestFunctions() =234 findFunctionAnnotatedWithAnyOf(AnnotationSpec.AfterEach::class, AnnotationSpec.After::class)235internal fun KClass<*>.findTestFunctions(): List<KFunction<*>> =236 findFunctionAnnotatedWithAnyOf(AnnotationSpec.Test::class)237internal fun KFunction<*>.isIgnoredTest() = isFunctionAnnotatedWithAnyOf(AnnotationSpec.Ignore::class)238internal fun KClass<*>.findFunctionAnnotatedWithAnyOf(vararg annotation: KClass<*>): List<KFunction<*>> =239 memberFunctions.filter { it.isFunctionAnnotatedWithAnyOf(*annotation) }240internal fun KFunction<*>.isFunctionAnnotatedWithAnyOf(vararg annotation: KClass<*>) =241 annotations.any { it.annotationClass in annotation }...
KClass.findRootTests
Using AI Code Generation
1val rootTests = AnnotationSpec::class.findRootTests()2println(rootTests)3val rootTests = FunSpec::class.findRootTests()4println(rootTests)5val rootTests = FreeSpec::class.findRootTests()6println(rootTests)7val rootTests = ExpectSpec::class.findRootTests()8println(rootTests)9val rootTests = FeatureSpec::class.findRootTests()10println(rootTests)11val rootTests = FreeSpec::class.findRootTests()12println(rootTests)13val rootTests = FreeSpec::class.findRootTests()14println(rootTests)15val rootTests = FreeSpec::class.findRootTests()16println(rootTests)17val rootTests = FreeSpec::class.findRootTests()18println(rootTests)19val rootTests = FreeSpec::class.findRootTests()20println(rootTests)21val rootTests = FreeSpec::class.findRootTests()22println(rootTests)23val rootTests = FreeSpec::class.findRootTests()24println(rootTests)25val rootTests = FreeSpec::class.findRootTests()26println(rootTests)
KClass.findRootTests
Using AI Code Generation
1 fun `test RootTestFinder`() {2 val rootTests = KClass.findRootTests()3 assertThat(rootTests).hasSize(2)4 assertThat(rootTests).contains("root test 1")5 assertThat(rootTests).contains("root test 2")6 }7}
KClass.findRootTests
Using AI Code Generation
1import io.kotest.core.spec.style.AnnotationSpec2class RootTestExampleTest : AnnotationSpec() {3 fun test1() {4 assert(true)5 }6 fun test2() {7 assert(true)8 }9 fun test3() {10 assert(true)11 }12 fun test4() {13 assert(true)14 }15 fun test5() {16 assert(true)17 }18 fun test6() {19 assert(true)20 }21 fun test7() {22 assert(true)23 }24 fun test8() {25 assert(true)26 }27 fun test9() {28 assert(true)29 }30 fun test10() {31 assert(true)32 }33 fun test11() {34 assert(true)35 }36 fun test12() {37 assert(true)38 }39 fun test13() {40 assert(true)41 }42 fun test14() {43 assert(true)44 }45 fun test15() {46 assert(true)47 }48 fun test16() {49 assert(true)50 }51 fun test17() {52 assert(true)53 }54 fun test18() {55 assert(true)56 }57 fun test19() {58 assert(true)59 }60 fun test20() {61 assert(true)62 }63 fun test21() {64 assert(true)65 }66 fun test22() {67 assert(true)68 }69 fun test23() {70 assert(true)71 }72 fun test24() {73 assert(true)74 }75 fun test25() {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!