【发布时间】:2024-04-23 17:35:02
【问题描述】:
我正在使用带有 scala 的 Play 2.2.1,并尝试使用 Specs2 匹配器跨多个文件进行测试。在一个非常大的 ApplicationSpec.scala 文件中一切正常,但我想将代码移动到单独的文件中。
以下代码是我用来测试多个文件的代码,但它非常断断续续。
ApplicationSpec.scala 文件
import org.specs2.mutable._
import org.specs2.mutable.Specification
import org.specs2.matcher.JsonMatchers
import org.specs2.runner._
import org.junit.runner._
@RunWith(classOf[JUnitRunner])
class ApplicationSpec extends PlaySpecification with JsonMatchers {
"Test using another file" should {
testing
"End of test" in {"End" must beEqualTo("End")}
}
此函数位于 ApplicationSpec.scala 文件中
def testing() {
"Multiple files" should {
"Testing testFile1" in {
testFile1.test1
testFile1.test2
"Test1 and Test2 should print before this line" in { 1 must beEqualTo(1)}
}
"Testing testFile2" in {
testFile2.test3
testFile2.test4
"Test3 and Test4 should print before this line" in { 1 must beEqualTo(1)}
}
}
}
testFile1.scala
object testFile1 extends ApplicationSpec {
def test1 {
"testFile1 - test1" in {1 must beEqualTo(1)}
}
def test2 {
"testFile1 - test2" in {1 must beEqualTo(1)}
}
}
testFile2.scala
object testFile2 extends ApplicationSpec {
def test3 {
"testFile2 - test3" in {1 must beEqualTo(1)}
}
def test4 {
"testFile2 - tes4" in {1 must beEqualTo(1)}
}
}
测试结果 每次“播放测试”运行 test1、test2 和 test3 时,test4 可能会或可能不会打印出来。有时会显示所有四个测试,或者没有打印任何测试。
+ test WS logic
[info]
[info] Test using another file should
[info]
[info] Multiple files should
[info]
[info] Testing testFile1
[info] + Test1 and Test2 should print before this line
[info]
[info] Testing testFile2
[info] + testFile2 - test3
[info] + testFile2 - tes4
[info] + Test3 and Test4 should print before this line
[info] + End of test
[info]
[info] Total for specification testFile2
[info] Finished in 1 second, 713 ms
[info] 6 examples, 0 failure, 0 error
[info] testFile1
[info]
[info] Application should
[info] + test WS logic
[info]
[info] Test using another file should
[info]
[info] Multiple files should
[info]
[info] Testing testFile1
[info] + testFile1 - test1
[info] + testFile1 - test2
[info] + Test1 and Test2 should print before this line
[info]
[info] Testing testFile2
[info] + Test3 and Test4 should print before this line
[info] + End of test
[info]
[info] Total for specification testFile1
[info] Finished in 111 ms
[info] 6 examples, 0 failure, 0 error
[info] ApplicationSpec
[info]
[info] Application should
[info] + test WS logic
[info]
[info] Test using another file should
[info]
[info] Multiple files should
[info]
[info] Testing testFile1
[info] + Test1 and Test2 should print before this line
[info]
[info] Testing testFile2
[info] + Test3 and Test4 should print before this line
[info] + End of test
[info]
[info] Total for specification ApplicationSpec
[info] Finished in 99 ms
[info] 4 examples, 0 failure, 0 error
【问题讨论】:
标签: scala playframework specs2