Skip to content

Commit

Permalink
Duplicated test name exception should include test name #1686
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Sep 4, 2020
1 parent 2d3c183 commit ae47e23
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 11 deletions.
Expand Up @@ -2,4 +2,5 @@ package io.kotest.core

import io.kotest.core.test.DescriptionName

class DuplicatedTestNameException(name: DescriptionName.TestName) : RuntimeException()
class DuplicatedTestNameException(name: DescriptionName.TestName) :
RuntimeException("Cannot create test with duplicated name ${name.displayName}")
Expand Up @@ -4,6 +4,7 @@ import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.DuplicatedTestNameException
import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.DescribeSpec
import io.kotest.matchers.shouldBe

abstract class DescribeSpecDuplicateNameTest(iso: IsolationMode) : DescribeSpec() {
init {
Expand All @@ -12,7 +13,7 @@ abstract class DescribeSpecDuplicateNameTest(iso: IsolationMode) : DescribeSpec(
it("woo") {}
shouldThrow<DuplicatedTestNameException> {
it("woo") {}
}
}.message shouldBe "Cannot create test with duplicated name woo"
}
shouldThrow<DuplicatedTestNameException> {
describe("foo") {}
Expand Down
Expand Up @@ -4,6 +4,7 @@ import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.DuplicatedTestNameException
import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.ExpectSpec
import io.kotest.matchers.shouldBe

abstract class ExpectSpecDuplicateNameTest(iso: IsolationMode) : ExpectSpec() {
init {
Expand All @@ -12,7 +13,7 @@ abstract class ExpectSpecDuplicateNameTest(iso: IsolationMode) : ExpectSpec() {
expect("woo") {}
shouldThrow<DuplicatedTestNameException> {
expect("woo") {}
}
}.message shouldBe "Cannot create test with duplicated name woo"
}
shouldThrow<DuplicatedTestNameException> {
context("foo") {}
Expand Down
Expand Up @@ -4,6 +4,7 @@ import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.DuplicatedTestNameException
import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.FeatureSpec
import io.kotest.matchers.shouldBe

abstract class FeatureSpecDuplicateNameTest(iso: IsolationMode) : FeatureSpec() {
init {
Expand All @@ -12,7 +13,7 @@ abstract class FeatureSpecDuplicateNameTest(iso: IsolationMode) : FeatureSpec()
scenario("woo") {}
shouldThrow<DuplicatedTestNameException> {
scenario("woo") {}
}
}.message shouldBe "Cannot create test with duplicated name woo"
}
shouldThrow<DuplicatedTestNameException> {
feature("foo") {}
Expand Down
Expand Up @@ -4,6 +4,7 @@ import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.DuplicatedTestNameException
import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.FreeSpec
import io.kotest.matchers.shouldBe

abstract class FreeSpecDuplicateNameTest(iso: IsolationMode) : FreeSpec() {

Expand All @@ -13,7 +14,7 @@ abstract class FreeSpecDuplicateNameTest(iso: IsolationMode) : FreeSpec() {
"wibble" { }
shouldThrow<DuplicatedTestNameException> {
"wibble" { }
}
}.message shouldBe "Cannot create test with duplicated name wibble"

"wobble" - {
"wibble" { }
Expand Down
Expand Up @@ -4,6 +4,7 @@ import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.DuplicatedTestNameException
import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

abstract class FunSpecDuplicateNameTest(iso: IsolationMode) : FunSpec() {
init {
Expand All @@ -12,7 +13,7 @@ abstract class FunSpecDuplicateNameTest(iso: IsolationMode) : FunSpec() {
test("wibble") { }
shouldThrow<DuplicatedTestNameException> {
test("wibble") {}
}
}.message shouldBe "Cannot create test with duplicated name wibble"
}
shouldThrow<DuplicatedTestNameException> {
context("wobble") {}
Expand Down
Expand Up @@ -4,6 +4,7 @@ import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.DuplicatedTestNameException
import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.ShouldSpec
import io.kotest.matchers.shouldBe

abstract class ShouldSpecDuplicateNameTest(iso: IsolationMode) : ShouldSpec() {
init {
Expand All @@ -12,7 +13,7 @@ abstract class ShouldSpecDuplicateNameTest(iso: IsolationMode) : ShouldSpec() {
should("woo") {}
shouldThrow<DuplicatedTestNameException> {
should("woo") {}
}
}.message shouldBe "Cannot create test with duplicated name woo"
}
shouldThrow<DuplicatedTestNameException> {
context("foo") {}
Expand Down
Expand Up @@ -4,14 +4,15 @@ import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.DuplicatedTestNameException
import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe

abstract class StringSpecDuplicateNameTest(iso: IsolationMode) : StringSpec() {
init {
isolationMode = iso
"foo" {}
shouldThrow<DuplicatedTestNameException> {
"foo" {}
}
}.message shouldBe "Cannot create test with duplicated name foo"
}
}

Expand Down
Expand Up @@ -128,6 +128,7 @@ class JUnitTestEngineListener(

// reset the flags for this spec
hasVisibleTest = false
hasIgnoredTest = false

try {
val descriptor = kclass.descriptor(root)
Expand Down Expand Up @@ -155,7 +156,7 @@ class JUnitTestEngineListener(
// if the spec itself had an error then we must make sure we add at least one nested test so that
// the test shows up properly in intellij
(exceptionThrowBySpec ?: t)?.apply {
checkSpecVisiblity(kclass, this)
ensureSpecIsVisible(kclass, this)
}

val result = when {
Expand All @@ -178,10 +179,11 @@ class JUnitTestEngineListener(

/**
* Checks that the spec has at least one test attached in case of failure.
* If it doesn't, then it will add a dummy test name to ensure it appears.
*/
private fun checkSpecVisiblity(kclass: KClass<out Spec>, t: Throwable) {
val description = kclass.toDescription()
private fun ensureSpecIsVisible(kclass: KClass<out Spec>, t: Throwable) {
if (!hasVisibleTest) {
val description = kclass.toDescription()
val spec = descriptors[description]!!
val test = spec.append(
description.append(createTestName("Spec execution failed"), TestType.Test), TestDescriptor.Type.TEST, null,
Expand Down

0 comments on commit ae47e23

Please sign in to comment.