How to use TestExtensions class of io.kotest.engine.test package

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

TestExtensions.kt

Source:TestExtensions.kt Github

copy

Full Screen

...24import 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> {...

Full Screen

Full Screen

LifecycleInterceptor.kt

Source:LifecycleInterceptor.kt Github

copy

Full Screen

...3import io.kotest.core.test.TestCase4import io.kotest.core.test.TestResult5import io.kotest.core.test.TestScope6import io.kotest.engine.test.TestCaseExecutionListener7import io.kotest.engine.test.TestExtensions8import io.kotest.engine.test.createTestResult9import io.kotest.mpp.Logger10import kotlin.time.TimeMark11/**12 * Executes a test taking care of invoking user level listeners.13 * The test is always marked as started at this stage.14 *15 * If the before-test listeners fail, then the test is not executed, but the after-test listeners16 * are executed, and the returned result contains the listener exception.17 *18 * If the test itself fails, then the after-test listeners are executed,19 * and the returned result is generated from the test exception.20 *21 * If the after-test listeners fail, then the returned result is taken from the listener exception22 * and any result from the test itself is ignored.23 *24 * Essentially, the after-test listeners are always attempted, and any error from invoking the before, test,25 * or after code is returned as higher priority than the result from the test case itself.26 */27internal class LifecycleInterceptor(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 extensions...

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

Full Screen

Full Screen

CoroutineLoggingInterceptor.kt

Source:CoroutineLoggingInterceptor.kt Github

copy

Full Screen

...3import io.kotest.core.config.ProjectConfiguration4import io.kotest.core.test.TestCase5import io.kotest.core.test.TestResult6import io.kotest.core.test.TestScope7import io.kotest.engine.test.TestExtensions8import io.kotest.engine.test.logging.SerialLogExtension9import io.kotest.engine.test.logging.TestLogger10import io.kotest.engine.test.logging.TestScopeLoggingCoroutineContextElement11import io.kotest.engine.test.scopes.withCoroutineContext12import io.kotest.mpp.Logger13import kotlinx.coroutines.withContext14@ExperimentalKotest15internal class CoroutineLoggingInterceptor(private val configuration: ProjectConfiguration) : TestExecutionInterceptor {16 private val logger = Logger(CoroutineLoggingInterceptor::class)17 override suspend fun intercept(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 }...

Full Screen

Full Screen

TestCaseExtensionInterceptor.kt

Source:TestCaseExtensionInterceptor.kt Github

copy

Full Screen

...3import 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

TestExtensions

Using AI Code Generation

copy

Full Screen

1import io.kotest.core.spec.style.FunSpec2import io.kotest.matchers.shouldBe3import io.kotest.engine.test.TestExtensions4import io.kotest.core.test.TestCase5import io.kotest.core.test.TestResult6import io.kotest.core.extensions.Extension7import io.kotest.core.extensions.TestCaseExtension8import io.kotest.core.extensions.TestExtension9import io.kotest.core.extensions.SpecExtension10class MySpecExtension: SpecExtension {11override fun intercept(spec: Spec, execute: () -> Unit) {12println("SpecExtension called before spec execute")13execute()14println("SpecExtension called after spec execute")15}16}17class MyTestCaseExtension: TestCaseExtension {18override fun intercept(testCase: TestCase, execute: (TestCase) -> TestResult): TestResult {19println("TestCaseExtension called before test case execute")20val result = execute(testCase)21println("TestCaseExtension called after test case execute")22}23}24class MyTestExtension: TestExtension {25override fun intercept(testCase: TestCase, execute: (TestCase) -> TestResult): TestResult {26println("TestExtension called before test execute")27val result = execute(testCase)28println("TestExtension called after test execute")29}30}31class MyExtension: Extension, TestExtension, TestCaseExtension, SpecExtension {32override fun intercept(spec: Spec, execute: () -> Unit) {33println("MyExtension called before spec execute")34execute()35println("MyExtension called after spec execute")36}37override fun intercept(testCase: TestCase, execute: (TestCase) -> TestResult): TestResult {38println("MyExtension called before test case execute")39val result = execute(testCase)40println("MyExtension called after test case execute")41}42override fun intercept(testCase: TestCase, execute: (TestCase) -> TestResult): TestResult {43println("MyExtension called before test execute")44val result = execute(testCase)45println("MyExtension called after test execute")46}47}48class MySpec : FunSpec({49TestExtensions.register(MyExtension())50TestExtensions.register(MySpecExtension())51TestExtensions.register(MyTestCaseExtension())52TestExtensions.register(MyTestExtension())53test("test1") {

Full Screen

Full Screen

TestExtensions

Using AI Code Generation

copy

Full Screen

1import io.kotest.core.spec.style.FunSpec2import io.kotest.engine.test.TestExtensions3import io.kotest.matchers.shouldBe4class TestExtensionsTest : FunSpec({5 test("TestExtensions should be empty by default") {6 TestExtensions().size shouldBe 07 }8 test("TestExtensions should be able to add an extension") {9 TestExtensions().add(TestExtensionsTest::class).size shouldBe 110 }11 test("TestExtensions should be able to add multiple extensions") {12 TestExtensions()13 .add(TestExtensionsTest::class)14 .add(TestExtensionsTest::class)15 }16 test("TestExtensions should be able to remove an extension") {17 TestExtensions()18 .add(TestExtensionsTest::class)19 .remove(TestExtensionsTest::class)20 }21 test("TestExtensions should be able to remove multiple extensions") {22 TestExtensions()23 .add(TestExtensionsTest::class)24 .add(TestExtensionsTest::class)25 .remove(TestExtensionsTest::class)26 .remove(TestExtensionsTest::class)27 }28 test("TestExtensions should be able to add and remove multiple extensions") {29 TestExtensions()30 .add(TestExtensionsTest::class)31 .add(TestExtensionsTest::class)32 .add(TestExtensionsTest::class)33 .remove(TestExtensionsTest::class)34 }35 test("TestExtensions should be able to add and remove multiple extensions in any order") {36 TestExtensions()37 .add(TestExtensionsTest::class)38 .add(TestExtensionsTest::class)39 .add(TestExtensionsTest::class)40 .remove(TestExtensionsTest::class)41 .remove(TestExtensionsTest::class)42 }43 test("TestExtensions should be able to add and remove multiple extensions in any order") {44 TestExtensions()45 .add(TestExtensionsTest::class)46 .add(TestExtensionsTest::class)47 .add(TestExtensionsTest::class)48 .remove(TestExtensionsTest::class)49 .remove(TestExtensionsTest::class)50 .remove(TestExtensionsTest::class)51 }52 test("TestExtensions should be able to add and remove multiple extensions in any order") {

Full Screen

Full Screen

TestExtensions

Using AI Code Generation

copy

Full Screen

1val myTestExtension = object : TestExtension {2override fun intercept(3execute: (TestCaseContext, (TestResult) -> Unit) -> Unit,4complete: (TestResult) -> Unit5) {6println("Before test")7execute(context) {8println("After test")9complete(result)10}11}12}13val myTestEngine = TestEngineLauncher()14.addTestExtensions(myTestExtension)15myTestEngine.launch()16}17val myTestListener = object : TestListener {18override suspend fun beforeTest(testCase: TestCase) {19println("Before test")20}21override suspend fun afterTest(testCase: TestCase, result: TestResult) {22println("After test")23}24}25val myTestEngine = TestEngineLauncher()26.addTestListeners(myTestListener)27myTestEngine.launch()28}

Full Screen

Full Screen

TestExtensions

Using AI Code Generation

copy

Full Screen

1fun registerTestExtension(extension: TestExtension): Unit2fun registerTestExtensions(extensions: List<TestExtension>): Unit3fun registerTestExtensions(vararg extensions: TestExtension): Unit4fun registerTestExtensions(extensions: Set<TestExtension>): Unit5fun registerTestExtensions(extensions: Sequence<TestExtension>): Unit6fun registerTestExtensions(extensions: Iterable<TestExtension>): Unit7fun registerTestExtensions(extensions: Collection<TestExtension>): Unit8fun registerTestExtensions(extensions: Array<TestExtension>): Unit9fun registerTestExtensions(extensions: Sequence<TestExtension>): Unit10fun intercept(spec: Spec, process: () -> Unit)11fun intercept(spec: Spec, process: () -> Unit)12fun intercept(spec: Spec, process: () -> Unit)13fun intercept(spec: Spec, process: () -> Unit)14fun intercept(spec: Spec, process: () -> Unit)15fun intercept(spec: Spec, process: ()

Full Screen

Full Screen

TestExtensions

Using AI Code Generation

copy

Full Screen

1import io.kotest.core.config.Configuration2Configuration {3extensions {4register(TestExtensions())5}6}7}8class TestExtensions : TestExecutionListener {9override fun beforeTest(testCase: TestCase) {10println("Test started: ${testCase.description.name}")11}12override fun afterTest(testCase: TestCase, result: TestResult) {13println("Test finished: ${testCase.description.name}")14}15}

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 methods 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