Best Kotest code snippet using io.kotest.engine.TestEngineLauncher.launch
ExtensionErrorsTest.kt
Source:ExtensionErrorsTest.kt
...15 test("beforeSpec function overrides should be wrapped") {16 val collector = CollectingTestEngineListener()17 TestEngineLauncher(collector)18 .withClasses(BeforeSpecFunctionOverrideError::class)19 .launch()20 val error = collector.specs.values.first().errorOrNull21 error.shouldBeInstanceOf<ExtensionException.BeforeSpecException>()22 }23 test("beforeSpec DSL errors should be wrapped") {24 val collector = CollectingTestEngineListener()25 TestEngineLauncher(collector)26 .withClasses(BeforeSpecDSLError::class)27 .launch()28 val error = collector.specs.values.first().errorOrNull29 error.shouldBeInstanceOf<ExtensionException.BeforeSpecException>()30 }31 test("multiple beforeSpec should be collected") {32 val collector = CollectingTestEngineListener()33 TestEngineLauncher(collector)34 .withClasses(MultipleBeforeSpecErrors::class)35 .launch()36 val error = collector.specs.values.first().errorOrNull37 error.shouldBeInstanceOf<MultipleExceptions>()38 error.causes.shouldHaveSize(3)39 error.causes.forAll { it.shouldBeInstanceOf<ExtensionException.BeforeSpecException>() }40 }41 test("afterSpec function overrides should be wrapped") {42 val collector = CollectingTestEngineListener()43 TestEngineLauncher(collector)44 .withClasses(AfterSpecFunctionOverrideError::class)45 .launch()46 val error = collector.specs.values.first().errorOrNull47 error.shouldBeInstanceOf<ExtensionException.AfterSpecException>()48 }49 test("afterSpec DSL errors should be wrapped") {50 val collector = CollectingTestEngineListener()51 TestEngineLauncher(collector)52 .withClasses(AfterSpecDSLError::class)53 .launch()54 val error = collector.specs.values.first().errorOrNull55 error.shouldBeInstanceOf<ExtensionException.AfterSpecException>()56 }57 test("multiple afterSpec should be collected") {58 val collector = CollectingTestEngineListener()59 TestEngineLauncher(collector)60 .withClasses(MultipleAfterSpecErrors::class)61 .launch()62 val error = collector.specs.values.first().errorOrNull63 error.shouldBeInstanceOf<MultipleExceptions>()64 error.causes.shouldHaveSize(3)65 error.causes.forAll { it.shouldBeInstanceOf<ExtensionException.AfterSpecException>() }66 }67 test("beforeTest function overrides should be wrapped") {68 val collector = CollectingTestEngineListener()69 TestEngineLauncher(collector)70 .withClasses(BeforeTestFunctionOverrideError::class)71 .launch()72 val error = collector.tests.values.first().errorOrNull73 error.shouldBeInstanceOf<ExtensionException.BeforeTestException>()74 }75 test("beforeTest DSL errors should be wrapped") {76 val collector = CollectingTestEngineListener()77 TestEngineLauncher(collector)78 .withClasses(BeforeTestDSLError::class)79 .launch()80 val error = collector.tests.values.first().errorOrNull81 error.shouldBeInstanceOf<ExtensionException.BeforeTestException>()82 }83 test("multiple beforeTest errors should be collected") {84 val collector = CollectingTestEngineListener()85 TestEngineLauncher(collector)86 .withClasses(MultipleBeforeTestErrors::class)87 .launch()88 val error = collector.tests.values.first().errorOrNull89 error.shouldBeInstanceOf<MultipleExceptions>()90 error.causes.shouldHaveSize(3)91 error.causes.forAll { it.shouldBeInstanceOf<ExtensionException.BeforeTestException>() }92 }93 test("afterTest function overrides should be wrapped") {94 val collector = CollectingTestEngineListener()95 TestEngineLauncher(collector)96 .withClasses(AfterTestFunctionOverrideError::class)97 .launch()98 val error = collector.tests.values.first().errorOrNull99 error.shouldBeInstanceOf<ExtensionException.AfterTestException>()100 }101 test("afterTest DSL errors should be wrapped") {102 val collector = CollectingTestEngineListener()103 TestEngineLauncher(collector)104 .withClasses(AfterTestDSLError::class)105 .launch()106 val error = collector.tests.values.first().errorOrNull107 error.shouldBeInstanceOf<ExtensionException.AfterTestException>()108 }109 test("multiple afterTest errors should be collected") {110 val collector = CollectingTestEngineListener()111 TestEngineLauncher(collector)112 .withClasses(MultipleAfterTestErrors::class)113 .launch()114 val error = collector.tests.values.first().errorOrNull115 error.shouldBeInstanceOf<MultipleExceptions>()116 error.causes.shouldHaveSize(3)117 error.causes.forAll { it.shouldBeInstanceOf<ExtensionException.AfterTestException>() }118 }119 }120}121private class BeforeSpecFunctionOverrideError : FunSpec() {122 override suspend fun beforeSpec(spec: Spec) {123 error("foo")124 }125 init {126 test("a") { }127 }...
TestEngineLauncher.kt
Source:TestEngineLauncher.kt
...112 tagExpression = expression,113 )114 }115 /**116 * Returns a copy of this launcher with the given [extensions] added to the configuration.117 *118 * Note: If after invoking this method, the [withConfiguration] is invoked, then any changes119 * here will be lost.120 */121 fun withExtensions(vararg extensions: Extension): TestEngineLauncher = withExtensions(extensions.toList())122 /**123 * Returns a copy of this launcher with the given [extensions] added to the configuration.124 *125 * Note: If after invoking this method, the [withConfiguration] is invoked, then any changes126 * here will be lost.127 */128 fun withExtensions(extensions: List<Extension>): TestEngineLauncher {129 extensions.forEach { projectConfiguration.registry.add(it) }130 return this131 }132 fun withConfiguration(configuration: ProjectConfiguration): TestEngineLauncher {133 return TestEngineLauncher(134 listener = listener,135 projectConfiguration = configuration,136 configs = configs,137 refs = refs,138 tagExpression = tagExpression,139 )140 }141 fun toConfig(): TestEngineConfig {142 // if the engine was configured with explicit tags, we register those via a tag extension143 tagExpression?.let { projectConfiguration.registry.add(SpecifiedTagsTagExtension(it)) }144 return TestEngineConfig(145 listener = ThreadSafeTestEngineListener(146 PinnedSpecTestEngineListener(147 listener148 )149 ),150 interceptors = testEngineInterceptors(),151 configuration = ConfigManager.initialize(152 projectConfiguration,153 configs + detectAbstractProjectConfigs() + listOfNotNull(loadProjectConfigFromClassname())154 ),155 tagExpression,156 )157 }158 fun testSuite(): TestSuite = TestSuite(refs)159 /**160 * Launch the [TestEngine] in an existing coroutine without blocking.161 */162 suspend fun async(): EngineResult {163 log { "TestEngineLauncher: Launching Test Engine" }164 val engine = TestEngine(toConfig())165 return engine.execute(testSuite())166 }167 /**168 * Launch the [TestEngine] created from this builder and block the thread until execution has completed.169 * This method will throw on JS.170 */171 fun launch(): EngineResult {172 log { "TestEngineLauncher: Launching Test Engine" }173 return runBlocking {174 val engine = TestEngine(toConfig())175 engine.execute(testSuite())176 }177 }178 /**179 * Launch the [TestEngine] created from this builder using a Javascript promise.180 * This method will throw on JVM or native.181 */182 fun promise() {183 log { "TestEngineLauncher: Launching Test Engine in Javascript promise" }184 runPromise {185 val engine = TestEngine(toConfig())...
SystemPropertyFiltersTests.kt
Source:SystemPropertyFiltersTests.kt
...46 mapOf(47 KotestEngineProperties.filterSpecs to "",48 KotestEngineProperties.filterTests to ""49 )50 ) { TestEngineLauncher().withClasses(testSuite).withConfiguration(ProjectConfiguration()).launch() }51 numberOfTestsRunShouldBe(13)52 }53 test("filters a specific class") {54 withSystemProperties(55 mapOf(56 KotestEngineProperties.filterSpecs to "*DistantFutureSciFiTests",57 KotestEngineProperties.filterTests to ""58 )59 ) { TestEngineLauncher().withClasses(testSuite).withConfiguration(ProjectConfiguration()).launch() }60 numberOfTestsRunShouldBe(7)61 }62 test("filters a class prefix") {63 withSystemProperties(64 mapOf(65 KotestEngineProperties.filterSpecs to "*FutureSciFiTests",66 KotestEngineProperties.filterTests to ""67 )68 ) { TestEngineLauncher().withClasses(testSuite).withConfiguration(ProjectConfiguration()).launch() }69 numberOfTestsRunShouldBe(9)70 }71 test("filters a specific class and test") {72 withSystemProperties(73 mapOf(74 KotestEngineProperties.filterSpecs to "*NearFutureSciFiTests",75 KotestEngineProperties.filterTests to "Daedalus*"76 )77 ) { TestEngineLauncher().withClasses(testSuite).withConfiguration(ProjectConfiguration()).launch() }78 numberOfTestsRunShouldBe(1)79 }80 test("filters a test name with spaces") {81 withSystemProperties(82 mapOf(83 KotestEngineProperties.filterSpecs to "",84 KotestEngineProperties.filterTests to "trek tests*"85 )86 ) { TestEngineLauncher().withClasses(testSuite).withConfiguration(ProjectConfiguration()).launch() }87 numberOfTestsRunShouldBe(3)88 }89 test("filters all classes in a package") {90 withSystemProperties(91 mapOf(92 KotestEngineProperties.filterSpecs to "com.sksamuel.kotest.engine.interceptors.filters1.*",93 KotestEngineProperties.filterTests to ""94 )95 ) { TestEngineLauncher().withClasses(testSuite).withConfiguration(ProjectConfiguration()).launch() }96 numberOfTestsRunShouldBe(2)97 }98 test("filters nested tests in a context") {99 withSystemProperties(100 mapOf(101 KotestEngineProperties.filterSpecs to "",102 KotestEngineProperties.filterTests to "expanse tests*"103 )104 ) { TestEngineLauncher().withClasses(testSuite).withConfiguration(ProjectConfiguration()).launch() }105 numberOfTestsRunShouldBe(4)106 }107 test("filter tests using prefix and suffix wildcard") {108 withSystemProperties(109 mapOf(110 KotestEngineProperties.filterSpecs to "",111 KotestEngineProperties.filterTests to "*anse tes*"112 )113 ) { TestEngineLauncher().withClasses(testSuite).withConfiguration(ProjectConfiguration()).launch() }114 numberOfTestsRunShouldBe(4)115 }116 test("filter tests with prefix wildcard") {117 withSystemProperties(118 mapOf(119 KotestEngineProperties.filterSpecs to "",120 KotestEngineProperties.filterTests to "*BC-304"121 )122 ) { TestEngineLauncher().withClasses(testSuite).withConfiguration(ProjectConfiguration()).launch() }123 numberOfTestsRunShouldBe(2)124 testsRunShouldBe("Daedalus BC-304", "Odyssey BC-304")125 }126})127private class DistantFutureSciFiTests : FunSpec({128 context("trek tests") {129 test("Enterprise NCC-1701") { testAndIncrementCounter() }130 test("Excelsior NCC-2000") { testAndIncrementCounter() }131 test("Defiant NX-74205") { testAndIncrementCounter() }132 }133 context("expanse tests") {134 test("MCRN Donnager") { testAndIncrementCounter() }135 test("Rocinante") { testAndIncrementCounter() }136 test("UNN Arboghast") { testAndIncrementCounter() }...
IgnoredTestReasonTest.kt
Source:IgnoredTestReasonTest.kt
...14 test("enabledOrReasonIf should report the reason for skipping") {15 val collector = CollectingTestEngineListener()16 TestEngineLauncher(collector)17 .withClasses(EnabledOrReasonIfSpec::class)18 .launch()19 collector.tests.toList().first().second.reasonOrNull shouldBe "wobble"20 }21 test("EnabledExtension should report the reason for skipping") {22 val ext = object : EnabledExtension {23 override suspend fun isEnabled(descriptor: Descriptor): Enabled = Enabled.disabled("wibble")24 }25 val c = ProjectConfiguration().apply { registry.add(ext) }26 val collector = CollectingTestEngineListener()27 TestEngineLauncher(collector)28 .withClasses(MyFunSpec::class)29 .withConfiguration(c)30 .launch()31 collector.tests.toList().first().second.reasonOrNull shouldBe "wibble"32 }33 test("xdisabled in fun spec should report the reason for skipping") {34 val collector = CollectingTestEngineListener()35 TestEngineLauncher(collector)36 .withClasses(XReasonFunSpec::class)37 .launch()38 collector.tests.toList().first().second.reasonOrNull shouldBe "Disabled by xmethod"39 }40 test("xdisabled in describe spec should report the reason for skipping") {41 val collector = CollectingTestEngineListener()42 TestEngineLauncher(collector)43 .withClasses(XReasonDescribeSpec::class)44 .launch()45 collector.tests.toList().first().second.reasonOrNull shouldBe "Disabled by xmethod"46 }47 test("xdisabled in should spec should report the reason for skipping") {48 val collector = CollectingTestEngineListener()49 TestEngineLauncher(collector)50 .withClasses(XReasonShouldSpec::class)51 .launch()52 collector.tests.toList().first().second.reasonOrNull shouldBe "Disabled by xmethod"53 }54 test("enabled should report some reason for skipping") {55 val collector = CollectingTestEngineListener()56 TestEngineLauncher(collector)57 .withClasses(EnabledSpec::class)58 .launch()59 collector.tests.toList().first().second.reasonOrNull shouldBe "Disabled by enabled flag in config"60 }61 test("enabledIf should report some reason for skipping") {62 val collector = CollectingTestEngineListener()63 TestEngineLauncher(collector)64 .withClasses(EnabledIfSpec::class)65 .launch()66 collector.tests.toList().first().second.reasonOrNull shouldBe "Disabled by enabledIf flag in config"67 }68 test("bang should report some reason for skipping") {69 val collector = CollectingTestEngineListener()70 TestEngineLauncher(collector)71 .withClasses(BangSpec::class)72 .launch()73 collector.tests.toList().first().second.reasonOrNull shouldBe "Disabled by bang"74 }75 }76}77private class BangSpec : FunSpec() {78 init {79 test("!a") {80 throw RuntimeException()81 }82 }83}84private class EnabledSpec : FunSpec() {85 init {86 test("a").config(enabled = false) {...
ActionTest.kt
Source:ActionTest.kt
...106 ActionTestSpec(actionTestData.action, reportScreen)107 }, ActionTestSpec::class)108 TestEngineLauncher(collector, projectConfiguration, emptyList(), listOf(ref), null)109 .withExtensions()110 .launch()111 Then("check test name") {112 collector.result(actionTestData.name)?.isSuccess shouldBe true113 }114 Then("check report", actionTestData.reportScreenNumberCheck)115 Then("check model", actionTestData.reportScreenModelCheck)116 }117 }118 }119})...
launcher.kt
Source:launcher.kt
1package launcher2import io.kotest.core.TagExpression3import io.kotest.core.descriptors.Descriptor4import io.kotest.core.descriptors.append5import io.kotest.core.descriptors.toDescriptor6import io.kotest.core.filter.TestFilter7import io.kotest.core.filter.TestFilterResult8import io.kotest.core.spec.Spec9import io.kotest.engine.TestEngineLauncher10import io.kotest.engine.listener.TestEngineListener11import io.kotest.framework.discovery.Discovery12import io.kotest.framework.discovery.DiscoveryRequest13import io.kotest.framework.discovery.DiscoveryResult14import io.kotest.framework.discovery.DiscoverySelector15import kotlin.reflect.KClass16/**17 * Creates a [TestEngineLauncher] to be used to launch the test engine.18 */19internal fun setupLauncher(20 args: LauncherArgs,21 listener: TestEngineListener,22): Result<TestEngineLauncher> = runCatching {23 val specClass = args.spec?.let { (Class.forName(it) as Class<Spec>).kotlin }24 val (specs, _, error) = specs(specClass, args.packageName)25 val filter = if (args.testpath == null || specClass == null) null else {26 TestPathTestCaseFilter(args.testpath, specClass)27 }28 if (error != null) throw error29 TestEngineLauncher(listener)30 .withExtensions(listOfNotNull(filter))31 .withTagExpression(args.tagExpression?.let { TagExpression(it) })32 .withClasses(specs)33}34/**35 * Returns the spec classes to execute by using an FQN class name, a package scan,36 * or a full scan.37 */38private fun specs(specClass: KClass<out Spec>?, packageName: String?): DiscoveryResult {39 // if the spec class was null, then we perform discovery to locate all the classes40 // otherwise that specific spec class is used41 return when (specClass) {42 null -> scan(packageName)43 else -> DiscoveryResult(listOf(specClass), emptyList(), null)44 }45}46private fun scan(packageName: String?): DiscoveryResult {47 val packageSelector = packageName?.let { DiscoverySelector.PackageDiscoverySelector(it) }48 val req = DiscoveryRequest(selectors = listOfNotNull(packageSelector))49 val discovery = Discovery(emptyList())50 return discovery.discover(req)51}52/**53 * Compares test descriptions to a given test path (delimited with ' -- ').54 * The comparison ignores test prefixes, so an application using the launcher should not55 * include test name prefixes in the test path.56 */57private class TestPathTestCaseFilter(58 private val testPath: String,59 spec: KClass<out Spec>,60) : TestFilter {61 private val target1 = testPath.trim().split(Descriptor.TestDelimiter)62 .fold(spec.toDescriptor() as Descriptor) { desc, name ->63 desc.append(name.trim())64 }65 // this is a hack where we append "should" to the first name, until 5.0 where we will66 // store names with affixes separately (right now word spec is adding them to the names at source)67 var should = true68 private val target2 = testPath.trim().split(Descriptor.TestDelimiter)...
UnfinishedTestDefinitionTest.kt
Source:UnfinishedTestDefinitionTest.kt
...16 }17 test("fun spec") {18 val result = TestEngineLauncher(NoopTestEngineListener)19 .withClasses(FunSpecUnfinishedTestDefinitionTest::class)20 .launch()21 result.errors.forAtLeastOne { it.message!!.shouldContain("unfinished test") }22 }23 test("describe spec") {24 val result = TestEngineLauncher(NoopTestEngineListener)25 .withClasses(DescribeSpecUnfinishedTestDefinitionTest::class)26 .launch()27 result.errors.forAtLeastOne { it.message!!.shouldContain("unfinished it") }28 }29 test("should spec") {30 val result = TestEngineLauncher(NoopTestEngineListener)31 .withClasses(ShouldSpecUnfinishedTestDefinitionTest::class)32 .launch()33 result.errors.forAtLeastOne { it.message!!.shouldContain("unfinished should") }34 }35 test("feature spec") {36 val result = TestEngineLauncher(NoopTestEngineListener)37 .withClasses(FeatureSpecUnfinishedTestDefinitionTest::class)38 .launch()39 result.errors.forAtLeastOne { it.message!!.shouldContain("unfinished scenario") }40 }41 test("expect spec") {42 val result = TestEngineLauncher(NoopTestEngineListener)43 .withClasses(ExpectSpecUnfinishedTestDefinitionTest::class)44 .launch()45 result.errors.forAtLeastOne { it.message!!.shouldContain("unfinished expect") }46 }47 }48}49private class FunSpecUnfinishedTestDefinitionTest : FunSpec({50 context("context") {51 test("unfinished test")52 }53})54private class FeatureSpecUnfinishedTestDefinitionTest : FeatureSpec({55 feature("feature") {56 scenario("unfinished scenario")57 }58})...
KotestUnit.kt
Source:KotestUnit.kt
...32 }33 TestEngineLauncher()34 .withListener(listener)35 .withClasses(klass)36 .launch()37 }38}...
launch
Using AI Code Generation
1TestEngineLauncher("io.kotest.engine.KotestEngine").withSpecs(specs).launch()2TestEngineLauncher("io.kotest.engine.KotestEngine").withSpecs(specs).launch()3TestEngineLauncher("io.kotest.engine.KotestEngine").withSpecs(specs).launch()4TestEngineLauncher("io.kotest.engine.KotestEngine").withSpecs(specs).launch()5TestEngineLauncher("io.kotest.engine.KotestEngine").withSpecs(specs).launch()6TestEngineLauncher("io.kotest.engine.KotestEngine").withSpecs(specs).launch()7TestEngineLauncher("io.kotest.engine.KotestEngine").withSpecs(specs).launch()8TestEngineLauncher("io.kotest.engine.KotestEngine").withSpecs(specs).launch()9TestEngineLauncher("io.kotest.engine.KotestEngine").withSpecs(specs).launch()10TestEngineLauncher("io.kotest.engine.KotestEngine").withSpecs(specs).launch()11TestEngineLauncher("io.kotest.engine.KotestEngine").withSpecs(specs).launch()12TestEngineLauncher("io.kotest.engine.KotestEngine").withSpecs(specs).launch()13TestEngineLauncher("io.kotest.engine.KotestEngine").withSpecs(specs).launch()
launch
Using AI Code Generation
1 val launcher = TestEngineLauncher()2 launcher.withArguments(args)3 launcher.execute()4 val launcher = TestEngineLauncher()5 launcher.withArguments(args)6 launcher.execute(object : TestEngineListener {7 override fun engineStarted(classes: List<KClass<out Spec>>) {8 println("Starting Kotest")9 }10 override fun engineFinished(t: List<Throwable>) {11 println("Finished Kotest")12 }13 })14 }15}
launch
Using AI Code Generation
1import io.kotest.core.config.configuration2import io.kotest.engine.TestEngineLauncher3import io.kotest.engine.config.EngineConfiguration4import io.kotest.engine.config.engineConfiguration5import io.kotest.engine.config.writeSpecFailureFile6import io.kotest.engine.listener.PrintStreamTestEngineListener7import io.kotest.engine.listener.TestEngineListener8import io.kotest.engine.listener.TestEngineListenerErrorAdapter9import io.kotest.engine.spec.SpecExecutor10import io.kotest.engine.test.TestCaseExecutor11import io.kotest.engine.test.interceptors.TestCaseExecutionInterceptor12import io.kotest.engine.test.interceptors.TestCaseFinalizeInterceptor13import io.kotest.engine.test.interceptors.TestCaseInitializeInterceptor14import io.kotest.engine.test.interceptors.TestCaseInterceptors15import io.kotest.engine.test.interceptors.TestCaseRetryInterceptor16import io.kotest.engine.test.interceptors.TestCaseTimeoutInterceptor17import io.kotest.engine.test.interceptors.TestCaseValidateInterceptor18import io.kotest.engine.test.interceptors.TestCaseWrapInterceptor19import io.kotest.engine.test.status.Status20import io.kotest.engine.test.status.TestStatus21import io.kotest.engine.test.status.TestStatusScope22import io.kotest.engine.test.status.toTestStatus23import io.kotest.engine.test.status.toTestStatusScope24import io.kotest.engine.test.toTestCase25import io.kotest.engine.test.toTestResult26import io.kotest.engine.test.toTestSuite27import io.kotest.engine.test.toTestSuiteEntry28import io.kotest.engine.test.toTestSuiteResult29import io.kotest.engine.test.toTestSuiteScope30import io.kotest.engine.test.toTestSuiteScopeEntry31import io.kotest.engine.test.toTestSuiteScopeResult32import io.kotest.engine.test.toTestSuiteScopeResultEntry33import io.kotest.engine.test.toTestSuiteScopeResultEntryStatus34import io.kotest.engine.test.toTestSuiteScopeResultStatus35import io.kotest.engine.test.toTestSuiteScopeStatus36import io.kotest.engine.test.toTestSuiteStatus37import io.kotest.engine.test.toTestSuiteStatusEntry38import io.kotest.engine.test.toTestSuiteStatusResult39import io.kotest.engine.test
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!