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

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

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

Using AI Code Generation

copy

Full Screen

1val beforeTestFunctions = AnnotationSpec::class.findBeforeTestFunctions()2println("Before Test Functions: $beforeTestFunctions")3Before Test Functions: [public final fun io.kotest.core.spec.style.AnnotationSpec.beforeTest(): kotlin.Unit]4val afterTestFunctions = AnnotationSpec::class.findAfterTestFunctions()5println("After Test Functions: $afterTestFunctions")6After Test Functions: [public final fun io.kotest.core.spec.style.AnnotationSpec.afterTest(): kotlin.Unit]7val testFunctions = AnnotationSpec::class.findTestFunctions()8println("Test Functions: $testFunctions")9Test Functions: [public final fun io.kotest.core.spec.style.AnnotationSpec.test(): kotlin.Unit]10val testExtensions = AnnotationSpec::class.findTestExtensions()11println("Test Extensions: $testExtensions")12Test Extensions: [public final fun io.kotest.core.spec.style.AnnotationSpec.test(): kotlin.Unit]13val testExtensions = AnnotationSpec::class.findTestExtensions()14println("Test Extensions: $testExtensions")15Test Extensions: [public final fun io.kotest.core.spec.style.AnnotationSpec.test(): kotlin.Unit]16val testExtensions = AnnotationSpec::class.findTestExtensions()17println("Test Extensions: $testExtensions")18Test Extensions: [public final fun io.kotest.core.spec.style.AnnotationSpec.test(): kotlin.Unit]19val testExtensions = AnnotationSpec::class.findTestExtensions()20println("Test Extensions: $testExtensions")21Test Extensions: [public final fun io.kotest.core.spec.style.AnnotationSpec.test(): kotlin.Unit]22val testExtensions = AnnotationSpec::class.findTestExtensions()23println("Test Extensions: $testExtensions")

Full Screen

Full Screen

KClass.findBeforeTestFunctions

Using AI Code Generation

copy

Full Screen

1@DisplayName("Kotest BeforeTest Annotation")2class BeforeTestAnnotationTest : AnnotationSpec() {3 fun beforeTest() {4 println("Before Test")5 }6 fun test1() {7 println("Test 1")8 }9 fun test2() {10 println("Test 2")11 }12}13@DisplayName("Kotest BeforeSpec Annotation")14class BeforeSpecAnnotationTest : AnnotationSpec() {15 fun beforeSpec() {16 println("Before Spec")17 }18 fun test1() {19 println("Test 1")20 }21 fun test2() {22 println("Test 2")23 }24}25@DisplayName("Kotest AfterTest Annotation")26class AfterTestAnnotationTest : AnnotationSpec() {27 fun afterTest() {28 println("After Test")29 }30 fun test1() {31 println("Test 1")32 }33 fun test2() {34 println("Test

Full Screen

Full Screen

KClass.findBeforeTestFunctions

Using AI Code Generation

copy

Full Screen

1val beforeTestFunctions = this::class.findBeforeTestFunctions()2beforeTestFunctions.forEach { it.call(this) }3this::class.findTestFunctions().forEach { it.call(this) }4val afterTestFunctions = this::class.findAfterTestFunctions()5afterTestFunctions.forEach { it.call(this) }6override fun execute() {7this::class.findTestFunctions().forEach { it.call(this) }8}

Full Screen

Full Screen

Blogs

Check out the latest blogs from LambdaTest on this topic:

How To Get Started With Cypress Debugging

One of the most important tasks of a software developer is not just writing code fast; it is the ability to find what causes errors and bugs whenever you encounter one and the ability to solve them quickly.

A Complete Guide To CSS Container Queries

In 2007, Steve Jobs launched the first iPhone, which revolutionized the world. But because of that, many businesses dealt with the problem of changing the layout of websites from desktop to mobile by delivering completely different mobile-compatible websites under the subdomain of ‘m’ (e.g., https://m.facebook.com). And we were all trying to figure out how to work in this new world of contending with mobile and desktop screen sizes.

Project Goal Prioritization in Context of Your Organization&#8217;s Strategic Objectives

One of the most important skills for leaders to have is the ability to prioritize. To understand how we can organize all of the tasks that must be completed in order to complete a project, we must first understand the business we are in, particularly the project goals. There might be several project drivers that stimulate project execution and motivate a company to allocate the appropriate funding.

How to increase and maintain team motivation

The best agile teams are built from people who work together as one unit, where each team member has both the technical and the personal skills to allow the team to become self-organized, cross-functional, and self-motivated. These are all big words that I hear in almost every agile project. Still, the criteria to make a fantastic agile team are practically impossible to achieve without one major factor: motivation towards a common goal.

Introducing LambdaTest Analytics: Test Reporting Made Awesome ????

Collecting and examining data from multiple sources can be a tedious process. The digital world is constantly evolving. To stay competitive in this fast-paced environment, businesses must frequently test their products and services. While it’s easy to collect raw data from multiple sources, it’s far more complex to interpret it properly.

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