我正在尝试从大约 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)
}
}
}
})
在这种情况下,我没有看到任何“AAAAAAAAAAAAAAAA..”,但我看到了 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)
}
}
}
})
哦,太好了!但是等一下......,它只执行一个测试,我的意思是只针对列表中的第一个元素。他?有人可以解释一下这种行为吗?另外,也许您知道 Spek 的其他替代品?我会用 Kotlin 编写测试。但是对于 Spek,这看起来几乎是不可能的