多次运行TestNG测试

时间:2017-11-04 23:58:13

标签: java gradle testng stress-testing

我想使用Gradle和TestNG / JUnit实现某种压力测试。

我发现https://stackoverflow.com/a/41650455与JUnit完全正常,但它不能与TestNG一起使用。我创建了一个小型回购来展示我的问题:

https://github.com/dhardtke/gradle-testng-stresstest

如果我运行gradlew stressTestJUnit我正确接收

JUnitTests > testNothing STANDARD_ERROR
    I am a JUnit test!

JUnitTests > testNothing STANDARD_ERROR
    I am a JUnit test!

JUnitTests > testNothing STANDARD_ERROR
    I am a JUnit test!

JUnitTests > testNothing STANDARD_ERROR
    I am a JUnit test!

JUnitTests > testNothing STANDARD_ERROR
    I am a JUnit test!

但在运行gradlew stressTestTestNG时我只会收到:

Gradle suite > Gradle test > TestNGTests.testNothing STANDARD_ERROR
    I am a TestNG test!

在尝试执行TestNG测试而不是JUnit测试时,我已经尝试弄清楚Gradle做了哪些不同的事情,但还是无法解决(还有):https://github.com/gradle/gradle/tree/master/subprojects/testing-jvm/src/main/java/org/gradle/api/internal/tasks/testing/testng

1 个答案:

答案 0 :(得分:0)

我终于找到了如何做到这一点:

的build.gradle:

task testNG(type: StressTest) {
    useTestNG() {
        options.listeners << "core.test.TestNgStressTestListener"
        environment "times", times
        environment "stressTest", true
    }
}

和我的TestNgStressTestListener班级:

@Override
public void alter(final List<XmlSuite> suites) {
    final boolean stressTest = Boolean.parseBoolean(System.getenv("stressTest"));

    if (stressTest) {
        final int times = Integer.parseInt(System.getenv("times"));

        final XmlSuite base = suites.get(0);
        suites.clear();
        final String baseName = base.getName();

        for (int i = 0; i < times; i++) {
            final XmlSuite copy = (XmlSuite) base.clone();
            copy.setName(String.format("%s (%d)", baseName, i + 1));

            suites.add(copy);
        }
    }
}

给定的侦听器只复制第一个XmlSuite并更改其名称(否则将在TestNG内部抛出异常)。

像魅力一样!