2014-03-05 36 views
0

我正在使用Play 2.2.1与scala并试图使用跨多个文件的Specs2匹配器进行测试。一切工作正常在一个非常大的ApplicationSpec.scala文件,但我想将代码移动到单独的文件。如何在多个文件中使用play scala specs2匹配器

下面的代码是我用来测试跨多个文件,但它是非常间歇性的。

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

} 

测试结果每个 “播放测试” 运行测试1,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

您可以使用特性来声明实例,然后通过将它们混合它们导入主要规格:从SBT运行

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

你如何排除的文件?我试着用testFile1和testFile2创建另一个目录。以下命令已添加到build.sbt文件中,但不排除unmanagedSources文件中的excludeFilter:=“tests/*。scala” –

+0

您可以根据名称添加一个测试过滤器:testOptions in Test:= Seq(Tests .Filter(_。startsWith(“test”)))' – Eric

+0

添加过滤器后,它仍然不起作用。在另一个论坛上,有人说不要使用和以“test”开头的文件名,所以我重命名了这些文件。我做了一个实验,在其中创建了一个默认测试项目并添加了一些文件。即使默认测试没有调用任何文件,它也会为每个文件运行一次测试。 –

相关问题