以随机顺序运行 TestNG 测试

发布于 2024-12-08 05:11:08 字数 1105 浏览 0 评论 0原文

类似于 如何运行 JUnit 测试以随机顺序? ,我希望 TestNG 以随机顺序运行我的测试,这样意外的依赖关系就不会潜入。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夢归不見 2024-12-15 05:11:08

是的,“随机”确实应该是“不可预测”。

如果您想要真正的随机化,请查找 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.

内心激荡 2024-12-15 05:11:08

为了扩展 Cedric Beust 的答案,这是我执行此操作的代码,并得到了 来自 GitHub 的示例

import org.testng.IMethodInstance;
import org.testng.IMethodInterceptor;
import org.testng.ITestContext;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class TestOrderRandomizer implements IMethodInterceptor {
    @Override
    public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
        long seed = System.nanoTime();
        System.out.println(String.format("Randomizing order of tests with seed: %s", seed));
        Collections.shuffle(methods, new Random(seed));
        return methods;
    }
}

要使用它,请在测试之前添加此示例类:

import org.testng.annotations.Listeners;

@Listeners(TestOrderRandomizer.class)
public class TesterClass {
...

打印种子允许通过再次种植相同的种子来重现运行顺序。

To expand on Cedric Beust's answer, here's my code for doing this, with some help from this example from GitHub:

import org.testng.IMethodInstance;
import org.testng.IMethodInterceptor;
import org.testng.ITestContext;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class TestOrderRandomizer implements IMethodInterceptor {
    @Override
    public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
        long seed = System.nanoTime();
        System.out.println(String.format("Randomizing order of tests with seed: %s", seed));
        Collections.shuffle(methods, new Random(seed));
        return methods;
    }
}

And to use it, add this before your test class:

import org.testng.annotations.Listeners;

@Listeners(TestOrderRandomizer.class)
public class TesterClass {
...

Printing the seed allows to reproduce a run-order by planting the same seed again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文