你如何在多个文件中使用scala specs2匹配器

时间:2014-03-05 00:52:38

标签: scala playframework specs2

我正在使用带有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

1 个答案:

答案 0 :(得分:1)

您可以使用traits声明示例,然后将它们混合到主要规范中:

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)}
  }
}