首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在并行执行模式下,只需执行一次setup方法

在并行执行模式下,只需执行一次setup方法
EN

Stack Overflow用户
提问于 2017-07-28 18:27:51
回答 1查看 1.1K关注 0票数 0

我已经用testng设置了并行测试用例执行,但我只需要执行一次setup方法。

BeforeClass,BeforeMethod也是针对单个线程执行的。但我需要在所有线程之前执行一次方法。

如何通过TestNG设置来实现这一点?

代码语言:javascript
复制
package com.howtodoinjava.parallelism;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParallelSuiteTest 
{
    String testName = "";

    @BeforeTest
    @Parameters({ "test-name" })
    public void beforeTest(String testName) {
        this.testName = testName;
        long id = Thread.currentThread().getId();
        System.out.println("Before test " + testName + ". Thread id is: " + id);
    }

    @BeforeClass
    public void beforeClass() {
        long id = Thread.currentThread().getId();
        System.out.println("Before test-class " + testName + ". Thread id is: "
                + id);
    }

    @Test
    public void testMethodOne() {
        long id = Thread.currentThread().getId();
        System.out.println("Sample test-method " + testName
                + ". Thread id is: " + id);
    }

    @AfterClass
    public void afterClass() {
        long id = Thread.currentThread().getId();
        System.out.println("After test-method  " + testName
                + ". Thread id is: " + id);
    }

    @AfterTest
    public void afterTest() {
        long id = Thread.currentThread().getId();
        System.out.println("After test  " + testName + ". Thread id is: " + id);
    }
}

testng.xml

代码语言:javascript
复制
<suite name="Test-class Suite" parallel="tests" thread-count="2">
    <test name="Test-class test 1">
        <parameter name="test-name" value="test-method One" />
        <classes>
            <class name="com.howtodoinjava.parallelism.ParallelSuiteTest" />
        </classes>
    </test>
    <test name="Test-class test 2">
        <parameter name="test-name" value="test-method One" />
        <classes>
            <class name="com.howtodoinjava.parallelism.ParallelSuiteTest" />
        </classes>
    </test>
</suite>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-29 12:07:13

下面的示例应该可以解释我的建议。

代码语言:javascript
复制
package com.rationaleemotions.stackoverflow.qn45371087;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class ParallelSuiteTest {
    private static final Object lock = new Object();
    private static boolean initialised = false;

    @BeforeClass
    public void beforeClass() {
        synchronized (lock) {
            if (!initialised) {
                init();
                initialised = true;
            }
        }
    }

    private void init() {
        System.err.println("Initialisation done");
    }

    @Test
    public void testMethodOne() {
        System.err.println("This is a test method running on [" + Thread.currentThread().getId() + "]");
    }

}

套件xml文件

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="45371087_Suite" verbose="2" parallel="tests" thread-count="10">
    <test name="45371087_Tests_1">
        <classes>
            <class name="com.rationaleemotions.stackoverflow.qn45371087.ParallelSuiteTest"/>
        </classes>
    </test>
    <test name="45371087_Tests_2">
        <classes>
            <class name="com.rationaleemotions.stackoverflow.qn45371087.ParallelSuiteTest"/>
        </classes>
    </test>
</suite>

下面是输出:

代码语言:javascript
复制
...
... TestNG 6.12 by Cédric Beust (cedric@beust.com)
...
Initialisation done
This is a test method running on [12]
This is a test method running on [11]

===============================================
45371087_Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45371087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档