Best Spek code snippet using org.spekframework.spek2.runtime.scope.Fixtures
Executor.kt
Source:Executor.kt
...37 scopeExecutionStarted(scope, listener)38 suspend fun finalize(result: ExecutionResult) {39 val actualResult = try {40 when (scope) {41 is GroupScopeImpl -> scope.invokeAfterGroupFixtures(false)42 is TestScopeImpl -> scope.invokeAfterTestFixtures()43 }44 result45 } catch (e: Throwable) {46 ExecutionResult.Failure(e)47 }48 scope.after(actualResult.toPublicExecutionResult())49 if (actualResult is ExecutionResult.Failure) {50 throw actualResult.cause51 }52 }53 val scopeCoroutineContext: CoroutineContext = EmptyCoroutineContext54 val result = executeSafely(scopeCoroutineContext, { finalize(it) }) {55 when (scope) {56 is GroupScopeImpl -> {57 withContext(scopeCoroutineContext) {58 scope.before()59 scope.invokeBeforeGroupFixtures(false)60 var failed = false61 for (it in scope.getChildren()) {62 if (failed) {63 scopeIgnored(it, "Previous failure detected, skipping.", listener)64 continue65 }66 val result = execute(it, listener)67 if (scope.failFast && it is TestScopeImpl && result is ExecutionResult.Failure) {68 failed = true69 }70 }71 }72 }73 is TestScopeImpl -> {74 val exception = withContext(scopeCoroutineContext) {75 val job = async {76 scope.before()77 scope.invokeBeforeTestFixtures()78 scope.execute()79 }80 if (scope.timeout == 0L) {81 try {82 job.await()83 null84 } catch (e: Throwable) {85 e86 }87 } else {88 val timedExecutionResult = withTimeoutOrNull(scope.timeout) {89 try {90 job.await()91 TimedExecutionResult.Success...
scopes.kt
Source:scopes.kt
...32 lifecycleManager: LifecycleManager,33 val preserveExecutionOrder: Boolean,34 val failFast: Boolean = false35) : ScopeImpl(id, path, skip, lifecycleManager), GroupScope {36 private val fixtures = Fixtures()37 private val children = mutableListOf<ScopeImpl>()38 fun addChild(child: ScopeImpl) {39 children.add(child)40 }41 fun removeChild(child: ScopeImpl) {42 children.remove(child)43 }44 fun getChildren() = children.toList()45 fun filterBy(path: Path) {46 val filteredChildren = children47 .filter { it.path.intersects(path) }48 .map {49 if (it is GroupScopeImpl) {50 it.filterBy(path)51 }52 it53 }54 children.clear()55 children.addAll(filteredChildren)56 }57 fun isEmpty() = children.isEmpty()58 override fun before() = lifecycleManager.beforeExecuteGroup(this)59 override fun after(result: ExecutionResult) = lifecycleManager.afterExecuteGroup(this, result)60 fun beforeEachTest(fixture: Fixture) {61 fixtures.beforeEachTest(fixture)62 }63 fun afterEachTest(fixture: Fixture) {64 fixtures.afterEachTest(fixture)65 }66 fun beforeEachGroup(fixture: Fixture) {67 fixtures.beforeEachGroup(fixture)68 }69 fun afterEachGroup(fixture: Fixture) {70 fixtures.afterEachGroup(fixture)71 }72 fun beforeGroup(fixture: Fixture) {73 fixtures.beforeGroup(fixture)74 }75 fun afterGroup(fixture: Fixture) {76 fixtures.afterGroup(fixture)77 }78 suspend fun invokeBeforeTestFixtures() {79 (parent as? GroupScopeImpl)?.invokeBeforeTestFixtures()80 fixtures.invokeBeforeTestFixtures()81 }82 suspend fun invokeAfterTestFixtures() {83 fixtures.invokeAfterTestFixtures()84 (parent as? GroupScopeImpl)?.invokeAfterTestFixtures()85 }86 suspend fun invokeBeforeGroupFixtures(inheritableOnly: Boolean) {87 // we only want to execute fixtures that we inherit aka beforeEachGroup88 (parent as? GroupScopeImpl)?.invokeBeforeGroupFixtures(true)89 fixtures.invokeBeforeGroupFixtures(inheritableOnly)90 }91 suspend fun invokeAfterGroupFixtures(inheritableOnly: Boolean) {92 // we only want to execute fixtures that we inherit aka afterEachGroup93 fixtures.invokeAfterGroupFixtures(inheritableOnly)94 (parent as? GroupScopeImpl)?.invokeAfterGroupFixtures(true)95 }96}97class TestScopeImpl(98 id: ScopeId,99 path: Path,100 override val parent: GroupScope,101 val timeout: Long,102 private val body: suspend TestBody.() -> Unit,103 skip: Skip,104 lifecycleManager: LifecycleManager105) : ScopeImpl(id, path, skip, lifecycleManager), TestScope {106 override fun before() = lifecycleManager.beforeExecuteTest(this)107 suspend fun execute() {108 body.invoke(object : TestBody {109 override fun <T> memoized(): MemoizedValue<T> {110 return MemoizedValueReader(this@TestScopeImpl)111 }112 })113 }114 override fun after(result: ExecutionResult) = lifecycleManager.afterExecuteTest(this, result)115 suspend fun invokeBeforeTestFixtures() {116 (parent as? GroupScopeImpl)?.invokeBeforeTestFixtures()117 }118 suspend fun invokeAfterTestFixtures() {119 (parent as? GroupScopeImpl)?.invokeAfterTestFixtures()120 }121}...
Collectors.kt
Source:Collectors.kt
...9import org.spekframework.spek2.runtime.scope.*10class Collector(11 val root: GroupScopeImpl,12 private val lifecycleManager: LifecycleManager,13 private val fixtures: FixturesAdapter,14 override val defaultCachingMode: CachingMode,15 override var defaultTimeout: Long16) : Root {17 private val ids = linkedMapOf<String, Int>()18 override fun <T> memoized(mode: CachingMode, factory: () -> T): MemoizedValue<T> = memoized(mode, factory) { }19 override fun <T> memoized(mode: CachingMode, factory: () -> T, destructor: (T) -> Unit): MemoizedValue<T> {20 return MemoizedValueCreator(21 root,22 mode,23 factory,24 destructor25 )26 }27 override fun <T> memoized(): MemoizedValue<T> {...
SpekRuntime.kt
Source:SpekRuntime.kt
...27 return DiscoveryResult(scopes)28 }29 fun execute(request: ExecutionRequest) = Executor().execute(request)30 private fun resolveSpec(instance: Spek, path: Path): GroupScopeImpl {31 val fixtures = FixturesAdapter()32 val lifecycleManager = LifecycleManager().apply {33 addListener(fixtures)34 }35 val (packageName, className) = ClassUtil.extractPackageAndClassNames(instance::class)36 val qualifiedName = if (packageName.isNotEmpty()) {37 "$packageName.$className"38 } else {39 className40 }41 val classScope = GroupScopeImpl(ScopeId(ScopeType.Class, qualifiedName), path, null, Skip.No, lifecycleManager, false)42 val collector = Collector(classScope, lifecycleManager, fixtures, CachingMode.TEST, DEFAULT_TIMEOUT)43 try {44 instance.root.invoke(collector)45 } catch (e: Exception) {...
FixturesAdapter.kt
Source:FixturesAdapter.kt
1package org.spekframework.spek2.runtime2import org.spekframework.spek2.lifecycle.GroupScope3import org.spekframework.spek2.lifecycle.LifecycleListener4import org.spekframework.spek2.lifecycle.TestScope5class FixturesAdapter : LifecycleListener {6 private val beforeEachTest: LinkedHashMap<GroupScope, MutableList<() -> Unit>> = LinkedHashMap()7 private val afterEachTest: LinkedHashMap<GroupScope, MutableList<() -> Unit>> = LinkedHashMap()8 private val beforeGroup: LinkedHashMap<GroupScope, MutableList<() -> Unit>> = LinkedHashMap()9 private val afterGroup: LinkedHashMap<GroupScope, MutableList<() -> Unit>> = LinkedHashMap()10 override fun beforeExecuteTest(test: TestScope) {11 invokeAllBeforeEachTest(test.parent)12 }13 override fun afterExecuteTest(test: TestScope) {14 invokeAllAfterEachTest(test.parent)15 }16 override fun beforeExecuteGroup(group: GroupScope) {17 beforeGroup[group]?.forEach { it() }18 }19 override fun afterExecuteGroup(group: GroupScope) {...
fixtures.kt
Source:fixtures.kt
1package org.spekframework.spek2.runtime.scope2import org.spekframework.spek2.dsl.Fixture3class Fixtures {4 private class GroupFixture(val inheritable: Boolean, val fixture: Fixture) {5 suspend operator fun invoke() {6 fixture()7 }8 }9 private val beforeTestFixtures = mutableListOf<Fixture>()10 private val afterTestFixtures = mutableListOf<Fixture>()11 private val beforeGroupFixtures = mutableListOf<GroupFixture>()12 private val afterGroupFixtures = mutableListOf<GroupFixture>()13 fun beforeEachTest(fixture: Fixture) {14 beforeTestFixtures.add(fixture)15 }16 fun afterEachTest(fixture: Fixture) {17 afterTestFixtures.add(fixture)18 }19 fun beforeGroup(fixture: Fixture) {20 beforeGroupFixtures.add(GroupFixture(false, fixture))21 }22 fun afterGroup(fixture: Fixture) {23 afterGroupFixtures.add(GroupFixture(false, fixture))24 }25 fun beforeEachGroup(fixture: Fixture) {26 beforeGroupFixtures.add(GroupFixture(true, fixture))27 }28 fun afterEachGroup(fixture: Fixture) {29 afterGroupFixtures.add(GroupFixture(true, fixture))30 }31 suspend fun invokeBeforeTestFixtures() {32 beforeTestFixtures.forEach { fixture ->33 fixture()34 }35 }36 suspend fun invokeAfterTestFixtures() {37 afterTestFixtures.forEach { fixture ->38 fixture()39 }40 }41 suspend fun invokeBeforeGroupFixtures(inheritableOnly: Boolean) {42 beforeGroupFixtures.filter { !inheritableOnly || it.inheritable }43 .forEach { fixture ->44 fixture()45 }46 }47 suspend fun invokeAfterGroupFixtures(inheritableOnly: Boolean) {48 afterGroupFixtures.filter { !inheritableOnly || it.inheritable }49 .forEach { fixture ->50 fixture()51 }52 }53}...
Fixtures
Using AI Code Generation
1val fixture = Fixtures.createInstance(MyFixture::class.java)2val fixtureScope = FixtureScope(fixture)3fixtureScope.beforeEach()4fixtureScope.afterEach()5fixtureScope.beforeGroup()6fixtureScope.afterGroup()7fixtureScope.afterExecute()8fixtureScope.beforeExecute()9fixtureScope.afterExecute()
Fixtures
Using AI Code Generation
1class MySpec : Spek({2 fixtures(MyFixture::class) {3 beforeEachTest { fixture ->4 }5 }6 fixtures(MyFixture::class) {7 beforeEachGroup { fixture ->8 }9 }10 fixtures(MyFixture::class) {11 afterEachGroup { fixture ->12 }13 }14 fixtures(MyFixture::class) {15 afterEachTest { fixture ->16 }17 }18})
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!!