How to use SpekTestDescriptorFactory class of org.spekframework.spek2.junit package

Best Spek code snippet using org.spekframework.spek2.junit.SpekTestDescriptorFactory

ReadmeTestEngine.kt

Source:ReadmeTestEngine.kt Github

copy

Full Screen

...5import ch.tutteli.niok.writeText6import org.junit.platform.engine.*7import org.spekframework.spek2.junit.JUnitEngineExecutionListenerAdapter8import org.spekframework.spek2.junit.SpekTestDescriptor9import org.spekframework.spek2.junit.SpekTestDescriptorFactory10import org.spekframework.spek2.junit.SpekTestEngine11import org.spekframework.spek2.runtime.SpekRuntime12import org.spekframework.spek2.runtime.execution.ExecutionRequest13import java.nio.file.Paths14import java.util.*15import kotlin.collections.HashSet16import kotlin.collections.component117import kotlin.collections.component218import kotlin.collections.filterIsInstance19import kotlin.collections.forEach20import kotlin.collections.isNotEmpty21import kotlin.collections.map22import kotlin.collections.mutableMapOf23import kotlin.collections.set24import org.junit.platform.engine.ExecutionRequest as JUnitExecutionRequest25class ReadmeTestEngine : TestEngine {26 private val spek = SpekTestEngine()27 private val examples = mutableMapOf<String, String>()28 private val code = HashSet<String>()29 private val snippets = mutableMapOf<String, String>()30 override fun getId(): String = "spek2-readme"31 override fun discover(discoveryRequest: EngineDiscoveryRequest, uniqueId: UniqueId): TestDescriptor {32 val descriptor = spek.discover(discoveryRequest, uniqueId)33 require(descriptor.children.isNotEmpty()) {34 "Could not find any specification, check your runtime classpath"35 }36 return descriptor37 }38 override fun execute(request: JUnitExecutionRequest) {39 val default = Locale.getDefault()40 try {41 Locale.setDefault(Locale.UK)42 val classes = runSpekWithCustomListener(request)43 processExamples(classes, request)44 } catch (t: Throwable) {45 t.printStackTrace()46 Locale.setDefault(default)47 request.fail(t)48 }49 }50 private fun processExamples(classes: List<String>, request: JUnitExecutionRequest) {51 val specContents = classes.map { qualifiedClass ->52 qualifiedClass to fileContent("src/main/kotlin/$qualifiedClass.kt", request)53 }54 specContents.forEach { (_, specContent) ->55 extractSnippets(specContent, request)56 }57 var readmeContent = fileContent(readmeStringPath, request)58 if (examples.isEmpty()) {59 request.fail("no examples found")60 return61 }62 if (code.isEmpty()) {63 request.fail("no code found")64 return65 }66 if (snippets.isEmpty()) {67 request.fail("no snippets found")68 return69 }70 examples.forEach { (exampleId, output) ->71 readmeContent = updateExampleLikeInReadme(readmeContent, specContents, exampleId, output, request)72 }73 code.forEach { codeId ->74 readmeContent = updateCodeInReadme(readmeContent, specContents, codeId, request)75 }76 snippets.forEach { (snippetId, snippetContent) ->77 readmeContent = updateSnippetInReadme(readmeContent, snippetId, snippetContent, request)78 }79 Paths.get(readmeStringPath).writeText(readmeContent)80 }81 private fun extractSnippets(specContent: String, request: JUnitExecutionRequest) {82 Regex("//(snippet-.+)-start([\\S\\s]*?)//(snippet-.+)-end").findAll(specContent).forEach {83 val (tag, content, endTag) = it.destructured84 request.failIf(tag != endTag) { "tag $tag-start did not end with $tag-end but with $endTag" }85 snippets[tag] = content.trimIndent()86 }87 }88 private fun runSpekWithCustomListener(request: JUnitExecutionRequest) : List<String> {89 val roots = request.rootTestDescriptor.children90 .filterIsInstance<SpekTestDescriptor>()91 .map(SpekTestDescriptor::scope)92 val executionListener = ReadmeExecutionListener(93 JUnitEngineExecutionListenerAdapter(request.engineExecutionListener, SpekTestDescriptorFactory()),94 examples,95 code96 )97 val executionRequest = ExecutionRequest(roots, executionListener)98 SpekRuntime().execute(executionRequest)99 return roots.map { it.path.toString() }100 }101 private fun fileContent(path: String, request: JUnitExecutionRequest): String {102 val file = Paths.get(path)103 request.failIf(!file.exists) { "could not find ${file.absolutePathAsString}" }104 return file.readText()105 }106 private inline fun JUnitExecutionRequest.failIf(predicate: Boolean, errorMessage: () -> String) {107 if (predicate) fail(errorMessage())...

Full Screen

Full Screen

SpekTestEngine.kt

Source:SpekTestEngine.kt Github

copy

Full Screen

...26 UriSelector::class.java,27 DirectorySelector::class.java28 )29 }30 private val descriptorFactory = SpekTestDescriptorFactory()31 private val runtime by lazy { SpekRuntime() }32 override fun getId() = ID33 override fun discover(discoveryRequest: EngineDiscoveryRequest, uniqueId: UniqueId): TestDescriptor {34 val engineDescriptor = SpekEngineDescriptor(uniqueId, id)35 if (containsUnsupportedSelector(discoveryRequest)) {36 return engineDescriptor37 }38 val sourceDirs = discoveryRequest.getSelectorsByType(ClasspathRootSelector::class.java)39 .map { it.classpathRoot }40 .map { Paths.get(it) }41 .map { it.toString() }42 val classSelectors = discoveryRequest.getSelectorsByType(ClassSelector::class.java)43 .filter {44 // get all super classes...

Full Screen

Full Screen

SpekTestDescriptor.kt

Source:SpekTestDescriptor.kt Github

copy

Full Screen

...10import org.spekframework.spek2.runtime.scope.TestScopeImpl11import java.util.*12class SpekTestDescriptor internal constructor(13 val scope: ScopeImpl,14 private val factory: SpekTestDescriptorFactory15) : TestDescriptor {16 companion object {17 private val ENGINE_ID = UniqueId.forEngine(SpekTestEngine.ID)18 }19 private val id by lazy { computeId(scope) }20 private fun computeId(scope: ScopeImpl?): UniqueId = if (scope == null) {21 ENGINE_ID22 } else {23 computeId(scope.parent as ScopeImpl?).append("${scope.id.type}", scope.id.name)24 }25 private var engineDescriptor: SpekEngineDescriptor? = null26 private val childDescriptors = mutableSetOf<TestDescriptor>()27 override fun getUniqueId() = id28 override fun getDisplayName(): String = scope.path.name...

Full Screen

Full Screen

Spek2ForgivingTestEngine.kt

Source:Spek2ForgivingTestEngine.kt Github

copy

Full Screen

1package ch.tutteli.atrium.bctest.spek2import org.junit.platform.engine.*3import org.spekframework.spek2.junit.JUnitEngineExecutionListenerAdapter4import org.spekframework.spek2.junit.SpekTestDescriptor5import org.spekframework.spek2.junit.SpekTestDescriptorFactory6import org.spekframework.spek2.junit.SpekTestEngine7import org.spekframework.spek2.runtime.SpekRuntime8import org.spekframework.spek2.runtime.execution.ExecutionRequest9import java.util.*10import org.junit.platform.engine.ExecutionRequest as JUnitExecutionRequest11class Spek2ForgivingTestEngine : TestEngine {12 private val spek = SpekTestEngine()13 private lateinit var forgiveRegex: Regex14 override fun getId(): String = "spek2-forgiving"15 override fun discover(discoveryRequest: EngineDiscoveryRequest, uniqueId: UniqueId): TestDescriptor {16 val descriptor = spek.discover(discoveryRequest, uniqueId)17 val forgive = discoveryRequest.configurationParameters.get("forgive").orElse(null)18 forgiveRegex = Regex(forgive)19 require(descriptor.children.isNotEmpty()) {20 "Could not find any specification, check your runtime classpath"21 }22 return descriptor23 }24 override fun execute(request: JUnitExecutionRequest) {25 val default = Locale.getDefault()26 try {27 Locale.setDefault(Locale.UK)28 runSpekWithCustomListener(request)29 } catch (t: Throwable) {30 t.printStackTrace()31 Locale.setDefault(default)32 request.fail(t)33 }34 }35 private fun runSpekWithCustomListener(request: JUnitExecutionRequest) {36 val roots = request.rootTestDescriptor.children37 .filterIsInstance<SpekTestDescriptor>()38 .map(SpekTestDescriptor::scope)39 val executionListener = Spek2ForgivingExecutionListener(40 JUnitEngineExecutionListenerAdapter(request.engineExecutionListener, SpekTestDescriptorFactory()),41 forgiveRegex42 )43 val executionRequest = ExecutionRequest(roots, executionListener)44 SpekRuntime().execute(executionRequest)45 }46 private fun JUnitExecutionRequest.fail(throwable: Throwable) {47 engineExecutionListener.executionFinished(48 rootTestDescriptor,49 TestExecutionResult.failed(throwable)50 )51 }52}...

Full Screen

Full Screen

JUnitEngineExecutionListenerAdapter.kt

Source:JUnitEngineExecutionListenerAdapter.kt Github

copy

Full Screen

...7import org.spekframework.spek2.runtime.scope.ScopeImpl8import org.spekframework.spek2.runtime.scope.TestScopeImpl9class JUnitEngineExecutionListenerAdapter(10 private val listener: EngineExecutionListener,11 private val factory: SpekTestDescriptorFactory12) : ExecutionListener {13 companion object {14 private const val DEFAULT_IGNORE_REASON = "<no reason provided>"15 }16 override fun executionStart() = Unit17 override fun executionFinish() = Unit18 override fun testExecutionStart(test: TestScopeImpl) {19 listener.executionStarted(test.asJUnitDescriptor())20 }21 override fun testExecutionFinish(test: TestScopeImpl, result: ExecutionResult) {22 listener.executionFinished(test.asJUnitDescriptor(), result.asJUnitResult())23 }24 override fun testIgnored(test: TestScopeImpl, reason: String?) {25 listener.executionSkipped(test.asJUnitDescriptor(), reason ?: DEFAULT_IGNORE_REASON)...

Full Screen

Full Screen

SpekTestDescriptorFactoryTest.kt

Source:SpekTestDescriptorFactoryTest.kt Github

copy

Full Screen

...10import org.spekframework.spek2.runtime.scope.PathBuilder11import org.spekframework.spek2.runtime.scope.ScopeId12import org.spekframework.spek2.runtime.scope.ScopeType13import kotlin.properties.Delegates14class SpekTestDescriptorFactoryTest {15 var factory: SpekTestDescriptorFactory by Delegates.notNull()16 var lifecycleManager: LifecycleManager by Delegates.notNull()17 @BeforeEach18 fun setup() {19 factory = SpekTestDescriptorFactory()20 lifecycleManager = mock()21 }22 @Test23 fun caching() {24 val path = PathBuilder()25 .append("SomeClass")26 .build()27 val scope = GroupScopeImpl(28 ScopeId(ScopeType.Class, "SomeClass"),29 path,30 null,31 Skip.No,32 lifecycleManager,33 false...

Full Screen

Full Screen

SpekTestDescriptorFactory.kt

Source:SpekTestDescriptorFactory.kt Github

copy

Full Screen

1package org.spekframework.spek2.junit2import org.spekframework.spek2.runtime.scope.GroupScopeImpl3import org.spekframework.spek2.runtime.scope.ScopeImpl4class SpekTestDescriptorFactory {5 private val cache = mutableMapOf<ScopeImpl, SpekTestDescriptor>()6 fun create(scope: ScopeImpl): SpekTestDescriptor = createDescriptor(scope)7 private fun createDescriptor(scope: ScopeImpl): SpekTestDescriptor {8 var cached = true9 val descriptor = cache.computeIfAbsent(scope) {10 cached = false11 SpekTestDescriptor(scope, this)12 }13 if (!cached) {14 descriptor.apply {15 if (scope is GroupScopeImpl) {16 scope.getChildren().forEach { child ->17 this.addChild(create(child))18 }...

Full Screen

Full Screen

SpekTestDescriptorFactory

Using AI Code Generation

copy

Full Screen

1SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();2SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();3SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();4SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();5SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();6SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();7SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();8SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();9SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();10SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();11SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();12SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();13SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();

Full Screen

Full Screen

SpekTestDescriptorFactory

Using AI Code Generation

copy

Full Screen

1SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();2TestDescriptor spekTestDescriptor = spekTestDescriptorFactory.createTestDescriptorForClass(SpekTest::class.java)3SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();4TestDescriptor spekTestDescriptor = spekTestDescriptorFactory.createTestDescriptorForClass(SpekTest::class.java)5SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();6TestDescriptor spekTestDescriptor = spekTestDescriptorFactory.createTestDescriptorForClass(SpekTest.class)7SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();8TestDescriptor spekTestDescriptor = spekTestDescriptorFactory.createTestDescriptorForClass(SpekTest.class)9SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();10TestDescriptor spekTestDescriptor = spekTestDescriptorFactory.createTestDescriptorForClass(SpekTest.class)11SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();12TestDescriptor spekTestDescriptor = spekTestDescriptorFactory.createTestDescriptorForClass(SpekTest.class)13SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();14TestDescriptor spekTestDescriptor = spekTestDescriptorFactory.createTestDescriptorForClass(SpekTest.class)15SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();16TestDescriptor spekTestDescriptor = spekTestDescriptorFactory.createTestDescriptorForClass(SpekTest.class)17SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();18TestDescriptor spekTestDescriptor = spekTestDescriptorFactory.createTestDescriptorForClass(SpekTest.class)19SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();20TestDescriptor spekTestDescriptor = spekTestDescriptorFactory.createTestDescriptorForClass(SpekTest.class)21SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();22TestDescriptor spekTestDescriptor = spekTestDescriptorFactory.createTestDescriptorForClass(SpekTest.class)23SpekTestDescriptorFactory spekTestDescriptorFactory = new SpekTestDescriptorFactory();24TestDescriptor spekTestDescriptor = spekTestDescriptorFactory.createTestDescriptorForClass(SpekTest.class)

Full Screen

Full Screen

SpekTestDescriptorFactory

Using AI Code Generation

copy

Full Screen

1 val spekTestDescriptor = SpekTestDescriptorFactory.create(2 engineDescriptor.addChild(spekTestDescriptor)3}4fun runSpekTest() {5 val spekTestEngine = SpekTestEngine()6 val engineDiscoveryRequest = EngineDiscoveryRequestBuilder.request()7 .selectors(8 UniqueIdSelector.forEngine(spekTestEngine.id),9 UniqueIdSelector.forUniqueId(spekTestEngine.id.append("org.spekframework.spek2.junit.SpekTestEngineTest", "test engine discovery"))10 .build()11 spekTestEngine.execute(engineDiscoveryRequest) { _, _ -> }12}

Full Screen

Full Screen

SpekTestDescriptorFactory

Using AI Code Generation

copy

Full Screen

1import org.junit.platform.engine.TestDescriptor2import org.junit.platform.engine.UniqueId3import org.junit.platform.launcher.EngineFilter4import org.junit.platform.launcher.LauncherDiscoveryRequest5import org.junit.platform.launcher.TestExecutionListener6import org.junit.platform.launcher.TestPlan7import org.spekframework.spek2.junit.SpekTestDescriptorFactory8import org.spekframework.spek2.runtime.SpekRuntime9import org.spekframework.spek2.runtime.execution.ExecutionListener10import org.spekframework.spek2.runtime.execution.ExecutionResult11import org.spekframework.spek2.runtime.execution.ExecutionResult.Failure12import org.spekframework.spek2.runtime.execution.ExecutionResult.Success13import org.spekframework.spek2.runtime.scope.Path14import org.spekframework.spek2.runtime.scope.Scope15import org.spekframework.spek2.runtime.scope.TestScopeImpl16import org.spekframework.spek2.runtime.scope.TestStatus17class SpekTestDescriptorFactory : SpekTestDescriptorFactory() {18override fun create(request: LauncherDiscoveryRequest, listener: TestExecutionListener): TestPlan {19val engineFilters = request.getEngineFilters()20val executionListener = object : ExecutionListener {21override fun executionStart() {22}23override fun executionFinish() {24}25override fun executionSkipped(path: Path, reason: String?) {26listener.executionSkipped(path.toUniqueId(), reason)27}28override fun executionFinish(path: Path, result: ExecutionResult) {29when (result) {30is Success -> listener.executionFinished(path.toUniqueId(), TestStatus.SUCCESS.toTestExecutionResult())31is Failure -> listener.executionFinished(path.toUniqueId(), TestStatus.FA

Full Screen

Full Screen

SpekTestDescriptorFactory

Using AI Code Generation

copy

Full Screen

1val testDescriptor = SpekTestDescriptorFactory.create(spec, testPath)2testPlan.add(testDescriptor)3val uniqueId = UniqueId.forEngine("spek").append("container", spec.name).append("test", testPath)4val testCase = SpekTestCase(uniqueId, spec, testPath)5testPlan.add(testCase)6testDescriptor.addChild(testCase)7}8}

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 Spek automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in SpekTestDescriptorFactory

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful