【问题标题】:specs2 skip test based on condition without an exceptionspecs2 根据条件跳过测试,无一例外
【发布时间】:2015-05-27 14:22:21
【问题描述】:

有没有办法在不发生 SkippedException 的情况下跳过 specs2 中的测试?我希望测试在特定条件下被忽略,这样它就不会显示为错误而是被忽略。 例如:我在 test/scala/Skipped.scala 文件中有以下内容

package models

import org.specs2.mutable._
import org.specs2.runner._

class Skipped extends Specification{
  "Application" should {
    if( 3 < 2 ){
      "do better with regex" in {

        "axbcd" must find( "bc".r )
      }
    }
    else skipped( "blah" )
  }
}

以下是我的 build.sbt

scalaVersion := "2.10.3"

libraryDependencies += "org.specs2" % "specs2_2.10" % "2.1.1" % "test"

当我运行测试时,我得到以下信息:

org.specs2.execute.SkipException: blah
at org.specs2.matcher.ThrownExpectations$class.skipped(ThrownExpectations.scala:87)

我错过了什么吗?

奖金问题: 为什么我用这么旧的版本?当我的 build.sbt 中有以下内容时,我无法编译上面的代码 sn-p。

scalaVersion := "2.11.1"

libraryDependencies += "org.specs2" %% "specs2" % "2.3.12" % "test"

错误是:

Skipped.scala:7: overloaded method value should with alternatives:
[error]   (fs: => Unit)(implicit p1: Skipped.this.ImplicitParam1, implicit p2: Skipped.this.ImplicitParam2)org.specs2.specification.Fragments <and>
[error]   (fs: => org.specs2.specification.Fragments)(implicit p: Skipped.this.ImplicitParam)org.specs2.specification.Fragments <and>
[error]   (fs: => org.specs2.specification.Fragment)org.specs2.specification.Fragments
[error]  cannot be applied to (Product with Serializable)
[error]   "Application" should {
[error]                 ^
[error] one error found

如果有人可以帮助我无一例外地跳过测试,我将不胜感激,这最好适用于新版本的 specs2

【问题讨论】:

    标签: specs2


    【解决方案1】:

    这应该可行:

    class Skipped extends Specification{
       "Application" should {
         if( 3 < 2 ){
           "do better with regex" in {
             "axbcd" must find( "bc".r )
           }
         }
         else "3 is >= 2" in skipped( "blah" )
       }
    }
    

    不同之处在于skipped是在一个例子的主体中声明的,它允许它被报告给控制台:

    [info] o 3 is >= 2
    [info]    SKIPPED blah
    

    (或类似的东西,我没有运行代码)

    【讨论】:

      猜你喜欢
      • 2021-04-29
      • 2021-07-02
      • 1970-01-01
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 1970-01-01
      • 2021-10-18
      相关资源
      最近更新 更多