scala sbt 中的 ScalaTest:有没有办法在没有标签的情况下运行单个测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11159953/
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
ScalaTest in sbt: is there a way to run a single test without tags?
提问by Nacht
I know that a single test can be ran by running, in sbt,
我知道可以通过在 sbt 中运行来运行单个测试,
testOnly *class -- -n Tag
Is there a way of telling sbt/scalatest to run a single test without tags? For example:
有没有办法告诉 sbt/scalatest 运行没有标签的单个测试?例如:
testOnly *class -- -X 2
it would mean "run the second test in the class. Whatever it is". We have a bunch of tests and no one bothered to tag them, so is there a way to run a single test without it having a tag?
这意味着“在课堂上运行第二个测试。不管它是什么”。我们有一堆测试,没有人费心去标记它们,那么有没有办法在没有标记的情况下运行单个测试?
回答by Seth Tisue
This is now supported (since ScalaTest 2.1.3) within interactive mode:
现在在交互模式下支持(自 ScalaTest 2.1.3 起):
testOnly *MySuite -- -z foo
to run only the tests whose name includes the substring "foo".
仅运行名称包含子字符串“foo”的测试。
For exact match rather than substring, use -tinstead of -z.
对于精确匹配而不是子字符串,请使用-t代替-z。
回答by Tyler
I wanted to add a concrete example to accompany the other answers
我想添加一个具体的例子来配合其他答案
You need to specify the name of the class that you want to test, so if you have the following project (this is a Play project):
您需要指定要测试的类的名称,因此如果您有以下项目(这是一个 Play 项目):
You can test just the Logintests by running the following command from the SBT console:
您可以Login通过从 SBT 控制台运行以下命令来仅测试测试:
test:testOnly *LoginServiceSpec
If you are running the command from outside the SBT console, you would do the following:
如果您从 SBT 控制台外部运行该命令,您将执行以下操作:
sbt "test:testOnly *LoginServiceSpec"
回答by cfeduke
I don't see a way to run a single untagged test within a test class but I am providing my workflow since it seems to be useful for anyone who runs into this question.
我没有看到在测试类中运行单个未标记测试的方法,但我提供了我的工作流程,因为它似乎对遇到这个问题的任何人都有用。
From within a sbt session:
从 sbt 会话中:
test:testOnly *YourTestClass
(The asterisk is a wildcard, you could specify the full path com.example.specs.YourTestClass.)
(星号是通配符,您可以指定完整路径com.example.specs.YourTestClass。)
All tests within that test class will be executed. Presumably you're most concerned with failing tests, so correct any failing implementations and then run:
将执行该测试类中的所有测试。大概你最关心失败的测试,所以纠正任何失败的实现,然后运行:
test:testQuick
... which will only execute tests that failed. (Repeating the most recently executed test:testOnlycommand will be the same as test:testQuickin this case, but if you break up your test methods into appropriate test classes you can use a wildcard to make test:testQuicka more efficient way to re-run failing tests.)
...它只会执行失败的测试。(重复最近执行的test:testOnly命令将与test:testQuick本例相同,但如果您将测试方法分解为适当的测试类,则可以使用通配符以test:testQuick更有效的方式重新运行失败的测试。)
Note that the nomenclature for test in ScalaTest is a test class, not a specific test method, so all untagged methods are executed.
请注意,ScalaTest 中 test 的命名法是一个测试类,而不是特定的测试方法,因此执行所有未标记的方法。
If you have too many test methods in a test class break them up into separate classes or tag them appropriately. (This could be a signal that the class under test is in violation of single responsibility principleand could use a refactoring.)
如果测试类中有太多测试方法,请将它们分解为单独的类或适当地标记它们。(这可能表明被测类违反了单一职责原则,可以使用重构。)
回答by pme
Just to simplify the example of Tyler.
只是为了简化泰勒的例子。
test:-prefix is not needed.
test:- 不需要前缀。
So according to his example:
所以根据他的例子:
In the sbt-console:
在-sbt控制台中:
testOnly *LoginServiceSpec
And in the terminal:
在终端中:
sbt "testOnly *LoginServiceSpec"
回答by Powers
Here's the Scalatest page on using the runnerand the extended discussion on the -tand -zoptions.
这是关于使用 runner的 Scalatest 页面以及关于-t和-z选项的扩展讨论。
This post shows what commands work for a test file that uses FunSpec.
这篇文章展示了哪些命令适用于使用FunSpec.
Here's the test file:
这是测试文件:
package com.github.mrpowers.scalatest.example
import org.scalatest.FunSpec
class CardiBSpec extends FunSpec {
describe("realName") {
it("returns her birth name") {
assert(CardiB.realName() === "Belcalis Almanzar")
}
}
describe("iLike") {
it("works with a single argument") {
assert(CardiB.iLike("dollars") === "I like dollars")
}
it("works with multiple arguments") {
assert(CardiB.iLike("dollars", "diamonds") === "I like dollars, diamonds")
}
it("throws an error if an integer argument is supplied") {
assertThrows[java.lang.IllegalArgumentException]{
CardiB.iLike()
}
}
it("does not compile with integer arguments") {
assertDoesNotCompile("""CardiB.iLike(1, 2, 3)""")
}
}
}
This command runs the four tests in the iLikedescribe block (from the SBT command line):
此命令运行iLike描述块中的四个测试(来自 SBT 命令行):
testOnly *CardiBSpec -- -z iLike
You can also use quotation marks, so this will also work:
您也可以使用引号,因此这也适用:
testOnly *CardiBSpec -- -z "iLike"
This will run a single test:
这将运行一个测试:
testOnly *CardiBSpec -- -z "works with multiple arguments"
This will run the two tests that start with "works with":
这将运行以“works with”开头的两个测试:
testOnly *CardiBSpec -- -z "works with"
I can't get the -toption to run any tests in the CardiBSpecfile. This command doesn't run any tests:
我无法-t选择在CardiBSpec文件中运行任何测试。此命令不运行任何测试:
testOnly *CardiBSpec -- -t "works with multiple arguments"
Looks like the -toption works when tests aren't nested in describeblocks. Let's take a look at another test file:
-t当测试未嵌套在describe块中时,该选项似乎有效。我们再来看看另一个测试文件:
class CalculatorSpec extends FunSpec {
it("adds two numbers") {
assert(Calculator.addNumbers(3, 4) === 7)
}
}
-tcan be used to run the single test:
-t可用于运行单个测试:
testOnly *CalculatorSpec -- -t "adds two numbers"
-zcan also be used to run the single test:
-z也可用于运行单个测试:
testOnly *CalculatorSpec -- -z "adds two numbers"
See this repoif you'd like to run these examples.
如果您想运行这些示例,请参阅此 repo。


