Scala FunSpec 测试 describe() 初始化和执行顺序
Scala FunSpec tests describe() initialization and execution order
我在尝试将 BDD 方法与 scalatest 和 mockito 结合使用时遇到了问题。
为了减少代码重复,我将每个需要的 when() 规则放在每个 describe 块中。但令我惊讶的是 describe() 块 运行.
的顺序
class SomeTest extends FunSpec with BeforeAndAfterAll with MockitoSugar {
private val catalogClient = mock[CatalogServiceClient]
override def beforeAll {
when(catalogClient.getFrame(any)).thenReturn(Frame())
}
describe("MyTest1") {
println("Inside MyTest1")
when(catalogClient.getConnection(any))
.thenReturn(Conn(ID_FOR_TEST_1))
it("should perform action with data ID_FOR_TEST_1") {
println("Inside it 1")
}
it("should perform another action with data ID_FOR_TEST_1") {
///
}
}
describe("MyTest2") {
println("Inside MyTest2")
when(catalogClient.getConnection(any))
.thenReturn(Conn(ID_FOR_TEST_2))
it("should perform logic with data ID_FOR_TEST_2") {
println("Inside it 2")
}
it("should perform another logic with data ID_FOR_TEST_2") {
///
}
}
}
它打印了:
"Inside MyTest1"
"Inside MyTest2"
"Inside it 1"
"Inside it 2"
在我期待的时候
"Inside MyTest1"
"Inside it 1"
"Inside MyTest2"
"Inside it 2"
第一个测试失败,因为在第二个 describe() 块中替换了模拟数据。
所以它首先通过所有描述块,然后是 运行 测试。
经过一些研究,我发现 path.FunSpec
class 保留了每个描述块的顺序,但它不允许使用像 BeforeAndAfter
这样的特征,因为覆盖了 runTest()
方法作为最终方法。
我想知道一些以最少的代码重复组织此类测试的良好做法。以及关于我的特殊情况的一些建议。
默认情况下,scalatest 运行并行测试以缩短测试时间。
你有这个问题的事实表明你有另一个问题,并且幸运地偶然发现了 - 你的测试不是孤立的。
要解决此问题,请让每个测试都创建自己的模拟对象版本。如果你想减少代码重复,scalatest 有钩子可以 运行 在每次测试之前编码。
我在尝试将 BDD 方法与 scalatest 和 mockito 结合使用时遇到了问题。 为了减少代码重复,我将每个需要的 when() 规则放在每个 describe 块中。但令我惊讶的是 describe() 块 运行.
的顺序class SomeTest extends FunSpec with BeforeAndAfterAll with MockitoSugar {
private val catalogClient = mock[CatalogServiceClient]
override def beforeAll {
when(catalogClient.getFrame(any)).thenReturn(Frame())
}
describe("MyTest1") {
println("Inside MyTest1")
when(catalogClient.getConnection(any))
.thenReturn(Conn(ID_FOR_TEST_1))
it("should perform action with data ID_FOR_TEST_1") {
println("Inside it 1")
}
it("should perform another action with data ID_FOR_TEST_1") {
///
}
}
describe("MyTest2") {
println("Inside MyTest2")
when(catalogClient.getConnection(any))
.thenReturn(Conn(ID_FOR_TEST_2))
it("should perform logic with data ID_FOR_TEST_2") {
println("Inside it 2")
}
it("should perform another logic with data ID_FOR_TEST_2") {
///
}
}
}
它打印了:
"Inside MyTest1"
"Inside MyTest2"
"Inside it 1"
"Inside it 2"
在我期待的时候
"Inside MyTest1"
"Inside it 1"
"Inside MyTest2"
"Inside it 2"
第一个测试失败,因为在第二个 describe() 块中替换了模拟数据。
所以它首先通过所有描述块,然后是 运行 测试。
经过一些研究,我发现 path.FunSpec
class 保留了每个描述块的顺序,但它不允许使用像 BeforeAndAfter
这样的特征,因为覆盖了 runTest()
方法作为最终方法。
我想知道一些以最少的代码重复组织此类测试的良好做法。以及关于我的特殊情况的一些建议。
默认情况下,scalatest 运行并行测试以缩短测试时间。
你有这个问题的事实表明你有另一个问题,并且幸运地偶然发现了 - 你的测试不是孤立的。
要解决此问题,请让每个测试都创建自己的模拟对象版本。如果你想减少代码重复,scalatest 有钩子可以 运行 在每次测试之前编码。