Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin测试,如何使用DescribeSpec测试风格在每次测试之前运行_Kotlin_Kotlintest - Fatal编程技术网

Kotlin测试,如何使用DescribeSpec测试风格在每次测试之前运行

Kotlin测试,如何使用DescribeSpec测试风格在每次测试之前运行,kotlin,kotlintest,Kotlin,Kotlintest,我正在尝试编写在JUnit5上运行的测试,使用 当我需要为某些测试初始化同一个变量时,@beforeach在DescribeSpec中不起作用 如何在每次测试之前初始化数据 class BlockchainUT : DescribeSpec({ var blockchain = Blockchain() @BeforeEach fun beforeEach() { blockchain = Blockchain() blockchain

我正在尝试编写在JUnit5上运行的测试,使用

当我需要为某些测试初始化同一个变量时,@beforeach在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的新版本中,有新的定义
测试前
测试后
测试前
,等等

在这种情况下,可行的解决方案应为:

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

  • 最后移动要为每个[TestCase]重复的代码

  • 你的方法行得通,或者你也可以重写beforeTest函数。它也行@monkjack,我已经编辑了解决方案来反映这个备选方案。你就快到了。您想要覆盖的函数是beforeTest(test:TestCase),而不是beforeach,这是一个junit。
    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
                        }
                    }
                }
            }
        }
    }