我正在尝试编写在JUnit5上运行的KotlinTest测试,使用描述规范样式。
当我需要为某些测试初始化相同的变量时,@BeforeEach在 DescribeSpec 中不起作用。
如何在每次测试之前初始化数据?
class BlockchainUT : DescribeSpec({
var blockchain = Blockchain()
@BeforeEach
fun beforeEach() {
blockchain = Blockchain()
blockchain.addBlock(listOf("foo1", "bar1"))
blockchain.addBlock(listOf("foo2", "bar2"))
blockchain.addBlock(listOf("foo3", "bar3"))
}
describe("isValidChain()") {
context("when the chain does not start with the genesis block") {
blockchain.chain[0] = Block(
System.currentTimeMillis(),
"---",
"xxx",
listOf("foo", "bar"))
it("returns false") {
Blockchain.isValid(blockchain) shouldBe false
}
}
context("when the chain starts with the genesis block and has multiple blocks") {
context("and a lastHash reference has changed returns false") {
blockchain.chain[2] = Block(
blockchain.chain[2].timestamp,
"broken-lastHash",
blockchain.chain[2].hash,
blockchain.chain[2].data)
it("returns false") {
Blockchain.isValid(blockchain) shouldBe false
}
}
context("and the chain contains a block with an invalid field") {
blockchain.chain[2] = Block(
blockchain.chain[2].timestamp,
blockchain.chain[2].lastHash,
blockchain.chain[2].hash,
listOf("some-bad-and-evil-data"))
it("returns false") {
Blockchain.isValid(blockchain) shouldBe false
}
}
}
}
})
在新版 KotlinTest 3.3 上,有新的侦听器来定义beforeTest
、afterTest
、beforeSpec
等。
在这种情况下,一个可能的解决方案应该是:
class BlockchainUT : DescribeSpec(), TestListener {
var blockchain = Blockchain()
override fun beforeTest(describe: TestCase): Unit {
blockchain = Blockchain()
blockchain.addBlock(listOf("foo1", "bar1"))
blockchain.addBlock(listOf("foo2", "bar2"))
blockchain.addBlock(listOf("foo3", "bar3"))
}
init {
describe("isValidChain()") {
context("when the chain starts with the genesis block and has multiple blocks") {
context("and a lastHash reference has changed returns false") {
blockchain.chain[2] = Block(
blockchain.chain[2].timestamp,
"broken-lastHash",
blockchain.chain[2].hash,
blockchain.chain[2].data)
it("returns false") {
Blockchain.isValid(blockchain) shouldBe false
}
}
context("and the chain contains a block with an invalid field") {
blockchain.chain[2] = Block(
blockchain.chain[2].timestamp,
blockchain.chain[2].lastHash,
blockchain.chain[2].hash,
listOf("some-bad-and-evil-data"))
it("returns false") {
Blockchain.isValid(blockchain) shouldBe false
}
}
context("and the chain does not contain any invalid blocks") {
it("returns true") {
Blockchain.isValid(blockchain) shouldBe true
}
}
}
}
}
}
也可以使用新的隔离模式来解决,将隔离模式定义为InstancePerLeaf
。从io.kotlintest.IsolationMode
javadoc
A new instance of the [Spec] class is instantiated for every
[TestCase] - both containers and leaf tests - and they are
executed once the previous test has completed.
For example, in the following test plan:
"this test" {
println("a")
"nested test" {
println("b")
}
"nested test 2" {
println("c")
}
}
The output will be:
a
a
b
a
c
- 最后移动要为每个 [测试用例] 重复的代码
class BlockchainUT : DescribeSpec(){
override fun isolationMode() = IsolationMode.InstancePerTest
describe("isValidChain()") {
context("when the chain starts with the genesis block and has multiple blocks") {
var blockchain = Blockchain()
blockchain.addBlock(listOf("foo1", "bar1"))
blockchain.addBlock(listOf("foo2", "bar2"))
blockchain.addBlock(listOf("foo3", "bar3"))
context("and a lastHash reference has changed returns false") {
blockchain.chain[2] = Block(
blockchain.chain[2].timestamp,
"broken-lastHash",
blockchain.chain[2].hash,
blockchain.chain[2].data)
it("returns false") {
Blockchain.isValid(blockchain) shouldBe false
}
}
context("and the chain contains a block with an invalid field") {
blockchain.chain[2] = Block(
blockchain.chain[2].timestamp,
blockchain.chain[2].lastHash,
blockchain.chain[2].hash,
listOf("some-bad-and-evil-data"))
it("returns false") {
Blockchain.isValid(blockchain) shouldBe false
}
}
context("and the chain does not contain any invalid blocks") {
it("returns true") {
Blockchain.isValid(blockchain) shouldBe true
}
}
}
}
}
}