排除参数化测试类中的非参数测试
JUnit 中是否有任何注释可以排除参数化测试类中的非参数测试?
Is there any annotation in JUnit to exclude a non param test in parameterized test class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
JUnit 5
从 Junit 5.0.0 开始,您现在可以使用
@ParameterizedTest
注释您的测试方法。所以不需要内部类。除了 ValueSource 之外,还有多种方法可以向参数化测试提供参数,如下所示。有关详细信息,请参阅官方 junit 用户指南 :JUnit 4
如果您仍在使用 Junit 4(我使用 v4.8.2 进行了测试),您可以将封闭运行器与内部类和参数化运行器结合使用:
JUnit 5
As of Junit 5.0.0 you can now annotate your test methods with
@ParameterizedTest
. So no need for inner classes. There are many ways to supply the arguments to the parameterized test apart from ValueSource as shown below. See the official junit user guide for details:JUnit 4
If you are still using Junit 4 (I tested with v4.8.2) you can use the Enclosed runner in conjunction with inner classes and the Parameterized runner:
否。最佳实践是将这些非参数化测试移至不同的类(.java 文件)
No. The best practice is to move those non-parameterized tests to a different class (.java file)
我能够做一些与 Matthew Madson 答案非常相似的事情,并发现创建一个基类来封装单个测试和参数测试之间的设置和通用辅助函数很有用。这无需使用Enclosure.class即可工作。
I was able to do something very similar to Matthew Madson answer and found it useful to create a Base Class to encapsulate setup and common helper functions between the single and param tests. This works without using Enclosed.class.
Zohhak 测试运行程序 是参数化特定测试的更简单方法。
谢谢彼得!
Zohhak test runner is a simpler way to parameterize specific tests.
Thanks Piotr!
看来 TestNG 不会遇到这个问题。
我并不那么绝望,所以我修改了内置的参数化类来支持此功能。只需将适用的测试注释为@NonParameterized。
请注意,此类仅适用于其 on 注释,即检查您的导入。
更新: 我正在尝试将此类内容添加到 junit 中。
It appears that TestNG does not suffer from this problem.
I'm not that desperate so I modified the builtin Parameterized class to support this feature. Just annotate applicable tests as @NonParameterized.
Note that this class only works with its on annotations, i.e. check your imports.
Update: I'm trying to get this sort of thing added to junit.
我做了类似于马修解决方案的事情。但是,我创建了两个新的 java 文件来扩展当前文件,以便 ComponentSingleTests 不会运行两次。这样,它们就可以共享父类中的公共成员变量和辅助方法。我在 Matthew 的解决方案中遇到的问题是,我的单个测试运行了两次而不是一次,因为 Enlined.class(扩展了 Suite.class)将使您的测试运行两次,如此链接中所述
防止 junit 测试运行两次
ComponentTest.java
ComponentParamTests.java
ComponentSingleTests.java
I did something similar to Matthew's Solution. However, I created two new java files that extends the current file so that the ComponentSingleTests do not run twice. This way, they can share common member variables and helper methods from the parent class. The problem I had with Matthew's Solution was that my single test was running twice instead of once as the Enclosed.class (which extends the Suite.class) will make your test run twice as described in this link
Prevent junit tests from running twice
ComponentTest.java
ComponentParamTests.java
ComponentSingleTests.java
对于那些希望参数来自java函数而不是注释的人:
来源:https://www.baeldung.com/parameterized-tests-junit-5
For those who wants the parameters come from java function instead of annotations:
source: https://www.baeldung.com/parameterized-tests-junit-5
假设您使用 Parametrized.class 来运行测试类 - 将所有非参数化测试标记为 @Ignored。否则,您可以创建一个静态内部类来对所有参数化测试进行分组,另一个用于非参数化测试。
Assuming you use Parametrized.class to run your test class - mark all non-parametrized tests as @Ignored. Otherwise you can make a static inner class to group all your parametrized tests and another - for non-parametrized.
我在 Spring Boot MockMvc 中编写测试时遇到了这个问题
我只是在单独的 java 文件中创建了两个类(一个用于 ParameterizedTest ,另一个用于 SingleTest ),并为它们创建一个套件。因为内部类为静态成员创建错误,而不是静态成员和类。
I stuck in this problem while I'm writing test in spring boot MockMvc
I simply created two classes in separate java files ( one for ParameterizedTest and other for SingleTest ) and create a suite for them. because inner classes were creating error for static members and and not static members and class.