【问题标题】:Why does surefire ignore test categories for Scala specs2 test when run with maven surefire?为什么在使用 maven surefire 运行时,surefire 会忽略 Scala specs2 测试的测试类别?
【发布时间】:2013-07-18 06:49:55
【问题描述】:

我有一个 Scala Specs 2 测试,如下所示:

import org.specs2.mutable.Specification
import org.junit.runner.RunWith
import org.junit.experimental.categories.Category
import util.categories.NightlyTestCategory
import org.specs2.runner.JUnitRunner
import org.junit.Test
import org.junit.runners.JUnit4

@RunWith(classOf[JUnitRunner]) 
class MyTest extends Specification {

  "My Test" should {
    "succeed" in {
      done
    }
  }

}

请注意,上述测试使用自定义运行程序 JUnitRunner(来自 Specs2)来执行测试。我使用 Maven 3 来维护项目,并已将 Surefire 配置为仅包含标记为组 NightlyTestCategory 的测试:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.15</version>
    <configuration>
    ...
       <groups>util.categories.NightlyTestCategory</groups>

    </configuration>
  </plugin>

当我执行测试时,我希望 Maven 不会运行上述测试,因为它没有使用 Category NightlyTestCategory 进行注释。但是,Maven 确实执行了测试。奇怪的是,如果我将测试变成使用 JUnit4 运行器执行的常规 JUnit 测试,只有添加 Category NightlyTestCategory 时,Maven 才会执行此测试:

@RunWith(classOf[JUnit4])
@Category(Array(classOf[NightlyTestCategory])) 
class MyTest extends Specification {

  @Test
  def test = println("Test")

}    

对我来说,使用自定义 Specs2 运行程序 JUnitRunner 似乎会以某种方式影响 surefire 以忽略组设置并简单地运行它找到的所有测试。我正在寻找一种解决方案,允许我使用 JUnitRunner 运行 specs2 测试,但同时应该尊重类别。

【问题讨论】:

    标签: scala maven junit4 specs2


    【解决方案1】:

    specs2 确实还不知道 Junit 类别。同时,一种解决方法是使用标签:

    import org.specs2.specification.Tags
    
    @RunWith(classOf[JUnitRunner])
    class MyTest extends Specification with Tags { section("NightlyTest")
    
      "My Test" should {
        "succeed" in {
          done
        }
      }
    }
    

    然后,如果您添加-Dspecs2.include=NightlyTest 作为系统参数,则只会执行此规范。但是,这意味着您不能使用 来自 Surefire 的“组”配置,您必须使用 Maven 配置文件。

    另一种选择是使用所有 JUnit/Maven 基础架构并仅重用 specs2 匹配器:

    import org.specs2.matcher._
    
    @RunWith(classOf[JUnit4])
    @Category(Array(classOf[NightlyTestCategory]))
    class MyTest extends JUnitMatchers {
    
      @Test
      def test = { 1 must_== 1 }
    
    }
    

    【讨论】:

    • 感谢您澄清这一点。
    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多