How to use extensions method of io.kotest.engine.test.TestExtensions class

Best Kotest code snippet using io.kotest.engine.test.TestExtensions.extensions

TestExtensions.kt

Source:TestExtensions.kt Github

copy

Full Screen

1package io.kotest.engine.test2import io.kotest.common.collect3import io.kotest.common.mapError4import io.kotest.core.config.ExtensionRegistry5import io.kotest.core.extensions.Extension6import io.kotest.core.extensions.TestCaseExtension7import io.kotest.core.listeners.AfterContainerListener8import io.kotest.core.listeners.AfterEachListener9import io.kotest.core.listeners.AfterInvocationListener10import io.kotest.core.listeners.AfterTestListener11import io.kotest.core.listeners.BeforeContainerListener12import io.kotest.core.listeners.BeforeEachListener13import io.kotest.core.listeners.BeforeInvocationListener14import io.kotest.core.listeners.BeforeTestListener15import io.kotest.core.spec.functionOverrideCallbacks16import io.kotest.core.test.TestCase17import io.kotest.core.test.TestResult18import io.kotest.core.test.TestScope19import io.kotest.core.test.TestType20import io.kotest.engine.extensions.ExtensionException21import io.kotest.engine.extensions.MultipleExceptions22import io.kotest.engine.test.logging.LogExtension23import io.kotest.engine.test.scopes.withCoroutineContext24import kotlin.coroutines.coroutineContext25/**26 * Used to invoke extension points on tests.27 */28internal class TestExtensions(private val registry: ExtensionRegistry) {29 /**30 * Returns all [Extension]s applicable to a [TestCase]. This includes extensions31 * included in test case config, those at the spec level, and project wide from32 * the registry.33 */34 fun extensions(testCase: TestCase): List<Extension> {35 return testCase.config.extensions +36 testCase.spec.extensions() + // overriding the extensions function in the spec37 testCase.spec.listeners() + // overriding the listeners function in the spec38 testCase.spec.functionOverrideCallbacks() + // spec level dsl eg beforeTest { }39 testCase.spec.registeredExtensions() + // added to the spec via register40 registry.all() // globals41 }42 suspend fun beforeInvocation(testCase: TestCase, invocation: Int): Result<TestCase> {43 val extensions = extensions(testCase).filterIsInstance<BeforeInvocationListener>()44 return extensions.map {45 runCatching {46 it.beforeInvocation(testCase, invocation)47 }.mapError { ExtensionException.BeforeInvocationException(it) }48 }.collect { if (it.size == 1) it.first() else MultipleExceptions(it) }.map { testCase }49 }50 suspend fun afterInvocation(testCase: TestCase, invocation: Int): Result<TestCase> {51 val extensions = extensions(testCase).filterIsInstance<AfterInvocationListener>()52 return extensions.map {53 runCatching {54 it.afterInvocation(testCase, invocation)55 }.mapError { ExtensionException.AfterInvocationException(it) }56 }.collect { if (it.size == 1) it.first() else MultipleExceptions(it) }.map { testCase }57 }58 /**59 * Invokes all beforeXYZ callbacks for this test, checking for the appropriate test type.60 * Returns a Result of [MultipleExceptions] if there are any exceptions.61 */62 suspend fun beforeTestBeforeAnyBeforeContainer(testCase: TestCase): Result<TestCase> {63 val bt = extensions(testCase).filterIsInstance<BeforeTestListener>()64 val bc = extensions(testCase).filterIsInstance<BeforeContainerListener>()65 val be = extensions(testCase).filterIsInstance<BeforeEachListener>()66 val errors = bc.mapNotNull {67 runCatching {68 if (testCase.type == TestType.Container) it.beforeContainer(testCase)69 }.mapError { ExtensionException.BeforeContainerException(it) }.exceptionOrNull()70 } + be.mapNotNull {71 runCatching {72 if (testCase.type == TestType.Test) it.beforeEach(testCase)73 }.mapError { ExtensionException.BeforeEachException(it) }.exceptionOrNull()74 } + bt.mapNotNull {75 runCatching {76 it.beforeAny(testCase)77 }.mapError { ExtensionException.BeforeAnyException(it) }.exceptionOrNull()78 } + bt.mapNotNull {79 runCatching {80 it.beforeTest(testCase)81 }.mapError { ExtensionException.BeforeTestException(it) }.exceptionOrNull()82 }83 return when {84 errors.isEmpty() -> Result.success(testCase)85 errors.size == 1 -> Result.failure(errors.first())86 else -> Result.failure(MultipleExceptions(errors))87 }88 }89 /**90 * Invokes all beforeXYZ callbacks for this test.91 * Returns a Result of [MultipleExceptions] if there are any exceptions.92 */93 suspend fun afterTestAfterAnyAfterContainer(testCase: TestCase, result: TestResult): Result<TestResult> {94 val at = extensions(testCase).filterIsInstance<AfterTestListener>()95 val ac = extensions(testCase).filterIsInstance<AfterContainerListener>()96 val ae = extensions(testCase).filterIsInstance<AfterEachListener>()97 val errors = at.mapNotNull {98 runCatching {99 it.afterTest(testCase, result)100 }.mapError { ExtensionException.AfterTestException(it) }.exceptionOrNull()101 } + at.mapNotNull {102 runCatching {103 it.afterAny(testCase, result)104 }.mapError { ExtensionException.AfterAnyException(it) }.exceptionOrNull()105 } + ac.mapNotNull {106 runCatching {107 if (testCase.type == TestType.Container) it.afterContainer(testCase, result)108 }.mapError { ExtensionException.AfterContainerException(it) }.exceptionOrNull()109 } + ae.mapNotNull {110 runCatching {111 if (testCase.type == TestType.Test) it.afterEach(testCase, result)112 }.mapError { ExtensionException.AfterEachException(it) }.exceptionOrNull()113 }114 return when {115 errors.isEmpty() -> Result.success(result)116 errors.size == 1 -> Result.failure(errors.first())117 else -> Result.failure(MultipleExceptions(errors))118 }119 }120 /**121 * Executes the [TestCaseExtension]s for this [TestCase].122 */123 suspend fun intercept(124 testCase: TestCase,125 context: TestScope,126 inner: suspend (TestCase, TestScope) -> TestResult,127 ): TestResult {128 val execute = extensions(testCase).filterIsInstance<TestCaseExtension>()129 .foldRight(inner) { extension, execute ->130 { tc, ctx ->131 extension.intercept(tc) {132 // the user's intercept method is free to change the context of the coroutine133 // to support this, we should switch the context used by the test case context134 execute(it, ctx.withCoroutineContext(coroutineContext))135 }136 }137 }138 return execute(testCase, context)139 }140 fun logExtensions(testCase: TestCase): List<LogExtension> {141 return extensions(testCase).filterIsInstance<LogExtension>()142 }143}...

Full Screen

Full Screen

LifecycleInterceptor.kt

Source:LifecycleInterceptor.kt Github

copy

Full Screen

...28 private val listener: TestCaseExecutionListener,29 private val timeMark: TimeMark,30 registry: ExtensionRegistry,31) : TestExecutionInterceptor {32 private val extensions = TestExtensions(registry)33 private val logger = Logger(LifecycleInterceptor::class)34 override suspend fun intercept(35 testCase: TestCase,36 scope: TestScope,37 test: suspend (TestCase, TestScope) -> TestResult38 ): TestResult {39 logger.log { Pair(testCase.name.testName, "Notifying listener test started") }40 listener.testStarted(testCase)41 return extensions.beforeTestBeforeAnyBeforeContainer(testCase)42 .fold(43 {44 val result = test(testCase, scope)45 // any error in the after listeners will override the test result unless the test was already an error46 extensions47 .afterTestAfterAnyAfterContainer(testCase, result)48 .fold(49 { result },50 { if (result.isErrorOrFailure) result else createTestResult(timeMark.elapsedNow(), it) }51 )52 },53 {54 val result = createTestResult(timeMark.elapsedNow(), it)55 // can ignore errors here as we already have the before errors to show56 extensions.afterTestAfterAnyAfterContainer(testCase, result)57 result58 }59 )60 }61}...

Full Screen

Full Screen

TestInvocationInterceptor.kt

Source:TestInvocationInterceptor.kt Github

copy

Full Screen

...12class TestInvocationInterceptor(13 registry: ExtensionRegistry,14 private val timeMark: TimeMark,15) : TestExecutionInterceptor {16 private val extensions = TestExtensions(registry)17 private val logger = Logger(TestInvocationInterceptor::class)18 override suspend fun intercept(19 testCase: TestCase,20 scope: TestScope,21 test: suspend (TestCase, TestScope) -> TestResult22 ): TestResult {23 return try {24 // we wrap in a coroutine scope so that we wait for any user-launched coroutines to finish,25 // and so we can grab any exceptions they throw26 coroutineScope {27 replay(28 testCase.config.invocations,29 testCase.config.threads,30 { extensions.beforeInvocation(testCase, it) },31 { extensions.afterInvocation(testCase, it) }) {32 test(testCase, scope)33 }34 }35 logger.log { Pair(testCase.name.testName, "Test returned without error") }36 try {37 TestResult.Success(timeMark.elapsedNow())38 } catch (e: Throwable) {39 TestResult.Success(Duration.ZERO) // workaround for kotlin 1.540 }41 } catch (t: Throwable) {42 logger.log { Pair(testCase.name.testName, "Test threw error $t") }43 try {44 createTestResult(timeMark.elapsedNow(), t)45 } catch (e: Throwable) {...

Full Screen

Full Screen

CoroutineLoggingInterceptor.kt

Source:CoroutineLoggingInterceptor.kt Github

copy

Full Screen

...18 testCase: TestCase,19 scope: TestScope,20 test: suspend (TestCase, TestScope) -> TestResult21 ): TestResult {22 val extensions = TestExtensions(configuration.registry).logExtensions(testCase)23 return when {24 configuration.logLevel.isDisabled() || extensions.isEmpty() -> {25 logger.log { Pair(testCase.name.testName, "Test logging is disabled (exts = $extensions)") }26 test(testCase, scope)27 }28 else -> {29 val logger = TestLogger(configuration.logLevel)30 withContext(TestScopeLoggingCoroutineContextElement(logger)) {31 test(testCase, scope.withCoroutineContext(coroutineContext))32 }.apply {33 extensions.map { SerialLogExtension(it) }.forEach { extension ->34 runCatching {35 extension.handleLogs(testCase, logger.logs.filter { it.level >= configuration.logLevel })36 }37 }38 }39 }40 }41 }42}

Full Screen

Full Screen

TestCaseExtensionInterceptor.kt

Source:TestCaseExtensionInterceptor.kt Github

copy

Full Screen

1package io.kotest.engine.test.interceptors2import io.kotest.core.config.ExtensionRegistry3import io.kotest.core.extensions.TestCaseExtension4import io.kotest.core.test.TestCase5import io.kotest.core.test.TestResult6import io.kotest.core.test.TestScope7import io.kotest.engine.test.TestExtensions8/**9 * This [TestExecutionInterceptor] executes any user level [TestCaseExtension]s.10 *11 * This extension should happen early, so users can override any disabled status.12 */13internal class TestCaseExtensionInterceptor(registry: ExtensionRegistry) : TestExecutionInterceptor {14 private val extensions = TestExtensions(registry)15 override suspend fun intercept(16 testCase: TestCase,17 scope: TestScope,18 test: suspend (TestCase, TestScope) -> TestResult19 ): TestResult {20 return extensions.intercept(testCase, scope) { tc, s -> test(tc, s) }21 }22}...

Full Screen

Full Screen

extensions

Using AI Code Generation

copy

Full Screen

1val testExtensions = TestExtensions()2testExtensions.register(object : TestExtension {3 override suspend fun intercept(4 execute: suspend (TestCase) -> TestResult,5 complete: suspend (TestCase, TestResult) -> Unit6 ) {7 execute(test).also { result ->8 }9 }10})11val specExtensions = SpecExtensions()12specExtensions.register(object : SpecExtension {13 override suspend fun intercept(14 execute: suspend (Spec) -> Unit,15 complete: suspend (Spec, Throwable?) -> Unit16 ) {17 execute(spec).also {18 }19 }20})21val specInterceptExtensions = SpecInterceptExtensions()22specInterceptExtensions.register(object : SpecInterceptExtension {23 override suspend fun intercept(24 execute: suspend (Spec) -> Unit,25 complete: suspend (Spec, Throwable?) -> Unit26 ) {27 execute(spec).also {28 }29 }30})31val testInterceptExtensions = TestInterceptExtensions()32testInterceptExtensions.register(object : TestInterceptExtension {33 override suspend fun intercept(34 execute: suspend (TestCase) -> TestResult,35 complete: suspend (TestCase, TestResult) -> Unit36 ) {37 execute(test).also { result ->38 }39 }40})41val testCaseExtensions = TestCaseExtensions()42testCaseExtensions.register(object : TestCaseExtension {43 override suspend fun intercept(44 execute: suspend (TestCase) -> TestResult45 ): TestResult {46 return execute(testCase).also { result ->47 }48 }49})50val testExtensions = TestExtensions()51testExtensions.register(object : TestExtension {52 override suspend fun intercept(

Full Screen

Full Screen

extensions

Using AI Code Generation

copy

Full Screen

1class TestExtensionsTest : FunSpec() {2 override fun extensions(): List<Extension> = listOf(3 TestExtensions {4 afterSpec { println("afterSpec") }5 beforeSpec { println("beforeSpec") }6 afterTest { println("afterTest") }7 beforeTest { println("beforeTest") }8 }9 init {10 test("test") {11 println("test")12 }13 }14}

Full Screen

Full Screen

extensions

Using AI Code Generation

copy

Full Screen

1+class TestExtensionsTest : FunSpec({2+ test("test case 1").config(extensions = listOf(MockExtension())) {3+ println("test case 1")4+ }5+ test("test case 2").config(extensions = listOf(MockExtension())) {6+ println("test case 2")7+ }8+})9+class TestExtensionsTest : FunSpec({10+ extensions(MockExtension())11+ test("test case 1") {12+ println("test case 1")13+ }14+ test("test case 2") {15+ println("test case 2")16+ }17+})18+class TestExtensionsTest : FunSpec({19+ extensions(MockExtension())20+ test("test case 1") {21+ println("test case 1")22+ }23+ test("test case 2") {24+ println("test case 2")25+ }26+})

Full Screen

Full Screen

extensions

Using AI Code Generation

copy

Full Screen

1+class TestExtensionsTest : FunSpec() {2+ init {3+ context("test context") {4+ test("test case") {5+ testCase.extensions().size shouldBe 26+ testCase.extensions()[0] shouldBe TestExtension17+ testCase.extensions()[1] shouldBe TestExtension28+ }9+ }10+ }11+}12+class TestListenerTest : FunSpec() {13+ private val listener = object : TestListener {14+ override suspend fun beforeTest(testCase: TestCase) {15+ }16+ override suspend fun afterTest(testCase: TestCase, result: TestResult) {17+ }18+ override suspend fun afterProject() {19+ }20+ }21+ init {22+ registerListener(listener)23+ context("test context") {24+ test("test case") {25+ }26+ }27+ }28+}29+class ProjectListenerTest : FunSpec() {30+ private val listener = object : ProjectListener {31+ override suspend fun beforeProject() {32+ }33+ override suspend fun afterProject() {34+ }35+ }36+ init {37+ registerListener(listener)38+ context("test context") {39+ test("test case") {40+ }41+ }42+ }43+}

Full Screen

Full Screen

extensions

Using AI Code Generation

copy

Full Screen

1class TestExtensionsExample : FunSpec({2 extensions {3 val extension = MyTestExtension()4 register(extension)5 }6 test("test 1") {7 }8 test("test 2") {9 }10 test("test 3") {11 }12})

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.

Run Kotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in TestExtensions

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful