반응형

DSL methods

import io.kotest.core.spec.style.WordSpec

class MyTests : WordSpec({
    beforeTest {
        println("Starting a test $it")
    }
    afterTest { (test, result) ->
        println("Finished spec with result $result")
    }
    "this test" should {
        "be alive" {
            println("Johnny5 is alive!")
        }
    }
})
Starting a test TestCase(descriptor=TestDescriptor(parent=SpecDescriptor(id=DescriptorId(value=com.example.demo.MyTests), kclass=class com.example.demo.MyTests), id=DescriptorId(value=this test)), name=TestName(testName=this test, focus=false, bang=false, prefix=null, suffix= should, defaultAffixes=true), spec=com.example.demo.MyTests@73ee9808, test=io.kotest.core.test.TestScope.() -> kotlin.Unit, source=FileSource(fileName=MyTests.kt, lineNumber=12), type=Container, config=ResolvedTestConfig(enabled=(io.kotest.core.test.TestCase) -> io.kotest.core.test.Enabled, invocations=1, threads=1, timeout=null, invocationTimeout=null, tags=[], extensions=[], severity=NORMAL, failfast=false, assertionMode=None, assertSoftly=false, coroutineDebugProbes=false, testCoroutineDispatcher=false, coroutineTestScope=false, blockingTest=false), factoryId=null, parent=null)
Starting a test TestCase(descriptor=TestDescriptor(parent=TestDescriptor(parent=SpecDescriptor(id=DescriptorId(value=com.example.demo.MyTests), kclass=class com.example.demo.MyTests), id=DescriptorId(value=this test)), id=DescriptorId(value=be alive)), name=TestName(testName=be alive, focus=false, bang=false, prefix=null, suffix=null, defaultAffixes=false), spec=com.example.demo.MyTests@73ee9808, test=io.kotest.core.test.TestScope.() -> kotlin.Unit, source=FileSource(fileName=MyTests.kt, lineNumber=13), type=Test, config=ResolvedTestConfig(enabled=(io.kotest.core.test.TestCase) -> io.kotest.core.test.Enabled, invocations=1, threads=1, timeout=null, invocationTimeout=null, tags=[], extensions=[], severity=NORMAL, failfast=false, assertionMode=None, assertSoftly=false, coroutineDebugProbes=false, testCoroutineDispatcher=false, coroutineTestScope=false, blockingTest=false), factoryId=null, parent=TestCase(descriptor=TestDescriptor(parent=SpecDescriptor(id=DescriptorId(value=com.example.demo.MyTests), kclass=class com.example.demo.MyTests), id=DescriptorId(value=this test)), name=TestName(testName=this test, focus=false, bang=false, prefix=null, suffix= should, defaultAffixes=true), spec=com.example.demo.MyTests@73ee9808, test=io.kotest.core.test.TestScope.() -> kotlin.Unit, source=FileSource(fileName=MyTests.kt, lineNumber=12), type=Container, config=ResolvedTestConfig(enabled=(io.kotest.core.test.TestCase) -> io.kotest.core.test.Enabled, invocations=1, threads=1, timeout=null, invocationTimeout=null, tags=[], extensions=[], severity=NORMAL, failfast=false, assertionMode=None, assertSoftly=false, coroutineDebugProbes=false, testCoroutineDispatcher=false, coroutineTestScope=false, blockingTest=false), factoryId=null, parent=null))
Johnny5 is alive!
Finished spec with result Success(duration=16ms)
Finished spec with result Success(duration=109ms)

DSL methods with functions

import io.kotest.core.spec.AfterTest
import io.kotest.core.spec.BeforeTest
import io.kotest.core.spec.style.WordSpec

class MyTests : WordSpec({
    val startTest: BeforeTest = { println("Starting a test $it") }
    val endTest: AfterTest = { (test, result) -> println("Finished spec with result $result") }

    beforeTest(startTest)
    afterTest(endTest)

    "this test" should {
        "be alive" {
            println("Johnny5 is alive!")
        }
    }
})
Starting a test TestCase(descriptor=TestDescriptor(parent=SpecDescriptor(id=DescriptorId(value=com.example.demo.MyTests), kclass=class com.example.demo.MyTests), id=DescriptorId(value=this test)), name=TestName(testName=this test, focus=false, bang=false, prefix=null, suffix= should, defaultAffixes=true), spec=com.example.demo.MyTests@3d71d053, test=io.kotest.core.test.TestScope.() -> kotlin.Unit, source=FileSource(fileName=MyTests.kt, lineNumber=14), type=Container, config=ResolvedTestConfig(enabled=(io.kotest.core.test.TestCase) -> io.kotest.core.test.Enabled, invocations=1, threads=1, timeout=null, invocationTimeout=null, tags=[], extensions=[], severity=NORMAL, failfast=false, assertionMode=None, assertSoftly=false, coroutineDebugProbes=false, testCoroutineDispatcher=false, coroutineTestScope=false, blockingTest=false), factoryId=null, parent=null)
Starting a test TestCase(descriptor=TestDescriptor(parent=TestDescriptor(parent=SpecDescriptor(id=DescriptorId(value=com.example.demo.MyTests), kclass=class com.example.demo.MyTests), id=DescriptorId(value=this test)), id=DescriptorId(value=be alive)), name=TestName(testName=be alive, focus=false, bang=false, prefix=null, suffix=null, defaultAffixes=false), spec=com.example.demo.MyTests@3d71d053, test=io.kotest.core.test.TestScope.() -> kotlin.Unit, source=FileSource(fileName=MyTests.kt, lineNumber=15), type=Test, config=ResolvedTestConfig(enabled=(io.kotest.core.test.TestCase) -> io.kotest.core.test.Enabled, invocations=1, threads=1, timeout=null, invocationTimeout=null, tags=[], extensions=[], severity=NORMAL, failfast=false, assertionMode=None, assertSoftly=false, coroutineDebugProbes=false, testCoroutineDispatcher=false, coroutineTestScope=false, blockingTest=false), factoryId=null, parent=TestCase(descriptor=TestDescriptor(parent=SpecDescriptor(id=DescriptorId(value=com.example.demo.MyTests), kclass=class com.example.demo.MyTests), id=DescriptorId(value=this test)), name=TestName(testName=this test, focus=false, bang=false, prefix=null, suffix= should, defaultAffixes=true), spec=com.example.demo.MyTests@3d71d053, test=io.kotest.core.test.TestScope.() -> kotlin.Unit, source=FileSource(fileName=MyTests.kt, lineNumber=14), type=Container, config=ResolvedTestConfig(enabled=(io.kotest.core.test.TestCase) -> io.kotest.core.test.Enabled, invocations=1, threads=1, timeout=null, invocationTimeout=null, tags=[], extensions=[], severity=NORMAL, failfast=false, assertionMode=None, assertSoftly=false, coroutineDebugProbes=false, testCoroutineDispatcher=false, coroutineTestScope=false, blockingTest=false), factoryId=null, parent=null))
Johnny5 is alive!
Finished spec with result Success(duration=16ms)
Finished spec with result Success(duration=108ms)

Overriding callback functions in a Spec

import io.kotest.core.spec.style.WordSpec
import io.kotest.core.test.TestCase

class MyTests : WordSpec() {
    override suspend fun beforeTest(testCase: TestCase) {
        println("Starting a test $testCase")
    }

    init {
        "this test" should {
            "be alive" {
                println("Johnny5 is alive!")
            }
        }
    }
}
Starting a test TestCase(descriptor=TestDescriptor(parent=SpecDescriptor(id=DescriptorId(value=com.example.demo.MyTests), kclass=class com.example.demo.MyTests), id=DescriptorId(value=this test)), name=TestName(testName=this test, focus=false, bang=false, prefix=null, suffix= should, defaultAffixes=true), spec=com.example.demo.MyTests@1cc3eb37, test=io.kotest.core.test.TestScope.() -> kotlin.Unit, source=ClassSource(fqn=com.example.demo.MyTests, lineNumber=12), type=Container, config=ResolvedTestConfig(enabled=(io.kotest.core.test.TestCase) -> io.kotest.core.test.Enabled, invocations=1, threads=1, timeout=null, invocationTimeout=null, tags=[], extensions=[], severity=NORMAL, failfast=false, assertionMode=None, assertSoftly=false, coroutineDebugProbes=false, testCoroutineDispatcher=false, coroutineTestScope=false, blockingTest=false), factoryId=null, parent=null)
Starting a test TestCase(descriptor=TestDescriptor(parent=TestDescriptor(parent=SpecDescriptor(id=DescriptorId(value=com.example.demo.MyTests), kclass=class com.example.demo.MyTests), id=DescriptorId(value=this test)), id=DescriptorId(value=be alive)), name=TestName(testName=be alive, focus=false, bang=false, prefix=null, suffix=null, defaultAffixes=false), spec=com.example.demo.MyTests@1cc3eb37, test=io.kotest.core.test.TestScope.() -> kotlin.Unit, source=FileSource(fileName=MyTests.kt, lineNumber=13), type=Test, config=ResolvedTestConfig(enabled=(io.kotest.core.test.TestCase) -> io.kotest.core.test.Enabled, invocations=1, threads=1, timeout=null, invocationTimeout=null, tags=[], extensions=[], severity=NORMAL, failfast=false, assertionMode=None, assertSoftly=false, coroutineDebugProbes=false, testCoroutineDispatcher=false, coroutineTestScope=false, blockingTest=false), factoryId=null, parent=TestCase(descriptor=TestDescriptor(parent=SpecDescriptor(id=DescriptorId(value=com.example.demo.MyTests), kclass=class com.example.demo.MyTests), id=DescriptorId(value=this test)), name=TestName(testName=this test, focus=false, bang=false, prefix=null, suffix= should, defaultAffixes=true), spec=com.example.demo.MyTests@1cc3eb37, test=io.kotest.core.test.TestScope.() -> kotlin.Unit, source=ClassSource(fqn=com.example.demo.MyTests, lineNumber=12), type=Container, config=ResolvedTestConfig(enabled=(io.kotest.core.test.TestCase) -> io.kotest.core.test.Enabled, invocations=1, threads=1, timeout=null, invocationTimeout=null, tags=[], extensions=[], severity=NORMAL, failfast=false, assertionMode=None, assertSoftly=false, coroutineDebugProbes=false, testCoroutineDispatcher=false, coroutineTestScope=false, blockingTest=false), factoryId=null, parent=null))
Johnny5 is alive!

ProjectListener

import io.kotest.core.annotation.AutoScan
import io.kotest.core.listeners.AfterProjectListener
import io.kotest.core.listeners.BeforeProjectListener

@AutoScan
class ProjectListener : BeforeProjectListener, AfterProjectListener {
    override suspend fun beforeProject() {
        println("beforeProject")
    }

    override suspend fun afterProject() {
        println("afterProject")
    }
}
import io.kotest.core.spec.style.FunSpec

class DemoTest1 : FunSpec({
    test("test1") {
        println("test1")
    }
})
import io.kotest.core.spec.style.FunSpec

class DemoTest2 : FunSpec({
    test("test2") {
        println("test2")
    }
})
beforeProject
test1
test2
afterProject
반응형

'Development > Kotest' 카테고리의 다른 글

[Kotest] SpringBootTest  (0) 2023.11.04
[Kotest] extensions  (0) 2023.11.04
[Kotest] isolation modes  (0) 2023.11.04
[Kotest] conditional evaluation  (0) 2023.11.04
[Kotest] testing styles  (0) 2023.11.04

+ Recent posts