【问题标题】:sbt testOnly exclusion list with tag does not worksbt testOnly 带有标签的排除列表不起作用
【发布时间】:2020-03-19 01:44:03
【问题描述】:

我有一个测试类

import org.scalatest.FlatSpec

import scala.collection.mutable

class Tags101Spec extends FlatSpec {
  "A Stack" should "pop values in last-in-first-out order" in {
    val stack = new mutable.Stack[Int]
    stack.push(1)
    stack.push(2)
    assert(stack.pop() === 2)
    assert(stack.pop() === 1)
  }

  it should "throw NoSuchElementException if an empty stack is popped" in {
    val emptyStack = new mutable.Stack[String]
    intercept[NoSuchElementException] {
      emptyStack.pop()
    }
  }

  "A String" should "return 0 size when empty" taggedAs (Fast) in {
    assert("".size === 0)
  }

  "A Sorted List of 10 numbers" must "return 10 as the first element when reversed" taggedAs (Slow) in {
    assert(10 === (1 to 10).toList.reverse.head)
  }

}

在同一个目录中,我有一个名为Tags 的类,它看起来像

import org.scalatest.Tag

object Slow extends Tag("Slow Tests")
object Fast extends Tag("Fast Tests")

我使用sbt 运行我的测试,方法是包含一个使用-n 标志的标签,它可以工作

sbt:Unit Testing in Scala> testOnly -- -n Fast
[info] Tags101Spec:
[info] A Stack
[info] A String
[info] A Sorted List of 10 numbers
[info] Run completed in 137 milliseconds.
[info] Total number of tests run: 0
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 0, failed 0, canceled 0, ignored 0, pending 0

由于只有1 测试是taggedAs(Fast),因此该测试只运行了一项测试。

现在,我想做相反的事情,排除 Fast 标记并运行剩余的测试。这是我尝试过的

sbt:Unit Testing in Scala> testOnly -- -l Fast
[info] Tags101Spec:
[info] A Stack
[info] - should pop values in last-in-first-out order
[info] - should throw NoSuchElementException if an empty stack is popped
[info] A String
[info] - should return 0 size when empty
[info] A Sorted List of 10 numbers
[info] - must return 10 as the first element when reversed
[info] Run completed in 252 milliseconds.
[info] Total number of tests run: 4
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 4, failed 0, canceled 0, ignored 0, pending 0

如您所见,它运行了4 测试,这是所有测试,包括1 Fast 标记测试。

我在这里缺少什么?如何使排除标记与sbt 一起使用?

谢谢

【问题讨论】:

    标签: scala sbt scalatest


    【解决方案1】:

    -l-n 的参数应该是传递给Tags 构造函数的name 字符串参数,而不是对象的名称。例如,给定

    object Slow extends Tag("SlowTests")
    object Fast extends Tag("FastTests")
    

    然后用

    排除
    testOnly -- -l FastTests
    

    而不是

    testOnly -- -l Fast
    

    哪个输出

    [info] Tags101Spec:
    [info] A Stack
    [info] - should pop values in last-in-first-out order
    [info] - should throw NoSuchElementException if an empty stack is popped
    [info] A String
    [info] A Sorted List of 10 numbers
    [info] - must return 10 as the first element when reversed
    [info] Run completed in 187 milliseconds.
    [info] Total number of tests run: 3
    [info] Suites: completed 1, aborted 0
    [info] Tests: succeeded 3, failed 0, canceled 0, ignored 0, pending 0
    [info] All tests passed.
    

    我们看到A string 测试没有执行。

    在像这样构造Tags 时,我个人会使用完全限定名称作为name 参数

    package example
    
    import org.scalatest.Tag
    
    object Slow extends Tag("example.Slow")
    object Fast extends Tag("example.Fast")
    

    并执行

    testOnly -- -n example.Fast
    

    哪个输出

    [info] Tags101Spec:
    [info] A Stack
    [info] A String
    [info] - should return 0 size when empty
    [info] A Sorted List of 10 numbers
    [info] Run completed in 158 milliseconds.
    [info] Total number of tests run: 1
    [info] Suites: completed 1, aborted 0
    [info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
    [info] All tests passed.
    

    【讨论】:

    • 谢谢@Mario。使用 "example.Slow" 这样的限定名称有什么好处?
    猜你喜欢
    • 2017-11-17
    • 2018-11-15
    • 2016-07-13
    • 2018-09-18
    • 2018-02-12
    • 1970-01-01
    • 2015-09-11
    • 2014-03-13
    • 1970-01-01
    相关资源
    最近更新 更多