java JUnit 中是否有任何条件注释来标记要跳过的几个测试用例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6096061/
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-30 14:18:45  来源:igfitidea点击:

is there any conditional annotation in JUnit to mark few test cases to be skipped?

javaunit-testingjunit

提问by Manoj

As far as I know to skip a test case the simplest thing to do is to remove the @Testannotation, but to do it over a large number of test cases is cumbersome. I was wondering if there is any annotation available in JUnit to turn off few test cases conditionally.

据我所知,跳过一个测试用例最简单的方法是删除@Test注释,但是在大量测试用例上进行操作很麻烦。我想知道 JUnit 中是否有任何注释可以有条件地关闭一些测试用例。

回答by Kaj

Hard to know if it is the @Ignoreannotation that you are looking for, or if you actually want to turn off certain JUnit tests conditionally. Turning off testcases conditionally is done using Assume.

很难知道它是否是@Ignore您正在寻找的注释,或者您是否真的想有条件地关闭某些 JUnit 测试。有条件地关闭测试用例是使用Assume.

You can read about assumptions in the release notesfor junit 4.5

您可以在 junit 4.5的发行说明中阅读有关假设的信息

There's also a rather good thread here on stack over flow: Conditionally ignoring tests in JUnit 4

这里还有一个关于堆栈溢出的相当不错的线程: 有条件地忽略 JUnit 4 中的测试

回答by Jon

As other people put here @Ignore ignores a test.

正如其他人所说,@Ignore 忽略了一个测试。

If you want something conditional that look at the junit assumptions.

如果您想要一些有条件的东西,请查看 junit 假设。

http://junit.sourceforge.net/javadoc/org/junit/Assume.html

http://junit.sourceforge.net/javadoc/org/junit/Assume.html

This works by looking at a condition and only proceeding to run the test if that condition is satisfied. If the condition is false the test is effectively "ignored".

这是通过查看条件并仅在满足该条件时才继续运行测试来实现的。如果条件为假,则测试被有效地“忽略”。

If you put this in a helper class and have it called from a number of your tests you can effectively use it in the way you want. Hope that helps.

如果你把它放在一个辅助类中,并从你的一些测试中调用它,你可以按照你想要的方式有效地使用它。希望有帮助。

回答by Sean Patrick Floyd

You can use the @Ignoreannotation which you can add to a single test or test class to deactivate it.

您可以使用@Ignore可以添加到单个测试或测试类的注释来停用它。

If you need something conditional, you will have to create a custom test runner that you can register using

如果您需要有条件的东西,您将不得不创建一个自定义测试运行器,您可以使用注册

@RunWith(YourCustomTestRunner.class)

You could use that to define a custom annotation which uses expression language or references a system property to check whether a test should be run. But such a beast doesn't exist out of the box.

您可以使用它来定义使用表达式语言或引用系统属性来检查是否应该运行测试的自定义注释。但是这样的野兽并不是开箱即用的。

回答by MarcoS

If you use JUnit 4.x, just use @Ignore. See here

如果您使用 JUnit 4.x,只需使用@Ignore. 看这里