How to use DisplayNameFormatter class of io.kotest.core.names package

Best Kotest code snippet using io.kotest.core.names.DisplayNameFormatter

JUnitTestEngineListener.kt

Source:JUnitTestEngineListener.kt Github

copy

Full Screen

2import io.kotest.core.config.ProjectConfiguration3import io.kotest.core.descriptors.Descriptor4import io.kotest.core.descriptors.DescriptorId5import io.kotest.core.descriptors.toDescriptor6import io.kotest.core.names.DisplayNameFormatter7import io.kotest.core.names.UniqueNames8import io.kotest.core.test.TestCase9import io.kotest.core.test.TestResult10import io.kotest.core.test.TestType11import io.kotest.engine.errors.ExtensionExceptionExtractor12import io.kotest.engine.interceptors.EngineContext13import io.kotest.engine.listener.AbstractTestEngineListener14import io.kotest.engine.test.names.DefaultDisplayNameFormatter15import io.kotest.engine.test.names.getDisplayNameFormatter16import io.kotest.mpp.Logger17import io.kotest.mpp.bestName18import org.junit.platform.engine.EngineExecutionListener19import org.junit.platform.engine.TestDescriptor20import org.junit.platform.engine.TestExecutionResult21import org.junit.platform.engine.support.descriptor.ClassSource22import org.junit.platform.engine.support.descriptor.EngineDescriptor23import kotlin.reflect.KClass24import kotlin.time.Duration25/**26 * Notifies JUnit Platform of test statuses via a [EngineExecutionListener].27 *28 * This is not thread safe and should only be invoked by one spec at a time.29 *30 * JUnit platform supports out of order notification of tests, in that sibling31 * tests can be executing in parallel and updating JUnit out of order. However the gradle test32 * task gets confused if we are executing two or more tests directly under the root at once.33 * Therefore we must queue up notifications until each spec is completed.34 *35 * Gradle test run observations:36 *37 * using Platform 1.6.0 --38 * TestDescriptor.Type.CONTAINER seem to be ignored in output.39 * TestDescriptor.Type.CONTAINER_AND_TEST appear as siblings of their nested tests if not added as a child40 * Add child first, then register dynamic test, then start the test41 *42 * Top level descriptors must have a source attached or the execution will fail with a parent attached exception.43 * Type.CONTAINER_TEST doesn't seem to work as a top level descriptor, it will hang44 * leaf tests do not need to be completed but they will be marked as uncomplete in intellij.45 * Dynamic test can be called after or before addChild.46 * A Type.TEST can be a child of a Type.TEST.47 * Intermediate Type.CONTAINER seem to be ignored in output.48 * Intermediate containers can have same class source as parent.49 * Type.TEST as top level seems to hang.50 * A TEST doesn't seem to be able to have the same source as a parent, or hang.51 * A TEST seems to hang if it has a ClassSource.52 * MethodSource seems to be ok with a TEST.53 * Container test names seem to be taken from a Source.54 * Nested tests are outputted as siblings.55 * Can complete executions out of order.56 * Child failures will fail parent CONTAINER.57 * Sibling containers can start and finish in parallel.58 *59 * Intellij runner observations:60 *61 * Intermediate Type.CONTAINERs are shown.62 * Intermediate Type.TESTs are shown.63 * A Type.TEST can be a child of a Type.TEST64 * MethodSource seems to be ok with a TEST.65 * Container test names seem to be taken from the name property.66 * Nested tests are outputted as nested.67 * Child failures will not fail containing TEST.68 * child failures will fail a containing CONTAINER.69 * Call addChild _before_ registering test otherwise will appear in the display out of order.70 * Must start tests after their parent or they can go missing.71 * Sibling containers can start and finish in parallel.72 */73class JUnitTestEngineListener(74 private val listener: EngineExecutionListener,75 val root: EngineDescriptor,76) : AbstractTestEngineListener() {77 private val logger = Logger(JUnitTestEngineListener::class)78 private var formatter: DisplayNameFormatter = DefaultDisplayNameFormatter(ProjectConfiguration())79 // contains a mapping of junit TestDescriptor's, so we can find previously registered tests80 private val descriptors = mutableMapOf<Descriptor, TestDescriptor>()81 private var started = false82 private val startedTests = mutableSetOf<Descriptor.TestDescriptor>()83 // the root tests are our entry point when outputting results84 private val rootTests = mutableListOf<TestCase>()85 private var failOnIgnoredTests = false86 private val children = mutableMapOf<Descriptor, MutableList<TestCase>>()87 private val results = mutableMapOf<Descriptor, TestResult>()88 private val dummies = hashSetOf<String>()89 override suspend fun engineStarted() {90 logger.log { Pair(null, "Engine started") }91 listener.executionStarted(root)92 }93 override suspend fun engineInitialized(context: EngineContext) {94 failOnIgnoredTests = context.configuration.failOnIgnoredTests95 formatter = getDisplayNameFormatter(context.configuration.registry, context.configuration)96 }97 override suspend fun engineFinished(t: List<Throwable>) {98 logger.log { Pair(null, "Engine finished; throwables=[${t}]") }99 registerExceptionPlaceholders(t)100 val result = if (failOnIgnoredTests && results.values.any { it.isIgnored }) {101 TestExecutionResult.failed(RuntimeException("Build contained ignored test"))102 } else {103 TestExecutionResult.successful()104 }105 logger.log { Pair(null, "Notifying junit that engine completed $root") }106 listener.executionFinished(root, result)107 }108 override suspend fun specStarted(kclass: KClass<*>) {109 markSpecStarted(kclass)...

Full Screen

Full Screen

TeamCityTestEngineListener.kt

Source:TeamCityTestEngineListener.kt Github

copy

Full Screen

1package io.kotest.engine.listener2import io.kotest.core.config.ProjectConfiguration3import io.kotest.core.descriptors.Descriptor4import io.kotest.core.descriptors.toDescriptor5import io.kotest.core.names.DisplayNameFormatter6import io.kotest.core.test.TestCase7import io.kotest.core.test.TestResult8import io.kotest.core.test.TestType9import io.kotest.engine.errors.ExtensionExceptionExtractor10import io.kotest.engine.extensions.MultipleExceptions11import io.kotest.engine.interceptors.EngineContext12import io.kotest.engine.teamcity.Locations13import io.kotest.engine.teamcity.TeamCityMessageBuilder14import io.kotest.engine.test.names.DefaultDisplayNameFormatter15import io.kotest.engine.test.names.getDisplayNameFormatter16import io.kotest.mpp.bestName17import kotlin.reflect.KClass18/**19 * A [TestEngineListener] that logs events to the console using a [TeamCityMessageBuilder].20 */21class TeamCityTestEngineListener(22 private val prefix: String = TeamCityMessageBuilder.TeamCityPrefix,23 private val details: Boolean = true,24) : TestEngineListener {25 private var formatter: DisplayNameFormatter = DefaultDisplayNameFormatter(ProjectConfiguration())26 // once a spec has completed, we want to be able to check whether any given test is27 // a container or a leaf test, and so this map contains all test that have children28 private val children = mutableMapOf<Descriptor, MutableList<TestCase>>()29 private val results = mutableMapOf<Descriptor, TestResult>()30 private val started = mutableSetOf<Descriptor.TestDescriptor>()31 // intellij has no method for failed suites, so if a container or spec fails we must insert32 // a dummy "test" in order to tag the error against that33 private fun insertPlaceholder(t: Throwable, parent: Descriptor) {34 val (name, cause) = ExtensionExceptionExtractor.resolve(t)35 val msg1 = TeamCityMessageBuilder36 .testStarted(prefix, name)37 .id(name)38 .parent(parent.path().value)39 .build()40 println(msg1)41 // we must print out the stack trace in between the dummy, so it appears when you click on the test name42 //t?.printStackTrace()43 val msg2 = TeamCityMessageBuilder44 .testFailed(prefix, name)45 .id(name)46 .parent(parent.path().value)47 .withException(cause, details)48 .build()49 println(msg2)50 val msg3 = TeamCityMessageBuilder51 .testFinished(prefix, name)52 .id(name)53 .parent(parent.path().value)54 .build()55 println(msg3)56 }57 override suspend fun engineStarted() {}58 override suspend fun engineInitialized(context: EngineContext) {59 formatter = getDisplayNameFormatter(context.configuration.registry, context.configuration)60 }61 override suspend fun engineFinished(t: List<Throwable>) {62 if (t.isNotEmpty()) {63 t.withIndex().forEach { (index, error) ->64 val testName = if (t.size == 1) "Engine exception" else "Engine exception ${index + 1}"65 println(TeamCityMessageBuilder.testStarted(prefix, testName).build())66 val message = error.message ?: t::class.bestName()67 println(TeamCityMessageBuilder.testFailed(prefix, testName).message(message).build())68 println(TeamCityMessageBuilder.testFinished(prefix, testName).build())69 }70 }71 }72 override suspend fun specStarted(kclass: KClass<*>) {73 val msg = TeamCityMessageBuilder...

Full Screen

Full Screen

EnhancedConsoleTestEngineListener.kt

Source:EnhancedConsoleTestEngineListener.kt Github

copy

Full Screen

2import com.github.ajalt.mordant.TermColors3import io.kotest.core.config.ProjectConfiguration4import io.kotest.core.descriptors.Descriptor5import io.kotest.core.descriptors.toDescriptor6import io.kotest.core.names.DisplayNameFormatter7import io.kotest.core.test.TestCase8import io.kotest.core.test.TestResult9import io.kotest.core.test.TestType10import io.kotest.engine.interceptors.EngineContext11import io.kotest.engine.test.names.DefaultDisplayNameFormatter12import io.kotest.engine.test.names.formatTestPath13import io.kotest.engine.test.names.getDisplayNameFormatter14import kotlin.reflect.KClass15import kotlin.time.Duration16/**17 * Generates test output to the console in an enhanced, formatted, coloured, way.18 * For a more basic output, see [BasicConsoleTestEngineListener]19 */20class EnhancedConsoleTestEngineListener(private val term: TermColors) : AbstractTestEngineListener() {21 private var errors = 022 private var start = System.currentTimeMillis()23 private var testsFailed = emptyList<Pair<TestCase, TestResult>>()24 private var testsIgnored = 025 private var testsPassed = 026 private var specsFailed = emptyList<Descriptor.SpecDescriptor>()27 private var specsSeen = emptyList<Descriptor>()28 private var slow = Duration.milliseconds(500)29 private var verySlow = Duration.milliseconds(5000)30 private var formatter:DisplayNameFormatter = DefaultDisplayNameFormatter(ProjectConfiguration())31 private fun green(str: String) = term.green(str)32 private fun greenBold(str: String) = term.green.plus(term.bold).invoke(str)33 private fun red(str: String) = term.red(str)34 private fun brightRed(str: String) = term.brightRed(str)35 private fun brightRedBold(str: String) = term.brightRed.plus(term.bold).invoke(str)36 private fun redBold(str: String) = term.red.plus(term.bold).invoke(str)37 private fun yellow(str: String) = term.yellow(str)38 private fun brightYellow(str: String) = term.brightYellow(str)39 private fun brightYellowBold(str: String) = term.brightYellow.plus(term.bold).invoke(str)40 private fun yellowBold(str: String) = term.yellow.plus(term.bold).invoke(str)41 private fun bold(str: String) = term.bold(str)42 private val intros = listOf(43 "Feeding the kotest engine with freshly harvested tests",44 "Engaging kotest engine at warp factor 9",45 "Harvesting the test fields",46 "Preparing to sacrifice your code to the demi-god of test",47 "Hamsters are turning the wheels of kotest",48 "Battle commanders are ready to declare war on bugs",49 "Be afraid - be very afraid - of failing tests",50 "The point is, ladies and gentlemen, that green is good",51 "Lock test-foils in attack position",52 "Fasten your seatbelts. It's going to be a bumpy test-run",53 "Lets crack open this test suite",54 "Lets get testing, I'm on the clock here",55 "Test time is the best time",56 "Open the test suite doors, HAL",57 "Mama always said testing was like a box of chocolates. You don't know which ones are gonna fail",58 "A test suite. Shaken, not stirred",59 "I'm going to make him a test he can't refuse",60 "You testing me? I don't see any other tests here, so you must be testing me",61 "I love the smell of tests in the morning",62 "Do you feel lucky punk? Do you think your tests will pass? Well, do ya?",63 "Mirab, with tests unfurled",64 "Dolly works 9 to 5. I test 24/7",65 "A test suite's gotta do what a test suite's gotta do",66 "I test code and chew bubblegum, and I'm all out of bubblegum"67 )68 override suspend fun engineInitialized(context: EngineContext) {69 formatter = getDisplayNameFormatter(context.configuration.registry, context.configuration)70 println(bold(">> Kotest"))71 println("- " + intros.shuffled().first())72 print("- Test plan has ")73 print(greenBold(context.suite.specs.size.toString()))74 println(" specs")75 println()76 }77 override suspend fun engineFinished(t: List<Throwable>) {78 if (specsSeen.isEmpty()) return79 if (t.isNotEmpty()) {80 errors += t.size81 t.forEach {82 printThrowable(it, 0)83 }...

Full Screen

Full Screen

DefaultDisplayNameFormatter.kt

Source:DefaultDisplayNameFormatter.kt Github

copy

Full Screen

...3import io.kotest.common.platform4import io.kotest.core.annotation.displayname.wrapper5import io.kotest.core.config.ExtensionRegistry6import io.kotest.core.config.ProjectConfiguration7import io.kotest.core.extensions.DisplayNameFormatterExtension8import io.kotest.core.names.DisplayNameFormatter9import io.kotest.core.names.TestNameCase10import io.kotest.core.spec.DisplayName11import io.kotest.core.test.TestCase12import io.kotest.mpp.annotation13import io.kotest.mpp.bestName14import kotlin.reflect.KClass15fun getDisplayNameFormatter(registry: ExtensionRegistry, configuration: ProjectConfiguration): DisplayNameFormatter {16 return registry.all()17 .filterIsInstance<DisplayNameFormatterExtension>()18 .firstOrNull()19 ?.formatter() ?: DefaultDisplayNameFormatter(configuration)20}21/**22 * A default implementation of [DisplayNameFormatter].23 * Used when there are no registered [io.kotest.core.extensions.DisplayNameFormatterExtension]s.24 */25class DefaultDisplayNameFormatter(26 private val configuration: ProjectConfiguration,27) : DisplayNameFormatter {28 constructor() : this(ProjectConfiguration())29 override fun format(testCase: TestCase): String {30 val prefix = when (configuration.includeTestScopeAffixes ?: testCase.name.defaultAffixes) {31 true -> testCase.name.prefix ?: ""32 false -> ""33 }34 val suffix = when (configuration.includeTestScopeAffixes ?: testCase.name.defaultAffixes) {35 true -> testCase.name.suffix ?: ""36 false -> ""37 }38 val displayName = if (prefix.isBlank()) {39 when (configuration.testNameCase) {40 TestNameCase.Sentence -> testCase.name.testName.capital() + suffix41 TestNameCase.InitialLowercase -> testCase.name.testName.uncapitalize() + suffix...

Full Screen

Full Screen

DisplayNameFormatter.kt

Source:DisplayNameFormatter.kt Github

copy

Full Screen

...3import kotlin.reflect.KClass4/**5 * Returns formatted spec and test names for display or reporting purposes.6 */7interface DisplayNameFormatter {8 /**9 * Returns a formatted name for a test.10 */11 fun format(testCase: TestCase): String12 /**13 * Returns a formatted name for a spec class.14 */15 fun format(kclass: KClass<*>): String16}

Full Screen

Full Screen

DisplayNameFormatterExtension.kt

Source:DisplayNameFormatterExtension.kt Github

copy

Full Screen

1package io.kotest.core.extensions2import io.kotest.core.names.DisplayNameFormatter3/**4 * An extension point that allows users to inject their own [DisplayNameFormatter].5 *6 * Note: If multiple [DisplayNameFormatterExtension]s are registered, then one7 * will be picked arbitrarily.8 */9interface DisplayNameFormatterExtension {10 fun formatter(): DisplayNameFormatter11}...

Full Screen

Full Screen

paths.kt

Source:paths.kt Github

copy

Full Screen

1package io.kotest.engine.test.names2import io.kotest.core.names.DisplayNameFormatter3import io.kotest.core.test.TestCase4fun DisplayNameFormatter.formatTestPath(testCase: TestCase, separator: String): String {5 return when (val parent = testCase.parent) {6 null -> format(testCase)7 else -> format(parent) + separator + format(testCase)8 }9}...

Full Screen

Full Screen

DisplayNameFormatter

Using AI Code Generation

copy

Full Screen

1 @DisplayName("Test case")2 fun testCase() {3 }4 @DisplayName("Test case")5 fun testCase() {6 }7}8@DisplayNameFormatter("My custom formatter")9class MyCustomFormatter : DisplayNameFormatter {10 override fun format(spec: Spec, test: TestCase): String {11 }12}13@DisplayNameFormatter("My custom formatter", "io.kotest.core.names.RemoveSpecNamePrefix")14class MyCustomFormatter : DisplayNameFormatter {15 override fun format(spec: Spec, test: TestCase): String {16 }17}18@DisplayNameFormatter("My custom formatter", "io.kotest.core.names.RemoveSpecNamePrefix", "io.kotest.core.names.RemoveSpecNameSuffix")19class MyCustomFormatter : DisplayNameFormatter {20 override fun format(spec: Spec, test: TestCase): String {21 }22}23@DisplayNameFormatter("My custom formatter", "io.kotest.core.names.RemoveSpecNamePrefix", "io.kotest.core.names.RemoveSpecNameSuffix", "io.kotest.core.names.RemoveTestNamePrefix")24class MyCustomFormatter : DisplayNameFormatter {25 override fun format(spec: Spec, test: TestCase): String {26 }27}28@DisplayNameFormatter("My custom formatter", "io.kotest.core.names.RemoveSpecNamePrefix", "io.kotest.core.names.RemoveSpecNameSuffix", "io.kotest.core.names.RemoveTestNamePrefix", "io.kotest.core.names.RemoveTestNameSuffix")29class MyCustomFormatter : DisplayNameFormatter {30 override fun format(spec: Spec, test: TestCase): String {31 }32}33@DisplayNameFormatter("My custom formatter", "io.kotest.core.names.RemoveSpecNamePrefix", "io.kotest.core.names.RemoveSpecNameSuffix", "io.kotest.core.names.RemoveTestNamePrefix", "io.kotest.core.names.RemoveTestNameSuffix", "io.kotest.core.names.ReplaceUnderscoreWithSpace")34class MyCustomFormatter : DisplayNameFormatter {35 override fun format(spec: Spec, test: TestCase): String {36 }37}38@DisplayNameFormatter("My custom formatter", "io.kotest.core.names.Remove

Full Screen

Full Screen

DisplayNameFormatter

Using AI Code Generation

copy

Full Screen

1fun main() {2val spec = describe("hello") {3context("world") {4test("this is a test") {5}6}7}8val formatter = DisplayNameFormatter()9val formatted = formatter.format(spec)10println(formatted)11}

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

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

Most used methods in DisplayNameFormatter

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful