【问题标题】:How do you use play scala specs2 matchers across multiple files您如何在多个文件中使用 play scala specs2 匹配器
【发布时间】: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


    【解决方案1】:

    您可以使用特征来声明示例,然后通过混合将它们导入到主要规范中:

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

    【讨论】:

    • 如何从 sbt 运行中排除文件?我尝试在其中创建另一个包含 testFile1 和 testFile2 的目录。以下命令已添加到 build.sbt 文件中,但不排除文件 - unmanagedSources 中的 excludeFilter := "tests/*.scala"
    • 可以根据名字添加测试过滤器:testOptions in Test := Seq(Tests.Filter(_.startsWith("test")))
    • 添加过滤器后还是不行。在另一个论坛上,有人说不要使用以“test”开头的文件名,所以我重命名了文件。我做了一个实验,我用默认测试创建了一个项目并添加了一些文件。即使默认测试不调用任何文件,它也会为每个文件运行一次测试。
    • 我搞错了,需要省略test开头的文件:testOptions in Test := Seq(Tests.Filter((s: String) => !s.startsWith("test")))
    • 有了过滤器,我终于得到了 specs2 来打印一次结果。现在文件中的匹配器没有打印,但 println 语句工作正常。
    最近更新 更多