gpt4 book ai didi

java - 在并行执行模式下只需要执行一次setup方法

转载 作者:行者123 更新时间:2023-12-02 02:41:32 28 4
gpt4 key购买 nike

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

BeforeClass、BeforeMethod 也会针对各个线程执行。但我需要在所有线程之前执行一次方法。

如何通过 TestNG 设置实现此目的?

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

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

最佳答案

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

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 文件

<?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>

这是输出:

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

关于java - 在并行执行模式下只需要执行一次setup方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45371087/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com