Spek参数化测试

时间:2018-04-09 09:55:31

标签: unit-testing testing kotlin spek

我正在尝试从大约8个小时开始编写一个单独的测试...这个问题在Spek中是非常奇怪的行为(对我而言)。有时会执行,有时则不执行。我有一个错误“测试类不能有两个构造函数”没有任何理由。当我删除class DriverTest(val filePath: String, val expectedTextResult: String)然后重新添加它时,它有效...无论如何,我正在尝试做的是参数化测试:

@RunWith(JUnitPlatform::class)
class ChromeDriverConfigurationTest : Spek
({
    class DriverTest(val filePath: String, val expectedTextResult: String)

    given("Chrome Driver") {
        val chromeDriver: ChromeDriver by lazy { setupChromeDriverForTest() }
        afterGroup { deleteChromeDriver(chromeDriver) }

        listOf(
            DriverTest(filePath = "js/js.html", expectedTextResult = "Text from JS!"),
            DriverTest(filePath = "jQuery/jQuery.html", expectedTextResult = "I come from jQuery!"),
            DriverTest(filePath = "angularv1/angularv1.html", expectedTextResult = "Hello from Angular v1!"),
            DriverTest(filePath = "reactJS/index.html", expectedTextResult = "Hello from reactJS!")
        ).forEach { driverTest ->
            println("BBBBBBBBBBBBBBBBBBBBBB")
            val path = ChromeDriverConfigurationTest::class.java.getResource(driverTest.filePath).path
            chromeDriver.get("file://$path")
            it("should extract test") {
                println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
                val text = chromeDriver.findElement(By.id("test")).text
                assert(text == driverTest.expectedTextResult)
            }

        }
    }
})

在这种情况下,我没有看到任何“AAAAAAAAAAAAAA ..”,但我看了4次“BBBBBBBB”。好像,it部分没有运行。好吧,也许chromeDriver.get有问题,让我们评论一下(休息保持不变)

//val path = ChromeDriverConfigurationTest::class.java.getResource(driverTest.filePath).path
//chromeDriver.get("file://$path")

现在我明白了......

BBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBB
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

他?这是怎么回事。好吧,让我们在每个内容中移动上下文组,就像我在许多教程中看到的那样:

@RunWith(JUnitPlatform::class)
class ChromeDriverConfigurationTest : Spek
({
    class DriverTest(val filePath: String, val expectedTextResult: String)

    listOf(
        DriverTest(filePath = "js/js.html", expectedTextResult = "Text from JS!"),
        DriverTest(filePath = "jQuery/jQuery.html", expectedTextResult = "I come from jQuery!"),
        DriverTest(filePath = "angularv1/angularv1.html", expectedTextResult = "Hello from Angular v1!"),
        DriverTest(filePath = "reactJS/index.html", expectedTextResult = "Hello from reactJS!")
    ).forEach { driverTest ->
        given("Chrome Driver") {
            val chromeDriver: ChromeDriver by lazy { setupChromeDriverForTest() }
            afterGroup { deleteChromeDriver(chromeDriver) }
            println("BBBBBBBBBBBBBBBBBBBBBB")
            val path = ChromeDriverConfigurationTest::class.java.getResource(driverTest.filePath).path
            chromeDriver.get("file://$path")
            it("should extract test") {
                println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
                val text = chromeDriver.findElement(By.id("test")).text
                assert(text == driverTest.expectedTextResult)
            }

        }
    }
})

再次惊讶:

BBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBB

好的,只需保留这些“可读性”,并使其正常工作......让我们把所有内容都放到it部分。

@RunWith(JUnitPlatform::class)
class ChromeDriverConfigurationTest : Spek
({
class DriverTest(val filePath: String, val expectedTextResult: String)

listOf(
    DriverTest(filePath = "js/js.html", expectedTextResult = "Text from JS!"),
    DriverTest(filePath = "jQuery/jQuery.html", expectedTextResult = "I come from jQuery!"),
    DriverTest(filePath = "angularv1/angularv1.html", expectedTextResult = "Hello from Angular v1!"),
    DriverTest(filePath = "reactJS/index.html", expectedTextResult = "Hello from reactJS!")
).forEach { driverTest ->
    given("Chrome Driver") {
        it("should extract test") {
            val chromeDriver: ChromeDriver by lazy { setupChromeDriverForTest() }
            afterGroup { deleteChromeDriver(chromeDriver) }
            println("BBBBBBBBBBBBBBBBBBBBBB")
            val path = ChromeDriverConfigurationTest::class.java.getResource(driverTest.filePath).path
            chromeDriver.get("file://$path")
            println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
            val text = chromeDriver.findElement(By.id("test")).text
            assert(text == driverTest.expectedTextResult)
        }

    }
}
}) 

哦,太棒了!但是等一下......,它只执行1次测试,我的意思是仅用于列表中的第一个元素。他?有人可以解释一下这种行为吗?也许您还知道Spek的其他一些替代方案吗?我会在Kotlin写我的测试。但是使用Spek看起来几乎不可能

0 个答案:

没有答案