How to use EngineContext class of io.kotest.engine.interceptors package

Best Kotest code snippet using io.kotest.engine.interceptors.EngineContext

ProjectListenerEngineInterceptorTest.kt

Source:ProjectListenerEngineInterceptorTest.kt Github

copy

Full Screen

...5import io.kotest.core.listeners.BeforeProjectListener6import io.kotest.core.spec.style.FunSpec7import io.kotest.engine.EngineResult8import io.kotest.engine.extensions.ExtensionException9import io.kotest.engine.interceptors.EngineContext10import io.kotest.engine.interceptors.ProjectListenerEngineInterceptor11import io.kotest.matchers.shouldBe12@KotestInternal13class ProjectListenerEngineInterceptorTest : FunSpec({14 test("should invoke beforeProject listener") {15 var fired = false16 val listener = object : BeforeProjectListener {17 override suspend fun beforeProject() {18 fired = true19 }20 }21 val c = ProjectConfiguration()22 c.registry.add(listener)23 ProjectListenerEngineInterceptor.intercept(24 EngineContext.empty.withConfiguration(c)25 ) { EngineResult(emptyList()) }26 fired shouldBe true27 }28 test("should invoke multiple beforeProject listeners") {29 var fired1 = false30 var fired2 = false31 val listener1 = object : BeforeProjectListener {32 override suspend fun beforeProject() {33 fired1 = true34 }35 }36 val listener2 = object : BeforeProjectListener {37 override suspend fun beforeProject() {38 fired2 = true39 }40 }41 val c = ProjectConfiguration()42 c.registry.add(listener1)43 c.registry.add(listener2)44 ProjectListenerEngineInterceptor.intercept(45 EngineContext.empty.withConfiguration(c)46 ) { EngineResult(emptyList()) }47 fired1 shouldBe true48 fired2 shouldBe true49 }50 test("should invoke afterProject listeners") {51 var fired = false52 val listener = object : AfterProjectListener {53 override suspend fun afterProject() {54 fired = true55 }56 }57 val c = ProjectConfiguration()58 c.registry.add(listener)59 ProjectListenerEngineInterceptor.intercept(60 EngineContext.empty.withConfiguration(c)61 ) { EngineResult(emptyList()) }62 fired shouldBe true63 }64 test("should invoke multiple afterProject listeners") {65 var fired1 = false66 var fired2 = false67 val listener1 = object : AfterProjectListener {68 override suspend fun afterProject() {69 fired1 = true70 }71 }72 val listener2 = object : AfterProjectListener {73 override suspend fun afterProject() {74 fired2 = true75 }76 }77 val c = ProjectConfiguration()78 c.registry.add(listener1)79 c.registry.add(listener2)80 ProjectListenerEngineInterceptor.intercept(81 EngineContext.empty.withConfiguration(c)82 ) { EngineResult(emptyList()) }83 fired1 shouldBe true84 fired2 shouldBe true85 }86 test("should return BeforeProjectListener errors wrapped in BeforeProjectListenerException") {87 val listener1 = object : BeforeProjectListener {88 override suspend fun beforeProject() {89 error("whack!")90 }91 }92 val listener2 = object : BeforeProjectListener {93 override suspend fun beforeProject() {94 error("zapp!")95 }96 }97 val c = ProjectConfiguration()98 c.registry.add(listener1)99 c.registry.add(listener2)100 val results = ProjectListenerEngineInterceptor.intercept(101 EngineContext.empty.withConfiguration(c)102 ) { EngineResult(emptyList()) }103 results.errors.filterIsInstance<ExtensionException.BeforeProjectException>().size shouldBe 2104 }105 test("should return AfterProjectListener errors wrapped in AfterProjectListenerException") {106 val listener1 = object : AfterProjectListener {107 override suspend fun afterProject() {108 error("whack!")109 }110 }111 val listener2 = object : AfterProjectListener {112 override suspend fun afterProject() {113 error("zapp!")114 }115 }116 val c = ProjectConfiguration()117 c.registry.add(listener1)118 c.registry.add(listener2)119 val results = ProjectListenerEngineInterceptor.intercept(120 EngineContext.empty.withConfiguration(c)121 ) { EngineResult(emptyList()) }122 results.errors.filterIsInstance<ExtensionException.AfterProjectException>().size shouldBe 2123 }124})...

Full Screen

Full Screen

ProjectExtensionEngineInterceptorTest.kt

Source:ProjectExtensionEngineInterceptorTest.kt Github

copy

Full Screen

...5import io.kotest.core.project.ProjectContext6import io.kotest.core.extensions.ProjectExtension7import io.kotest.core.spec.style.FunSpec8import io.kotest.engine.EngineResult9import io.kotest.engine.interceptors.EngineContext10import io.kotest.engine.interceptors.ProjectExtensionEngineInterceptor11import io.kotest.matchers.shouldBe12@KotestInternal13class ProjectExtensionEngineInterceptorTest : FunSpec({14 test("should invoke all extensions") {15 var fired1 = false16 var fired2 = false17 val ext1 = object : ProjectExtension {18 override suspend fun interceptProject(context: ProjectContext, callback: suspend (ProjectContext) -> Unit) {19 fired1 = true20 callback(context)21 }22 }23 val ext2 = object : ProjectExtension {24 override suspend fun interceptProject(context: ProjectContext, callback: suspend (ProjectContext) -> Unit) {25 fired2 = true26 callback(context)27 }28 }29 val c = ProjectConfiguration()30 c.registry.add(ext1)31 c.registry.add(ext2)32 ProjectExtensionEngineInterceptor.intercept(EngineContext.empty.withConfiguration(c)) { EngineResult.empty }33 fired1 shouldBe true34 fired2 shouldBe true35 }36 test("should invoke downstream after extensions") {37 var fired = false38 val ext1 = object : ProjectExtension {39 override suspend fun interceptProject(context: ProjectContext, callback: suspend (ProjectContext) -> Unit) {40 callback(context)41 }42 }43 val ext2 = object : ProjectExtension {44 override suspend fun interceptProject(context: ProjectContext, callback: suspend (ProjectContext) -> Unit) {45 callback(context)46 }47 }48 val c = ProjectConfiguration()49 c.registry.add(ext1)50 c.registry.add(ext2)51 ProjectExtensionEngineInterceptor.intercept(EngineContext.empty.withConfiguration(c)) {52 fired = true53 EngineResult.empty54 }55 fired shouldBe true56 }57 test("should invoke downstream without extensions") {58 var fired = false59 ProjectExtensionEngineInterceptor.intercept(EngineContext.empty) {60 fired = true61 EngineResult.empty62 }63 fired shouldBe true64 }65 test("should propagate tag changes") {66 var tags = TagExpression("none")67 val ext = object : ProjectExtension {68 override suspend fun interceptProject(context: ProjectContext, callback: suspend (ProjectContext) -> Unit) {69 callback(context.copy(tags = context.tags.include("bar")))70 }71 }72 val c = ProjectConfiguration()73 c.registry.add(ext)74 ProjectExtensionEngineInterceptor.intercept(EngineContext.empty.withTags(TagExpression("foo")).withConfiguration(c)) {75 tags = it.tags76 EngineResult.empty77 }78 tags.expression shouldBe "foo & bar"79 }80})...

Full Screen

Full Screen

TestEngine.kt

Source:TestEngine.kt Github

copy

Full Screen

...5import io.kotest.common.platform6import io.kotest.core.TagExpression7import io.kotest.core.config.ProjectConfiguration8import io.kotest.core.project.TestSuite9import io.kotest.engine.interceptors.EngineContext10import io.kotest.engine.interceptors.EngineInterceptor11import io.kotest.engine.listener.TestEngineListener12import io.kotest.engine.tags.runtimeTags13import io.kotest.mpp.Logger14data class EngineResult(val errors: List<Throwable>) {15 companion object {16 val empty = EngineResult(emptyList())17 }18 fun addError(t: Throwable): EngineResult {19 return EngineResult(errors + t)20 }21}22@KotestInternal23data class TestEngineConfig(24 val listener: TestEngineListener,25 val interceptors: List<EngineInterceptor>,26 val configuration: ProjectConfiguration,27 val explicitTags: TagExpression?,28)29/**30 * Multiplatform Kotest Test Engine.31 */32@KotestInternal33class TestEngine(private val config: TestEngineConfig) {34 private val logger = Logger(this::class)35 /**36 * Starts execution of the given [TestSuite], intercepting calls via [EngineInterceptor]s.37 *38 * It is recommended that this method is not invoked, but instead the engine39 * is launched via the [TestEngineLauncher].40 */41 @OptIn(KotestInternal::class, ExperimentalKotest::class)42 internal suspend fun execute(suite: TestSuite): EngineResult {43 logger.log { Pair(null, "Executing test suite with ${suite.specs.size} specs") }44 val innerExecute: suspend (EngineContext) -> EngineResult = { context ->45 val scheduler = when (platform) {46 Platform.JVM -> ConcurrentTestSuiteScheduler(47 config.configuration.concurrentSpecs ?: config.configuration.parallelism,48 context,49 )50 Platform.JS -> SequentialTestSuiteScheduler(context)51 Platform.Native -> SequentialTestSuiteScheduler(context)52 }53 scheduler.schedule(context.suite)54 }55 logger.log { Pair(null, "${config.interceptors.size} engine interceptors") }56 val execute = config.interceptors.foldRight(innerExecute) { extension, next ->57 { context -> extension.intercept(context, next) }58 }59 val tags = config.configuration.runtimeTags()60 logger.log { Pair(null, "TestEngine: Active tags: ${tags.expression}") }61 return execute(EngineContext(suite, config.listener, tags, config.configuration))62 }63}...

Full Screen

Full Screen

EmptyTestSuiteInterceptorTest.kt

Source:EmptyTestSuiteInterceptorTest.kt Github

copy

Full Screen

...12import io.kotest.core.test.TestType13import io.kotest.engine.EngineResult14import io.kotest.engine.interceptors.EmptyTestSuiteException15import io.kotest.engine.interceptors.EmptyTestSuiteInterceptor16import io.kotest.engine.interceptors.EngineContext17import io.kotest.matchers.collections.shouldHaveSize18import kotlin.time.Duration19@ExperimentalKotest20@KotestInternal21class EmptyTestSuiteInterceptorTest : FunSpec() {22 init {23 test("should error on empty test suite") {24 val conf = ProjectConfiguration()25 conf.failOnEmptyTestSuite = true26 val result =27 EmptyTestSuiteInterceptor.intercept(EngineContext.empty.withConfiguration(conf)) { EngineResult.empty }28 result.errors.filterIsInstance<EmptyTestSuiteException>().shouldHaveSize(1)29 }30 test("should not error on non empty test suite") {31 val tc = TestCase(32 EmptyTestSuiteInterceptorTest::class.toDescriptor().append("foo"),33 TestName("foo"),34 EmptyTestSuiteInterceptorTest(),35 {},36 sourceRef(),37 TestType.Test38 )39 val conf = ProjectConfiguration()40 conf.failOnEmptyTestSuite = true41 val result = EmptyTestSuiteInterceptor.intercept(42 EngineContext.empty.withConfiguration(conf)43 ) {44 it.listener.testFinished(tc, TestResult.Success(Duration.ZERO))45 EngineResult.empty46 }47 result.errors.filterIsInstance<EmptyTestSuiteException>().shouldHaveSize(0)48 }49 }50}...

Full Screen

Full Screen

TestEngineStartedFinishedInterceptorTest.kt

Source:TestEngineStartedFinishedInterceptorTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.engine.interceptors2import io.kotest.common.KotestInternal3import io.kotest.core.spec.style.FunSpec4import io.kotest.engine.EngineResult5import io.kotest.engine.interceptors.EngineContext6import io.kotest.engine.interceptors.TestEngineStartedFinishedInterceptor7import io.kotest.engine.listener.AbstractTestEngineListener8import io.kotest.matchers.collections.shouldHaveSize9import io.kotest.matchers.shouldBe10@KotestInternal11class TestEngineStartedFinishedInterceptorTest : FunSpec({12 test("should invoke engineStarted before downstream") {13 var fired = ""14 val listener = object : AbstractTestEngineListener() {15 override suspend fun engineStarted() {16 fired += "a"17 }18 }19 TestEngineStartedFinishedInterceptor.intercept(20 EngineContext.empty.mergeListener(listener)21 ) {22 fired += "b"23 EngineResult.empty24 }25 fired.shouldBe("ab")26 }27 test("should invoke engineFinished after downstream") {28 var fired = ""29 val listener = object : AbstractTestEngineListener() {30 override suspend fun engineFinished(t: List<Throwable>) {31 fired += "a"32 }33 }34 TestEngineStartedFinishedInterceptor.intercept(35 EngineContext.empty.mergeListener(listener)36 ) {37 fired += "b"38 EngineResult(listOf(Exception("foo")))39 }40 fired.shouldBe("ba")41 }42 test("should invoke engineFinished with errors") {43 var errors = emptyList<Throwable>()44 val listener = object : AbstractTestEngineListener() {45 override suspend fun engineFinished(t: List<Throwable>) {46 errors = t47 }48 }49 TestEngineStartedFinishedInterceptor.intercept(50 EngineContext.empty.mergeListener(listener)51 ) { EngineResult(listOf(Exception("foo"))) }52 errors.shouldHaveSize(1)53 }54})...

Full Screen

Full Screen

SpecSortEngineInterceptorTest.kt

Source:SpecSortEngineInterceptorTest.kt Github

copy

Full Screen

...8import io.kotest.core.spec.SpecExecutionOrder9import io.kotest.core.spec.SpecRef10import io.kotest.core.spec.style.FunSpec11import io.kotest.engine.EngineResult12import io.kotest.engine.interceptors.EngineContext13import io.kotest.engine.interceptors.SpecSortEngineInterceptor14import io.kotest.matchers.shouldBe15@Isolate16class SpecSortEngineInterceptorTest : FunSpec({17 test("should sort classes") {18 val c = ProjectConfiguration()19 c.specExecutionOrder = SpecExecutionOrder.Lexicographic20 var sorted = emptyList<SpecRef>()21 SpecSortEngineInterceptor.intercept(22 EngineContext.empty.withConfiguration(c).withTestSuite(23 TestSuite(24 listOf(25 SpecRef.Reference(IgnoredTestsTest::class),26 SpecRef.Reference(BangDisableFunSpec::class),27 SpecRef.Reference(FocusTest::class),28 )29 )30 )31 ) {32 sorted = it.suite.specs33 EngineResult(emptyList())34 }35 sorted.map { it.kclass } shouldBe listOf(BangDisableFunSpec::class, FocusTest::class, IgnoredTestsTest::class)36 }...

Full Screen

Full Screen

ProjectTimeoutEngineInterceptorTest.kt

Source:ProjectTimeoutEngineInterceptorTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.engine.interceptors2import io.kotest.core.config.ProjectConfiguration3import io.kotest.core.spec.style.FunSpec4import io.kotest.engine.EngineResult5import io.kotest.engine.interceptors.EngineContext6import io.kotest.engine.interceptors.ProjectTimeoutEngineInterceptor7import io.kotest.engine.interceptors.ProjectTimeoutException8import io.kotest.matchers.shouldBe9import io.kotest.matchers.types.shouldBeInstanceOf10import kotlinx.coroutines.delay11import kotlin.time.Duration.Companion.milliseconds12class ProjectTimeoutEngineInterceptorTest : FunSpec({13 test("should return ProjectTimeoutException when project times out") {14 val c = ProjectConfiguration()15 c.projectTimeout = 1.milliseconds16 val result = ProjectTimeoutEngineInterceptor.intercept(EngineContext.empty.withConfiguration(c)) {17 delay(1000)18 EngineResult.empty19 }20 result.errors.size shouldBe 121 result.errors.first().shouldBeInstanceOf<ProjectTimeoutException>()22 }23 test("should not return ProjectTimeoutException when project does not time out") {24 val c = ProjectConfiguration()25 c.projectTimeout = 100000.milliseconds26 val result = ProjectTimeoutEngineInterceptor.intercept(EngineContext.empty.withConfiguration(c)) {27 delay(1)28 EngineResult.empty29 }30 result.errors.size shouldBe 031 }32})...

Full Screen

Full Screen

TestEngineInitializeInterceptorTest.kt

Source:TestEngineInitializeInterceptorTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.engine.interceptors2import io.kotest.core.spec.style.FunSpec3import io.kotest.engine.EngineResult4import io.kotest.engine.interceptors.EngineContext5import io.kotest.engine.interceptors.TestEngineInitializedInterceptor6import io.kotest.engine.listener.AbstractTestEngineListener7import io.kotest.matchers.booleans.shouldBeTrue8class TestEngineInitializeInterceptorTest : FunSpec({9 test("should invoke initialize") {10 var fired = false11 val listener = object : AbstractTestEngineListener() {12 override suspend fun engineInitialized(context: EngineContext) {13 fired = true14 }15 }16 TestEngineInitializedInterceptor.intercept(17 EngineContext.empty.mergeListener(listener)18 ) { EngineResult.empty }19 fired.shouldBeTrue()20 }21})...

Full Screen

Full Screen

EngineContext

Using AI Code Generation

copy

Full Screen

1val engineContext = EngineContext(configuration)2val engineExecutionListener = EngineExecutionListener(configuration)3val engineTestExecutionListener = EngineTestExecutionListener(configuration)4val testEngineListener = TestEngineListener(configuration)5val testEngineListener = TestEngineListener(configuration)6val testEngineListener = TestEngineListener(configuration)7val testEngineListener = TestEngineListener(configuration)8val testEngineListener = TestEngineListener(configuration)9val testEngineListener = TestEngineListener(configuration)10val testEngineListener = TestEngineListener(configuration)11val testEngineListener = TestEngineListener(configuration)12val testEngineListener = TestEngineListener(configuration)13val testEngineListener = TestEngineListener(configuration)14val testEngineListener = TestEngineListener(configuration)15val testEngineListener = TestEngineListener(configuration)

Full Screen

Full Screen

EngineContext

Using AI Code Generation

copy

Full Screen

1class MyEngineContextInterceptor : EngineInterceptor {2 override suspend fun intercept(spec: Spec, process: suspend (Spec) -> Unit) {3 val engineContext = EngineContext.current()4 process(spec)5 }6}7class MySpecContextInterceptor : EngineInterceptor {8 override suspend fun intercept(spec: Spec, process: suspend (Spec) -> Unit) {9 val specContext = SpecContext.current()10 process(spec)11 }12}13class MyTestCaseContextInterceptor : EngineInterceptor {14 override suspend fun intercept(spec: Spec, process: suspend (Spec) -> Unit) {15 val testCaseContext = TestCaseContext.current()16 process(spec)17 }18}19class MyTestContextInterceptor : EngineInterceptor {20 override suspend fun intercept(spec: Spec, process: suspend (Spec) -> Unit) {21 val testContext = TestContext.current()22 process(spec)23 }24}25class MyTestResultContextInterceptor : EngineInterceptor {26 override suspend fun intercept(spec: Spec, process: suspend (Spec) -> Unit) {27 val testResultContext = TestResultContext.current()28 process(spec)29 }30}31class MySpecInstantiationContextInterceptor : EngineInterceptor {32 override suspend fun intercept(spec: Spec, process: suspend (Spec) -> Unit) {33 val specInstantiationContext = SpecInstantiationContext.current()34 process(spec)35 }36}

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful