How to use KClass.findTestFunctions method of io.kotest.core.spec.style.AnnotationSpec class

Best Kotest code snippet using io.kotest.core.spec.style.AnnotationSpec.KClass.findTestFunctions

AnnotationSpec.kt

Source:AnnotationSpec.kt Github

copy

Full Screen

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 }...

Full Screen

Full Screen

KClass.findTestFunctions

Using AI Code Generation

copy

Full Screen

1import io.kotest.core.spec.style.AnnotationSpec2import io.kotest.matchers.shouldBe3class AnnotationSpecTest : AnnotationSpec() {4 fun test1() {5 }6 fun test2() {7 }8}9import io.kotest.core.spec.style.BehaviorSpec10import io.kotest.matchers.shouldBe11class BehaviorSpecTest : BehaviorSpec() {12 init {13 given("a test") {14 `when`("a condition is true") {15 then("do this") {16 }17 }18 }19 }20}21import io.kotest.core.spec.style.DescribeSpec22import io.kotest.matchers.shouldBe23class DescribeSpecTest : DescribeSpec() {24 init {25 describe("a test") {26 context("a condition is true") {27 it("do this") {28 }29 }30 }31 }32}33import io.kotest.core.spec.style.ExpectSpec34import io.kotest.matchers.shouldBe35class ExpectSpecTest : ExpectSpec() {36 init {37 context("a condition is true") {38 expect("do this") {39 }40 }41 }42}43import io.kotest.core.spec.style.FreeSpec44import io.kotest.matchers.shouldBe45class FreeSpecTest : FreeSpec() {46 init {47 "a test" - {48 "a condition is true" - {49 "do this" {50 }51 }52 }53 }54}55import io.kotest.core.spec.style.Feature

Full Screen

Full Screen

KClass.findTestFunctions

Using AI Code Generation

copy

Full Screen

1class TestAnnotationSpec : AnnotationSpec() {2 fun test1() {3 println("test1")4 }5 fun test2() {6 println("test2")7 }8}9class TestFunSpec : FunSpec() {10 init {11 test("test1") {12 println("test1")13 }14 test("test2") {15 println("test2")16 }17 }18}19class TestFreeSpec : FreeSpec() {20 init {21 "test1" {22 println("test1")23 }24 "test2" {25 println("test2")26 }27 }28}29class TestStringSpec : StringSpec() {30 init {31 "test1" {32 println("test1")33 }34 "test2" {35 println("test2")36 }37 }38}39class TestWordSpec : WordSpec() {40 init {41 "test1" should {42 println("test1")43 }44 "test2" should {45 println("test2")46 }47 }48}49class TestBehaviorSpec : BehaviorSpec() {50 init {51 Given("test1") {52 println("test1")53 }54 Given("test2") {55 println("test2")56 }57 }58}59class TestExpectSpec : ExpectSpec() {60 init {61 expect("test1") {62 println("test1")

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful