我如何标记一些ScalaTest,以便仅在显式调用时执行

时间:2019-04-27 20:24:51

标签: scala sbt scalatest

我是SBT和scalatest的新手,但想知道如何让我的org.scalatest._测试中的一些仅按需执行。

在SBT中,我可以调用sbt:Sprout> test之类的所有单元测试,或者sbt:Sprout> it:test之类的所有IntegrationTests。我需要一种注释测试的方法,它允许sbt:Sprout test调用跳过它们,但是使用其他一些调用只能执行这些测试。 Scalatest文档谈到了一些sbt:Sprout> test-only *RedSuite调用,以允许我对测试进行“分类”,但是尚不清楚如何利用它来避免它们作为单元测试运行。 org.scalatest.Tag本身不能使sbt:Sprout> test脱离执行的“默认”状态。除非明确调用,否则我需要将它们设为ignored

在ScalaTest中通过SBT可以使用这种用例吗?

2 个答案:

答案 0 :(得分:4)

  

您可以指定要包含在运行中或从运行中排除的测试的标记名。要指定要包括的标记,请使用-n,后跟要包括的标记名称列表。同样,要指定要排除的标签,请使用-l,后跟要排除的标签名称列表

(请查看here,以获取官方文档中的更多信息)。

例如:

package com.test

import org.scalatest.FlatSpec
import org.scalatest.Tag

object IncludeTest extends Tag("com.tags.Include")
object ExcludeTest extends Tag("com.tags.Exclude")

class TestSuite extends FlatSpec {

 "Test1" taggedAs(IncludeTest) in {
   val sum = 1 + 1
   assert(sum === 2)
  }

 "Test2" taggedAs(ExcludeTest) in {
   val minus = 2 - 1
   assert(minus === 1)
 }
}

要包含IncludeTest和排除ExcludeTest标签,您应该执行以下操作:

test-only org.* -- -n com.tags.Include -l com.tags.Exclude

答案 1 :(得分:2)

assume中指定的

fixture-context object断言可用于实现依赖于环境标志的条件忽略语义。例如,考虑以下IfIgnored灯具

trait IfIgnored extends Assertions {
  assume(System.getenv("runIgnoredTest").toBoolean, "!!! TEST IGNORED !!!")
}

可以像这样实例化

it should "not say goodbye" in new IfIgnored  {
  Hello.greeting shouldNot be ("goodbye")
}

现在,如果我们在build.sbt中定义以下设置

Test / fork := true,
Test / envVars := Map("runIgnoredTest" -> "false")

以及以下测试

class HelloSpec extends FlatSpec with Matchers {
  "The Hello object" should "say hello" in {
    Hello.greeting shouldEqual "hello"
  }

  it should "not say goodbye" in new IfIgnored  {
    Hello.greeting shouldNot be ("goodbye")
  }

  it should "not say live long and prosper" in new IfIgnored {
    Hello.greeting shouldNot be ("live long and prosper")
  }
}

然后执行sbt test应该输出

[info] HelloSpec:
[info] The Hello object
[info] - should say hello
[info] - should not say goodbye !!! CANCELED !!!
[info]   scala.Predef.augmentString(java.lang.System.getenv("runIgnoredTest")).toBoolean was false !!! TEST IGNORED !!! (HelloSpec.scala:6)
[info] - should not say live long and prosper !!! CANCELED !!!
[info]   scala.Predef.augmentString(java.lang.System.getenv("runIgnoredTest")).toBoolean was false !!! TEST IGNORED !!! (HelloSpec.scala:6)
[info] Run completed in 2 seconds, 389 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 2, ignored 0, pending 0
[info] All tests passed.

我们只看到should say hello跑了,其余的都被忽略了。

要仅执行忽略的测试,我们可以定义以下自定义命令testOnlyIgnored

commands += Command.command("testOnlyIgnored") { state =>
  val ignoredTests = List(
    """"should not say goodbye"""",
    """"should not say live long and prosper""""
  ).mkString("-z ", " -z ", "")

  """set Test / envVars := Map("runIgnoredTest" -> "true")""" ::
    s"""testOnly -- $ignoredTests""" :: state
}

请注意我们如何利用-z运行参数来运行特定的测试,例如,

testOnly -- -z "should not say goodbye" -z "should not say live long and prosper"

还要注意我们如何手动将测试名称添加到ignoredTests。现在执行sbt testOnlyIgnored应该输出

[info] HelloSpec:
[info] The Hello object
[info] - should not say goodbye
[info] - should not say live long and prosper
[info] Run completed in 2 seconds, 298 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.

在运行所有被忽略的测试的同时,我们没有看到should say hello运行的地方。

如果我们放弃了必须单独运行忽略的测试的要求,那么我们可以像这样使用提供的ignore注释

ignore should "not say goodbye" in {
  Hello.greeting shouldNot be ("goodbye")
}

哪个在sbt test上输出

[info] HelloSpec:
[info] The Hello object
[info] - should say hello
[info] - should not say goodbye !!! IGNORED !!!
[info] - should not say live long and prosper !!! IGNORED !!!
[info] Run completed in 2 seconds, 750 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 2, pending 0