多次重新运行一组 JUnit 测试
我有一组测试来测试一些 timzeone 相关功能,
我有一个 Before 方法,将默认时区设置为我选择的一个,以及一个 after 方法,用于恢复测试之前的默认值。
所以我基本上想做 将
时区设置为 UTC
运行测试
恢复时区
将时区设置为 EST
运行测试
恢复时区
将时区设置为 JST
运行测试
恢复时区
每种情况下的测试都是相同的。
有没有简单的方法可以做到这一点?
I've got a set of tests that test some timzeone related functionality
I have a Before method that sets the default timezone to one of my choosing, and an after method that restores whatever the default was before the tests.
So I basically want to do
Set timezone to UTC
Run tests
Restore timezone
Set timezone to EST
Run tests
Restore timezone
Set timezone to JST
Run tests
Restore timezone
The tests are identical in each case.
Is there an easy way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,有一个很好的方法可以一遍又一遍地进行相同的测试,只需使用其他一些数据集。解决方案是“@RunWith(Parameterized.class)” - 链接到 javadoc< /a>.我假设您正在使用 JUnit 4 - 但据我所知,TestNG 也具有此类功能。
您需要编写的代码将类似于以下内容:
Yup, there is a nice way to do the same test over and over again, just with some other dataset. The solution is "@RunWith(Parameterized.class)" - link to javadoc. I'm assuming you're using JUnit 4 - but TestNG also has such functionality from what I remember.
The code you'll need to write will look somewhat like this:
我通过使用参数化和 RunWith 符号解决了这个问题:
请参阅: http://ourcraft.wordpress.com/2008/08/27/writing-a-parameterized-junit-test/
I solved the problem by using the Parameterized and RunWith notations:
See: http://ourcraft.wordpress.com/2008/08/27/writing-a-parameterized-junit-test/
这是我写的一篇文章,展示了通过代码示例重复运行测试的几种方法:
http://codehowtos.blogspot.com/2011/04/run -junit-test-repeatedly.html
您可以使用@Parametrized运行器,或使用帖子中包含的特殊运行器
Here is a post I wrote that shows several ways of running the tests repeatedly with code examples:
http://codehowtos.blogspot.com/2011/04/run-junit-test-repeatedly.html
You can use the @Parametrized runner, or use the special runner included in the post
最近我创建了 zohhak 项目。你可能会发现它很有用。它可以让你写:
recently I created zohhak project. you may find it useful. it lets you write: