Konsist
GitHubSlack (kotlinlang)Twitter
  • 🚀GETTING STARTED
    • What is Konsist?
    • Getting Started
      • Add Konsist Dependency
      • Create First Konsist Test - Declaration Check
      • Create Secound Konsist Test - Architectural Check
    • Articles & Videos
  • ✅WRITING TESTS
    • Create The Scope
    • Declaration Filtering
    • Declaration Assertion
    • Architecture Assertion
    • Suppress Konsist Test
  • ✏️VERYFYING CODEBASE
    • Verify Classes
    • Verify Interfaces
    • Verify Functions
    • Verify Properties
    • Verify Generics
    • Verify Source Declarations
  • 📗FEATURES
    • Add Konsist Existing To Project (Baseline)
    • Debug Konsist Test
    • Declaration
    • Declaration Vs Property
    • Compiler Type Inference
    • Package Wildcard
    • Declaration References
    • Indirect Parents
    • Kotest Support
  • 🔍INSPIRATION
    • Starter Projects
    • Snippets
      • General Snippets
      • Android Snippets
      • Spring Snippets
      • Test Snippets
      • JUnit Snippets
      • Kotest Snippets
      • Architecture Snippets
      • Clean Architecture Snippets
      • Kotlin Serialization Snippets
      • Library Snippets
      • Generic Types Snippets
  • 🎓ADVANCED
    • Isolate Konsist Tests
    • Enable Full Command Line Logging
    • Dynamic Konsist Tests
      • Explicit Test Names
    • When Konsist API Is Not Enough
    • Additional JUnit5 Setup
    • Why There Are No Pre-defined Rules?
    • Konsist Snapshots
  • ❓HELP
    • Getting Help
    • Known Issues
      • java.lang.OutOfMemoryError: Java heap space
    • Compatibility
  • ℹ️OTHER
    • Changelog
    • Project Status
    • Contributing
    • Contributors
    • Assets And Logos
    • Open Source Licenses
    • Sponsor Konsist
Powered by GitBook
On this page
Edit on GitHub
Export as PDF
  1. ADVANCED
  2. Dynamic Konsist Tests

Explicit Test Names

PreviousDynamic Konsist TestsNextWhen Konsist API Is Not Enough

Last updated 11 months ago

For dynamic tests, Konsist can't obtain the current test's name. Test name may be correctly displayed in the IDE, however, the testName argument should be provided to enable:

  • Correct test names are displayed in the log when the test is failing

  • Test suppression (See Suppress Konsist Test)

See Dynamic Konsist Tests.

The testName argument should be passed to assertX methods such as assertTrue , assertFalse etc. Let's look at the code:

Konsist.scopeFromProject()
    .classes()
    .assertTrue(testName = "My test name") { ... } //passed test name

Here is the summary of test frameworks:

Testing Framework
Determination
Pass testName?

JUnit4

static

Not required

JUnit5

static

Not required

JUnit5

dynamic

Recommended

Kotest

dynamic

Recommended

Here is a concrete implementation passing he testName argument for each test Framework:

introduced native support for dynamic tests, however, it also supports static tests. For static test testName does not have to be passed as it can be internally retrieved by Konsist.

@Test
fun myTest() {
    Konsist.scopeFromProject()
        .classes()
        .assertTrue { ... }
}

introduced native support for dynamic tests, allowing tests to be generated at runtime through the @TestFactory annotation.

class SampleDynamicKonsistTest {
    @TestFactory
    fun `use case test`(): Stream<DynamicTest> = Konsist
        .scopeFromProject()
        .classes()
        .withNameEndingWith("UseCase")
        .stream()
        .flatMap { useCase ->
            Stream.of(
                dynamicTest("${useCase.name} should have test") {
                   useCase.assertTrue(testName = "${useCase.name} should have test") {
                        it.hasTestClass()
                    }
                },
                dynamicTest("${useCase.name} should reside in ..domain.usecase.. package") {
                    useCase.assertTrue(testName = "${useCase.name} should reside in ..domain.usecase.. package") {
                        it.resideInPackage("..domain.usecase..")
                    }
                },
            )
        }
}
class SampleDynamicKonsistTest : FreeSpec({
    Konsist
        .scopeFromProject()
        .classes()
        .withNameEndingWith("UseCase")
        .forEach { useCase ->
            "${useCase.name} should have test" {
                useCase.assertTrue(testName = this.testCase.name.testName) { it.hasTestClass() }
            }
            "${useCase.name} should reside in ..domain.usecase.. package" {
                useCase.assertTrue(testName = this.testCase.name.testName) { it.resideInPackage("..domain.usecase..") }
            }
        }
})

To facilitate test name retrieval you can add this custom koTestName extension:

val TestScope.koTestName: String
    get() = this.testCase.name.testName
@Test
fun myTest() {
    Konsist.scopeFromProject()
        .classes()
        .assertTrue { ... }
}

provides robust support for dynamic tests, allowing developers to define test cases programmatically at runtime, making it a flexible alternative to traditional JUnit testing. It is recommended to utilize the name derived from the Kotest (this.testCase.name.testName) context as the value for the testName argument:

does not natively support dynamic tests; tests in this framework are typically static and determined at compile-time, so there is no need to pass testName argument.

🎓
JUnit 5
JUnit 5
Kotest
JUnit 4