Best Kotest code snippet using io.kotest.core.spec.DslDrivenSpec.globalExtensions
DslDrivenSpec.kt
Source:DslDrivenSpec.kt
...17 * Contains the [RootTest]s that have been registered on this spec.18 */19 private var rootTests = emptyList<RootTest>()20 private var sealed = false21 private val globalExtensions = mutableListOf<Extension>()22 /**23 * Marks that this spec has been instantiated and all root tests have been registered.24 * After this point, no further root tests are allowed to be defined.25 */26 fun seal() {27 sealed = true28 }29 override fun rootTests(): List<RootTest> {30 return rootTests31 }32 override fun globalExtensions(): List<Extension> {33 return globalExtensions.toList()34 }35 override fun add(test: RootTest) {36 if (sealed) throw InvalidDslException("Cannot add a root test after the spec has been instantiated: ${test.name.testName}")37 rootTests = rootTests + test38 }39 /**40 * Include the tests and extensions from the given [TestFactory] in this spec.41 * Tests are added in order from where this include was invoked using configuration and42 * settings at the time the method was invoked.43 */44 fun include(factory: TestFactory) {45 factory.tests.forEach { add(it.copy(factoryId = factory.factoryId)) }46 register(factory.extensions)47 }48 /**49 * Includes the tests from the given [TestFactory] in this spec or factory, with the given50 * prefixed added to each of the test's name.51 */52 fun include(prefix: String, factory: TestFactory) {53 val renamed = factory.tests.map { test ->54 val name = test.name.copy(testName = prefix + " " + test.name.testName)55 test.copy(name = name)56 }57 include(factory.copy(tests = renamed))58 }59 /**60 * Registers a callback that will execute after all tests in this spec have completed.61 *62 * This is a convenience method for creating a [FinalizeSpecListener] and constraining63 * it to only fire for this spec.64 */65 fun finalizeSpec(f: FinalizeSpec) {66 globalExtensions.add(object : FinalizeSpecListener {67 override suspend fun finalizeSpec(kclass: KClass<out Spec>, results: Map<TestCase, TestResult>) {68 if (kclass == this@DslDrivenSpec::class) {69 f(Tuple2(kclass, results))70 }71 }72 })73 }74 /**75 * Registers a callback that will execute after all specs have completed.76 *77 * This is a convenience method for creating a [ProjectListener] and registering78 * it with project configuration.79 */80 fun afterProject(f: AfterProject) {81 globalExtensions.add(object : ProjectListener {82 override suspend fun afterProject() {83 f()84 }85 })86 }87 @Deprecated("This has no effect and will be removed in 6.0", level = DeprecationLevel.ERROR)88 fun aroundSpec(aroundSpecFn: AroundSpecFn) {89 extension(object : SpecExtension {90 override suspend fun intercept(spec: KClass<out Spec>, process: suspend () -> Unit) {91 aroundSpecFn(Tuple2(spec, process))92 }93 })94 }95}...
globalExtensions
Using AI Code Generation
1MyExtension()2})3class MySpec : Spec({4MyExtension()5})6class MySpec : BehaviorSpec({7MyExtension()8})9class MySpec : StringSpec({10MyExtension()11})12class MySpec : FreeSpec({13MyExtension()14})15class MySpec : FunSpec({16MyExtension()17})18class MySpec : ShouldSpec({19MyExtension()
globalExtensions
Using AI Code Generation
1class GlobalExtensionsTest : DslDrivenSpec({2 globalExtensions(3 LabelExtension(Label("Global Extension Label"))4 "test case" {5 }6})7class GlobalExtensionsTest : DslDrivenSpec({8 globalExtensions(9 LabelExtension(Label("Global Extension Label"))10 "test case" {11 }12})13class GlobalExtensionsTest : DslDrivenSpec({14 globalExtensions(15 LabelExtension(Label("Global Extension Label"))16 "test case" {17 }18})19class GlobalExtensionsTest : DslDrivenSpec({20 globalExtensions(
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!!