为什么 JUnit 仅在第一次失败之前才运行理论测试用例?
最近,JUnit 添加了一个新概念 理论(从 v4.4 开始) )。
简而言之,您可以使用 @Theory
注解(而不是 @Test
)标记您的测试方法,使您的测试方法参数化并声明一个参数数组,用 < code>@DataPoints 注释位于同一类中的某处。
JUnit 将依次运行参数化测试方法,并依次传递从 @DataPoints
检索到的参数。但仅限于第一次此类调用失败(由于任何原因)。
这个概念似乎与 TestNG 中的@DataProviders 非常相似,但是当我们使用数据提供程序时,所有场景都会运行,无论其执行结果如何。它很有用,因为您可以看到有多少场景有效/无效,并且可以更有效地修复程序。
所以,我想知道为什么不对每个 @DataPoint
执行 @Theory
标记的方法? (从 Theories runner 继承并创建一个忽略失败的自定义 runner 似乎并不困难,但为什么我们没有开箱即用的这种行为?)
UPD:我创建了一个错误 - Theories runner 的宽容版本并可供公众访问:https://github.com/rgorodischer/fault-operative-theories
为了将其与标准 Theories 运行器进行比较,运行 StandardTheoriesBehaviorDemo,然后运行位于 src/test/ 下的 FaultTolerantTheoriesBehaviorDemo ...
文件夹。
Recently a new concept of Theories was added to JUnit (since v4.4).
In a nutshell, you can mark your test method with @Theory
annotation (instead of @Test
), make your test method parametrized and declare an array of parameters, marked with @DataPoints
annotation somewhere in the same class.
JUnit will sequentially run your parametrized test method passing parameters retrieved from @DataPoints
one after another. But only until the first such invocation fails (due to any reason).
The concept seems to be very similar to @DataProviders
from TestNG, but when we use data providers, all the scenarios are run inspite of their execution results. And it's useful because you can see how many scenarious work/don't work and you can fix your program more effectively.
So, I wonder what's the reason not to execute @Theory
-marked method for every @DataPoint
? (It appears not so difficult to inherit from Theories runner and make a custom runner which will ignore failures but why don't we have such behaviour out of the box?)
UPD: I have created a fault-tolerant version of Theories runner and made it available for a public access: https://github.com/rgorodischer/fault-tolerant-theories
In order to compare it with the standard Theories runner run StandardTheoriesBehaviorDemo then FaultTolerantTheoriesBehaviorDemo which are placed under src/test/...
folder.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要忽略断言失败,您还可以使用 JUnit 错误收集器规则:
例如,您可以编写这样的测试。
错误收集器使用 hamcrest Matchers。这是否积极取决于您的喜好。
To ignore assertion failures you can also use a JUnit error collector rule:
For example you can write a test like this.
The error collector uses hamcrest Matchers. Depending on your preferences this is positive or not.
AFAIK,这个想法与断言相同,第一次失败会停止测试。这就是参数化和参数化之间的区别。理论。
参数化采用一组数据点并针对每个数据点运行一组测试方法。理论也做了同样的事情,但是当第一个断言失败时就会失败。
尝试查看参数化。也许它提供了你想要的。
AFAIK, the idea is the same as with asserts, the first failure stops the test. This is the difference between Parameterized & Theories.
Parameterized takes a set of data points and runs a set of test methods with each of them. Theories does the same, but fails when the first assert fails.
Try looking at Parameterized. Maybe it provides what you want.
根据理论的定义,如果其中的单个测试是错误的,那么该理论就是错误的。如果您的测试用例不遵循此规则,则将它们称为“理论”是错误的。
A Theory is wrong if a single test in it is wrong, according to the definition of a Theory. If your test cases don't follow this rule, it would be wrong to call them a "Theory".