How to use DefaultDisplayNameFormatter class of io.kotest.engine.test.names package

Best Kotest code snippet using io.kotest.engine.test.names.DefaultDisplayNameFormatter

TeamCityTestEngineListener.kt

Source:TeamCityTestEngineListener.kt Github

copy

Full Screen

...10import 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()...

Full Screen

Full Screen

EnhancedConsoleTestEngineListener.kt

Source:EnhancedConsoleTestEngineListener.kt Github

copy

Full Screen

...7import 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",...

Full Screen

Full Screen

DefaultDisplayNameFormatterTest.kt

Source:DefaultDisplayNameFormatterTest.kt Github

copy

Full Screen

...11import io.kotest.core.spec.style.FunSpec12import io.kotest.core.test.TestCase13import io.kotest.core.test.TestType14import io.kotest.core.test.config.ResolvedTestConfig15import io.kotest.engine.test.names.DefaultDisplayNameFormatter16import io.kotest.matchers.shouldBe17@Isolate18class DefaultDisplayNameFormatterTest : FunSpec() {19   init {20      test("@DisplayName should be used for spec name") {21         DefaultDisplayNameFormatter(ProjectConfiguration()).format(SpecWithDisplayName::class) shouldBe "ZZZZZ"22      }23      test("test name should use full path option") {24         val conf = ProjectConfiguration()25         conf.displayFullTestPath = true26         val tc1 = TestCase(27            SpecWithDisplayName::class.toDescriptor().append("test"),28            TestName("test"),29            SpecWithDisplayName(),30            {},31            sourceRef(),32            TestType.Test,33         )34         val tc2 = TestCase(35            SpecWithDisplayName::class.toDescriptor().append("test2"),36            TestName("test2"),37            SpecWithDisplayName(),38            {},39            sourceRef(),40            TestType.Test,41            parent = tc142         )43         DefaultDisplayNameFormatter(conf).format(tc2) shouldBe "test test2"44      }45      test("tags should be appended from config when configuration is set") {46         val c = ProjectConfiguration()47         c.testNameAppendTags = true48         val tc = TestCase(49            SpecWithDisplayName::class.toDescriptor().append("test"),50            TestName("test"),51            SpecWithDisplayName(),52            {},53            sourceRef(),54            TestType.Test,55            ResolvedTestConfig.default.copy(tags = setOf(NamedTag("Foo"), Dummy))56         )57         DefaultDisplayNameFormatter(c).format(tc) shouldBe "test[tags = Foo, Dummy]"58      }59      test("bang should not be included in test name") {60         val tc = TestCase(61            descriptor = SpecWithDisplayName::class.toDescriptor().append("!test"),62            name = TestName("!test"),63            spec = SpecWithDisplayName(),64            test = {},65            source = sourceRef(),66            type = TestType.Test,67            config = ResolvedTestConfig.default.copy(tags = setOf(Dummy, NoUse))68         )69         DefaultDisplayNameFormatter(ProjectConfiguration()).format(tc) shouldBe "test"70      }71      test("focus should not be included in test name") {72         val tc = TestCase(73            descriptor = SpecWithDisplayName::class.toDescriptor().append("f:test"),74            name = TestName("f:test"),75            spec = SpecWithDisplayName(),76            test = {},77            source = sourceRef(),78            type = TestType.Test,79            config = ResolvedTestConfig.default.copy(tags = setOf(Dummy, NoUse))80         )81         DefaultDisplayNameFormatter(ProjectConfiguration()).format(tc) shouldBe "test"82      }83      test("name should include prefix if affixes are included by default") {84         val tc = TestCase(85            descriptor = SpecWithDisplayName::class.toDescriptor().append("f:test"),86            name = TestName("prefix", "foo", null, true),87            spec = SpecWithDisplayName(),88            test = {},89            source = sourceRef(),90            type = TestType.Test,91            config = ResolvedTestConfig.default.copy(tags = setOf(Dummy, NoUse))92         )93         DefaultDisplayNameFormatter(ProjectConfiguration()).format(tc) shouldBe "prefixfoo"94      }95      test("name should include prefix if affixes are excluded by default but enabled by config") {96         val tc = TestCase(97            descriptor = SpecWithDisplayName::class.toDescriptor().append("f:test"),98            name = TestName("prefix", "foo", null, false),99            spec = SpecWithDisplayName(),100            test = {},101            source = sourceRef(),102            type = TestType.Test,103            config = ResolvedTestConfig.default.copy(tags = setOf(Dummy, NoUse))104         )105         val c = ProjectConfiguration()106         c.includeTestScopeAffixes = true107         DefaultDisplayNameFormatter(c).format(tc) shouldBe "prefixfoo"108      }109      test("name should include suffix if affixes are included by default") {110         val tc = TestCase(111            descriptor = SpecWithDisplayName::class.toDescriptor().append("f:test"),112            name = TestName(null, "foo", "suffix", true),113            spec = SpecWithDisplayName(),114            test = {},115            source = sourceRef(),116            type = TestType.Test,117            config = ResolvedTestConfig.default.copy(tags = setOf(Dummy, NoUse))118         )119         DefaultDisplayNameFormatter(ProjectConfiguration()).format(tc) shouldBe "foosuffix"120      }121      test("name should include suffix if affixes are excluded by default but enabled in config") {122         val tc = TestCase(123            descriptor = SpecWithDisplayName::class.toDescriptor().append("f:test"),124            name = TestName(null, "foo", "suffix", false),125            spec = SpecWithDisplayName(),126            test = {},127            source = sourceRef(),128            type = TestType.Test,129            config = ResolvedTestConfig.default.copy(tags = setOf(Dummy, NoUse))130         )131         val c = ProjectConfiguration()132         c.includeTestScopeAffixes = true133         DefaultDisplayNameFormatter(c).format(tc) shouldBe "foosuffix"134      }135   }136}137object Dummy : Tag()138object NoUse : Tag()139@DisplayName("ZZZZZ")140private class SpecWithDisplayName : FunSpec({141   test("a") { }142})...

Full Screen

Full Screen

DefaultDisplayNameFormatter.kt

Source:DefaultDisplayNameFormatter.kt Github

copy

Full Screen

...15fun 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) {...

Full Screen

Full Screen

KotestTestRunner.kt

Source:KotestTestRunner.kt Github

copy

Full Screen

...4import io.kotest.core.spec.Spec5import io.kotest.engine.TestEngineLauncher6import io.kotest.engine.spec.Materializer7import io.kotest.engine.spec.createAndInitializeSpec8import io.kotest.engine.test.names.DefaultDisplayNameFormatter9import kotlinx.coroutines.runBlocking10import org.junit.runner.Description11import org.junit.runner.Runner12import org.junit.runner.notification.RunNotifier13class KotestTestRunner(14   private val kclass: Class<out Spec>15) : Runner() {16   private val formatter = DefaultDisplayNameFormatter(ProjectConfiguration())17   override fun run(notifier: RunNotifier) {18      runBlocking {19         val listener = JUnitTestEngineListener(notifier)20         TestEngineLauncher(listener).withClasses(kclass.kotlin).launch()21      }22   }23   override fun getDescription(): Description {24      val spec = runBlocking { createAndInitializeSpec(kclass.kotlin, EmptyExtensionRegistry).getOrThrow() }25      val desc = Description.createSuiteDescription(spec::class.java)26      Materializer(ProjectConfiguration()).materialize(spec).forEach { rootTest ->27         desc.addChild(28            describeTestCase(29               rootTest,30               formatter.format(rootTest)...

Full Screen

Full Screen

JUnitTestEngineListener.kt

Source:JUnitTestEngineListener.kt Github

copy

Full Screen

2import io.kotest.core.config.ProjectConfiguration3import io.kotest.core.test.TestCase4import io.kotest.core.test.TestResult5import io.kotest.engine.listener.AbstractTestEngineListener6import io.kotest.engine.test.names.DefaultDisplayNameFormatter7import org.junit.runner.Description8import org.junit.runner.notification.Failure9import org.junit.runner.notification.RunNotifier10class JUnitTestEngineListener(11   private val notifier: RunNotifier,12) : AbstractTestEngineListener() {13   private val formatter = DefaultDisplayNameFormatter(ProjectConfiguration())14   override suspend fun testStarted(testCase: TestCase) {15      notifier.fireTestStarted(describeTestCase(testCase, formatter.format(testCase)))16   }17   override suspend fun testFinished(testCase: TestCase, result: TestResult) {18      val desc = describeTestCase(testCase, formatter.format(testCase))19      when (result) {20         is TestResult.Success -> notifier.fireTestFinished(desc)21         is TestResult.Error -> notifyFailure(desc, result)22         is TestResult.Ignored -> notifier.fireTestIgnored(desc)23         is TestResult.Failure -> notifyFailure(desc, result)24      }25   }26   private fun notifyFailure(desc: Description, result: TestResult) {27      notifier.fireTestFailure(Failure(desc, result.errorOrNull))...

Full Screen

Full Screen

DefaultDisplayNameFormatter

Using AI Code Generation

copy

Full Screen

1+class DefaultDisplayNameFormatterTest : FunSpec({2+   test("test name") {3+      val testPath = TestPath(4+         listOf(5+            Description.spec("Spec"),6+            Description.test("test name"),7+            Description.test("test name")8+      DefaultDisplayNameFormatter.format(testPath) shouldBe "Spec: test name: test name"9+   }10+})11+class DefaultTestNameFormatterTest : FunSpec({12+   test("test name") {13+      val testPath = TestPath(14+         listOf(15+            Description.spec("Spec"),16+            Description.test("test name"),17+            Description.test("test name")18+      DefaultTestNameFormatter.format(testPath) shouldBe "test name"19+   }20+})

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful