Skip to content

Provide the Ability to Disable a Single DynamicTest When Used in a TestFactory #4620

@philliplbryant

Description

@philliplbryant

When using DynamicTest instances as input to a TestFactory, one cannot disable a single DynamicTest instance. Whereas commenting out an instance would allow the test to pass, disabling the instance would also allow the instance to be included in test output, indicating the instance has been ignored.

This approach seems more consistent with disabling other tests.

In some instances, using the @Disabled annotation may not be feasible. As such, potential solutions could create additional dynamicTest(...) functions, that accept an additional boolean parameter (defaulting to false), indicating the test should be disabled.

Consider the Kotlin example, Example One (below), where the forbearance DynamicTest could be argued as valid (forbearance is translated as patience in some translations). One could comment out the forbearance DynamicTest until the FruitOfTheSpirit enum is modified to accept it as valid, but doing so would not indicate the test was disabled in the test output. It would be preferable to disable the test instead.

Additionally, using Example One, it seems unlikely that the @Disabled annotation could be used. Thus the suggestion to use an additional boolean parameter with the dynamicTest(...) method.

See Example Two, below, indicating what the updated test might look like to pass in the boolean parameter (which seems to be a lot of work to introduce the TestParameter class and new parameter just to disable a single test).

Example One

import org.junit.jupiter.api.DynamicTest.dynamicTest
import org.junit.jupiter.api.TestFactory
import org.junit.jupiter.api.assertDoesNotThrow

enum class FruitOfTheSpirit {
    love,
    joy,
    peace,
    patience,
    kindness,
    goodness,
    faithfulness,
    gentleness,
    self_control,
}

class FruitOfTheSpiritTest {

    @TestFactory
    fun `test fruit of the Spirit (revised)`() = listOf(
        "love",
        "joy",
        "peace",
        "patience",
        "forbearance",
        "kindness",
        "goodness",
        "faithfulness",
        "gentleness",
        "self_control",
        "watermelon",
    ).map { fruit ->

        dynamicTest("Is $fruit a fruit of the Spirit?") {
            assertDoesNotThrow(
                "If you want to be a $fruit,\n" +
                        "you might as well hear it,\n" +
                        "you can't be a fruit of the Spirit!\n" +
                        "'Cuz the fruit of the Spirit is:\n" +
                        "${
                            FruitOfTheSpirit.entries.toTypedArray().joinToString(
                                separator = "\n",
                            )
                        }\n\n"
            ) {
                FruitOfTheSpirit.valueOf(fruit)
            }
        }
    }
}

Example Two

import org.junit.jupiter.api.DynamicTest.dynamicTest
import org.junit.jupiter.api.TestFactory
import org.junit.jupiter.api.assertDoesNotThrow

enum class FruitOfTheSpirit {
    love,
    joy,
    peace,
    patience,
    kindness,
    goodness,
    faithfulness,
    gentleness,
    self_control,
}

data class TestParameters(
    val fruitName: String,
    val isDisabled: Boolean = false,
)

class FruitOfTheSpiritTest {

    @TestFactory
    fun `test fruit of the Spirit (revised)`() = listOf(
        TestParameters("love"),
        TestParameters("joy"),
        TestParameters("peace"),
        TestParameters("patience"),
        TestParameters("forbearance", true),
        TestParameters("kindness"),
        TestParameters("goodness"),
        TestParameters("faithfulness"),
        TestParameters("gentleness"),
        TestParameters("self_control"),
        TestParameters("watermelon"),
    ).map { (fruit, isDisabled) ->

        if (isDisabled) {
            println("DynamicTest \"$fruit\" has been disabled")
        }

        // Pass `isDisabled` as an additional functional parameter here
        dynamicTest("Is $fruit a fruit of the Spirit?") {
            assertDoesNotThrow(
                "If you want to be a $fruit,\n" +
                        "you might as well hear it,\n" +
                        "you can't be a fruit of the Spirit!\n" +
                        "'Cuz the fruit of the Spirit is:\n" +
                        "${
                            FruitOfTheSpirit.entries.toTypedArray().joinToString(
                                separator = "\n",
                            )
                        }\n\n"
            ) {
                FruitOfTheSpirit.valueOf(fruit)
            }
        }
    }
}

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions