以编程方式生成 JUnit 4 测试
我有一个 JUnit 4 测试,它创建测试数据并断言每个数据的测试条件。如果一切正确,我会得到绿色测试。
如果某一数据未通过测试,则整个测试的执行将被中断。我想对每个数据进行一个 JUnit 测试。是否可以以编程方式生成 JUnit 测试,以便我在 IDE 中获得大量测试?
采用这种方法的原因是为了更快地了解哪个测试失败,并在一个数据失败时继续剩余的测试。
I have a JUnit 4 test which creates test data and asserts the test condition for every single data. If everything is correct, I get a green test.
If one data fails the test, the execution of the whole test is interrupted. I would like to have a single JUnit test for each data. Is it possible to spawn JUnit tests programmatically, so that I get a lot of tests in my IDE?
The reason for this approach is to get a quicker overview which test fails and to continue the remaining tests, if one data fails.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您想要编写一个参数化测试(对不同的数据集进行完全相同的检查)。
有一个
Parameterized
类。此示例展示了如何使用它:请注意,
data()
方法返回要由test()
方法使用的数据。该方法可以从任何地方获取数据(例如与测试源一起存储的数据文件)。此外,没有什么可以阻止您在此类中使用多个
@Test
方法。这提供了一种对同一组参数执行不同测试的简单方法。It sounds like you'd want to write a parameterized test (that does the exact same checks on different sets of data).
There's the
Parameterized
class for this. This example shows how it can be used:Note that the
data()
method returns the data to be used by thetest()
method. That method could take the data from anywhere (say a data file stored with your test sources).Also, there's nothing that stops you from having more than one
@Test
method in this class. This provides an easy way to execute different tests on the same set of parameters.