是否可以控制 Scalatest 异步测试用例的执行?
Is it possible to control execution of Scalatest's async test cases?
我正在使用 ScalaTest 的 AsyncFlatSpec风格。
有什么办法可以确保 Second 和 Third 测试用例仅在 First 完成后才开始执行?
这是代码示例:-
class SomeAsyncSpec extends AsyncFlatSpec with Matchers {
var mutableVariable = 0
"First test case" should "perform foo asynchronously" in {
println("First TEST CASE")
performFooFutureCall(mutableVariable)
//assert something
}
"Second test case" should "perform bar asynchronously" in {
println("Second TEST CASE")
performBarFutureCall(mutableVariable)
//assert something
}
"Third test case" should "perform baz asynchronously" in {
println("Second TEST CASE")
performBazFutureCall(mutableVariable)
//assert something
}
}
从代码中可以明显看出,在这个套件中一个接一个地执行测试的原因是不影响共享的可变状态。
在这种情况下,AsyncFlatSpec 文档中的行会有所帮助:-
Because ScalaTest's default execution context on the JVM confines execution of Future
transformations
* and call backs to a single thread, you need not (by default) worry about synchronizing access to mutable state
* in your asynchronous-style tests.
查看 link 提供的文档以获取更多信息。
我正在使用 ScalaTest 的 AsyncFlatSpec风格。 有什么办法可以确保 Second 和 Third 测试用例仅在 First 完成后才开始执行?
这是代码示例:-
class SomeAsyncSpec extends AsyncFlatSpec with Matchers {
var mutableVariable = 0
"First test case" should "perform foo asynchronously" in {
println("First TEST CASE")
performFooFutureCall(mutableVariable)
//assert something
}
"Second test case" should "perform bar asynchronously" in {
println("Second TEST CASE")
performBarFutureCall(mutableVariable)
//assert something
}
"Third test case" should "perform baz asynchronously" in {
println("Second TEST CASE")
performBazFutureCall(mutableVariable)
//assert something
}
}
从代码中可以明显看出,在这个套件中一个接一个地执行测试的原因是不影响共享的可变状态。
在这种情况下,AsyncFlatSpec 文档中的行会有所帮助:-
Because ScalaTest's default execution context on the JVM confines execution of
Future
transformations * and call backs to a single thread, you need not (by default) worry about synchronizing access to mutable state * in your asynchronous-style tests.
查看 link 提供的文档以获取更多信息。