scala 如何在 ScalaTest 中禁用测试套件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11516038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 04:21:48  来源:igfitidea点击:

How to disable test suite in ScalaTest

scalatestingscalatest

提问by lambdas

How to disable a test suite, i.e. all tests inside class extending FunSpec?

如何禁用测试套件,即类扩展中的所有测试FunSpec

The only solution I'd found is to replace itwith ignorein front of each test, but it is boring to do so with the dozens of tests.

我发现的唯一的解决办法是更换itignore每次试验前,但它是无聊与几十个测试这样做。

回答by Bill Venners

The easy way to do this in 1.8 is to add a constructor that takes a dummy argument. ScalaTest (and sbt) won't discover the class if it doesn't have a public, no-arg constructor:

在 1.8 中做到这一点的简单方法是添加一个带有虚拟参数的构造函数。如果 ScalaTest(和 sbt)没有公共的、无参数的构造函数,则它不会发现该类:

class MySuite(ignore: String) extends FunSuite { 
  // ...
}

In 2.0 you'll be able to just write @Ignore on the class:

在 2.0 中,您将能够只在类上写 @Ignore:

@Ignore
class MySuite extends FunSuite {
  // ...
}

回答by Mitch Sitin

You can use @Ignoreboth for distinct tests and the whole suit.

您可以将@Ignore用于不同的测试和整个套装。

回答by user272735

Tagthe troublesome tests and then exclude or includetests based on the tag.

标记麻烦的测试,然后根据标记排除或包含测试。

Tagging is a one time effort.

标记是一次性的。

回答by Malte Schwerhoff

Tags are one option, but you need to tag each tests accordingly, you cannot exclude complete suits that way. However, as user272735 mentioned, tagging tests is a one time effort.

标签是一种选择,但您需要相应地标记每个测试,您不能以这种方式排除完整的套装。但是,正如 user272735 所提到的,标记测试是一次性的。

Another option is defining a master suite and limiting the list of nestedSuitesto the suites you would like to run (see the docs). The obvious disadvantage is, that you have to maintain the master test suite, i.e., add newly created test suites to it. You could avoid this by recursing through your folders, creating a list of all test suits that can be found, and subtracting those that you put into a black list. In addition, recursing through your folders already gives you an opportunity the filter out files that match an (externally) given criterion.

另一种选择是定义一个主套件并将列表限制为nestedSuites您想要运行的套件(请参阅文档)。明显的缺点是,您必须维护主测试套件,即向其中添加新创建的测试套件。您可以通过遍历文件夹、创建所有可以找到的测试套装的列表并减去您放入黑名单的那些来避免这种情况。此外,遍历文件夹已经为您提供了过滤掉与(外部)给定标准匹配的文件的机会。

A third option - albeit more hacky - is to change the package to which the suites you want to black list belong. You can then use a test runner to only run suits included in certain packages. Read more in the docs, section "Specifying 'members-only' and 'wildcard' Suite paths".

第三种选择 - 尽管更笨拙 - 是更改要列入黑名单的套件所属的包。然后,您可以使用测试运行器仅运行包含在某些包中的套装。在文档中阅读更多信息,“指定‘仅限会员’和‘通配符’套件路径”部分

回答by James Moore

Use @DoNotDiscover

使用@DoNotDiscover

From the scaladoc:

Scaladoc

import org.scalatest._

@DoNotDiscover
class SetSpec extends FlatSpec {

  "An empty Set" should "have size 0" in {
    assert(Set.empty.size === 0)
  }

  it should "produce NoSuchElementException when head is invoked" in {
    intercept[NoSuchElementException] {
      Set.empty.head
    }
  }
}