以随机顺序运行 TestNG 测试 [英] Run TestNG tests in random order

查看:36
本文介绍了以随机顺序运行 TestNG 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于 我如何制作我的JUnit 测试以随机顺序运行?,我希望 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.

TestNG 手册说明:

默认情况下,TestNG 将运行在您的 testng.xml 文件中找到的测试随机顺序.

By default, TestNG will run the tests found in your testng.xml file in a random order.

但是,我创建了一个小测试项目,使用简单的 testng.xml:

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>

testngtests 包包含两个测试类(MyTest1、MyTest2),它们包含一些像这样的空方法:

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.

当我运行它们时(使用 Eclipse TestNG 运行程序,或在命令行上),测试始终以相同的顺序运行(即按字母顺序排序,首先按类,然后按方法名称).

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).

那么文档是错误的吗?

还是按随机顺序"只是意味着没有保证的顺序"?那么如何让TestNG主动随机化测试顺序呢?

Or does "in random order" simply mean "there is no guaranteed order"? Then how can I make TestNG actively randomize test order?

推荐答案

是的,随机"确实应该是不可预测的".

Yes, 'random' should really be 'non predictable'.

如果您想要真正的随机化,请查找 IMethodInterceptor,其中 TestNG 将让您有机会将顺序更改为您喜欢的任何内容.

If you want true randomization, look up IMethodInterceptor, where TestNG will give you an opportunity to change the ordering to anything you like.

这篇关于以随机顺序运行 TestNG 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆