以随机顺序运行 TestNG 测试
类似于 如何运行 JUnit 测试以随机顺序? ,我希望 TestNG 以随机顺序运行我的测试,这样意外的依赖关系就不会潜入。
默认情况下,TestNG 将运行在 testng.xml 文件中找到的测试 随机顺序。
但是,我创建了一个小型测试项目,其中包含一个简单的 testng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="My suite">
<test name="Simple test">
<packages>
<package name="testngtests"></package>
</packages>
</test>
</suite>
包 testngtests
包含两个测试类(MyTest1、MyTest2),并且它们包含一些如下所示的空方法:
@Test
public void testOne(){
}
测试方法是都是空的,只是名称不同。
当我运行它们时(使用 Eclipse TestNG 运行程序或在命令行上),测试始终以相同的顺序运行(即按字母顺序排序,首先按类排序,然后按方法名称排序)。
那么文档是错误的吗?
或者“按随机顺序”只是意味着“没有保证的顺序”?那么如何让TestNG主动随机化测试顺序呢?
Similarly to How can I make my JUnit tests run in random order? , I'd like TestNG to run my tests in random order, so unintended dependencies cannot creep in.
The TestNG manual states:
By default, TestNG will run the tests found in your testng.xml file in
a random order.
However, I created a small test project, with a simple testng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="My suite">
<test name="Simple test">
<packages>
<package name="testngtests"></package>
</packages>
</test>
</suite>
The package testngtests
contains two test classes (MyTest1, MyTest2), and these contain a few empty methods like this:
@Test
public void testOne(){
}
The test mehods are all empty, and only differ by name.
When I run them (using the Eclipse TestNG runner, or on the command line), the tests consistenly run in the same order (namely sorted alphabetically, first by class and then by method name).
So is the documentation wrong?
Or does "in random order" simply mean "there is no guaranteed order"? Then how can I make TestNG actively randomize test order?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,“随机”确实应该是“不可预测”。
如果您想要真正的随机化,请查找 IMethodInterceptor,其中 TestNG 将为您提供将顺序更改为您喜欢的任何顺序的机会。
Yes, 'random' should really be 'non predictable'.
If you want true randomization, look up IMethodInterceptor, where TestNG will give you an opportunity to change the ordering to anything you like.
为了扩展 Cedric Beust 的答案,这是我执行此操作的代码,并得到了 来自 GitHub 的示例:
要使用它,请在测试之前添加此示例类:
打印种子允许通过再次种植相同的种子来重现运行顺序。
To expand on Cedric Beust's answer, here's my code for doing this, with some help from this example from GitHub:
And to use it, add this before your test class:
Printing the seed allows to reproduce a run-order by planting the same seed again.