I am using Play 2.2.1 with scala and trying to test with Specs2 matchers across multiple files. Everything works fine in one very large ApplicationSpec.scala file but I would like to move the code to separate files.
The following code is what I am using to test across multiple files but it is very intermittent.
ApplicationSpec.scala file
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")}
}
This function is located inside the ApplicationSpec.scala file
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)}
}
}
Test results Each time "play test" is run test1, test2, and test3, test4 may or may not print out. Sometimes all four tests show up or none of the tests are printed.
+ 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
You can use traits to declare examples then import them to the main specification by mixing them in:
class TestSpec extends org.specs2.mutable.Specification with testFile1 with testFile2 {
"Test using another file" should {
testing
"End of test" in {"End" must beEqualTo("End")}
}
def testing {
"Multiple files" should {
"Testing testFile1" in {
tests1
"Test1 and Test2 should print before this line" in { 1 must beEqualTo(1)}
}
"Testing testFile2" in {
tests2
"Test3 and Test4 should print before this line" in { 1 must beEqualTo(1)}
}
}
}
trait testFile1 extends org.specs2.mutable.Specification {
def tests1 = {
"testFile1 - test1" in {1 must beEqualTo(1)}
"testFile1 - test2" in {1 must beEqualTo(1)}
}
}
trait testFile2 extends org.specs2.mutable.Specification {
def tests2 = {
"testFile2 - test3" in {1 must beEqualTo(1)}
"testFile2 - tes4" in {1 must beEqualTo(1)}
}
}