如果我们将 parallel="tests" 放在套件 xml 中,@beforeclass 方法将在同一个线程中执行

@beforeclass methods get exexcuted in same thread if we put parallel="tests" in the suite xml

我有一个测试套件,我在其中通过使用注释来使用并行性,例如: 测试名称="All Regular Tests" 并行="tests" 线程数=“30”

现在,假设我们在一个 class 中有多个 @test 注释。它也有@beforeclass注解方法。因此,当单独的线程将从相同的 class 中选择测试时,它会在两个线程中执行 @beforeclass 方法还是会共享相同的数据。

或者我应该使用parallel="methods",正确的方法是什么?

无法理解并行的概念。

我明白你为什么感到困惑了。

在测试标签中使用 parallel="tests" 没有任何意义。 parallel="tests" 表示 XML 中的所有定义在不同的线程中都是 运行。但是您将其分配给测试级别,因此它只会将该选项应用于该测试。

这里有两个选择。

将多线程选项置于套件级别,使用 "tests" 并行化: <suite name = "ParallelTesting" parallel="tests" thread-count="2">

或者将其置于测试级别,使用 "method" 选项: <test name = "Test Parallelism" parallel="method" thread-count="2">

所以:

  • "tests": XML 文件中的每个。
  • "method": 对于每个 @Test 方法,无论您将 属性 放在何处(套件中的所有@Tests,套件中的所有@Tests一个 class,等等)

关于你的第二个问题,不,它只会 运行 @BeforeClass 一次。您可以很容易地对其进行测试:

XML 文件:并行=套件级别的测试:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "ParallelTesting" parallel="tests" thread-count="2">

    <test name = "Test Parallelism 1" >
        <classes>
            <class name = "com.test.testing.test.TestClass"/>
        </classes>
    </test>

    <test name = "Test Parallelism 2" >
        <classes>
            <class name = "com.test.testing.test.TestClass2"/>
        </classes>
    </test>

</suite>

测试Class1:

public class TestClass {

    @BeforeClass
    public void beforeClass() {
        System.out.println("Class 1 - Before Class with Thread Id:- "+ Thread.currentThread().getId());
    }

    @Test
    public void testA() {
        System.out.println("Class 1 - Test Case A with Thread Id:- " + Thread.currentThread().getId());
    }

    @Test
    public void testB() {
        System.out.println("Class 1 - Test Case B with Thread Id:- " + Thread.currentThread().getId());
    }

}

测试Class2:

public class TestClass2 {

    @BeforeClass
    public void beforeClass() {
        System.out.println("Class 2 - Before Class with Thread Id:- "+ Thread.currentThread().getId());
    }

    @Test
    public void testA() {
        System.out.println("Class 2 - Test Case A with Thread Id:- " + Thread.currentThread().getId());
    }

    @Test
    public void testB() {
        System.out.println("Class 2 - Test Case B with Thread Id:- " + Thread.currentThread().getId());
    }

}

输出:

Class 1 - Before Class with Thread Id:- 12
Class 2 - Before Class with Thread Id:- 13
Class 2 - Test Case A with Thread Id:- 13
Class 1 - Test Case A with Thread Id:- 12
Class 2 - Test Case B with Thread Id:- 13
Class 1 - Test Case B with Thread Id:- 12

===============================================
ParallelTesting
Total tests run: 4, Failures: 0, Skips: 0
===============================================

并行化的另外两个变体是(使用上面相同的 classes):

XML 文件:并行=套件级别的方法:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "ParallelTesting2" parallel="methods" thread-count="2">

    <test name = "Test Parallelism 1" >
        <classes>
            <class name = "com.test.testing.test.TestClass"/>
        </classes>
    </test>

    <test name = "Test Parallelism 2" >
        <classes>
            <class name = "com.test.testing.test.TestClass2"/>
        </classes>
    </test>

</suite>

输出:

Class 1 - Before Class with Thread Id:- 12
Class 1 - Test Case B with Thread Id:- 13
Class 1 - Test Case A with Thread Id:- 12
Class 2 - Before Class with Thread Id:- 14
Class 2 - Test Case A with Thread Id:- 14
Class 2 - Test Case B with Thread Id:- 15

XML 文件:并行=测试级别的方法:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "ParallelTesting2" >

    <test name = "Test Parallelism 1" parallel="methods" thread-count="2">
        <classes>
            <class name = "com.test.testing.test.TestClass"/>
        </classes>
    </test>

    <test name = "Test Parallelism 2" parallel="methods" thread-count="2">
        <classes>
            <class name = "com.test.testing.test.TestClass2"/>
        </classes>
    </test>

</suite>

输出:

Class 1 - Before Class with Thread Id:- 12
Class 1 - Test Case A with Thread Id:- 12
Class 1 - Test Case B with Thread Id:- 13
Class 2 - Before Class with Thread Id:- 14
Class 2 - Test Case B with Thread Id:- 15
Class 2 - Test Case A with Thread Id:- 14

最后,我提到的是没有意义的:

XML 文件:并行=测试级别测试:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "ParallelTesting2" >

    <test name = "Test Parallelism 1" parallel="tests" thread-count="2">
        <classes>
            <class name = "com.test.testing.test.TestClass"/>
        </classes>
    </test>

    <test name = "Test Parallelism 2" parallel="tests" thread-count="2">
        <classes>
            <class name = "com.test.testing.test.TestClass2"/>
        </classes>
    </test>

</suite>

输出:

Class 1 - Before Class with Thread Id:- 1
Class 1 - Test Case A with Thread Id:- 1
Class 1 - Test Case B with Thread Id:- 1
Class 2 - Before Class with Thread Id:- 1
Class 2 - Test Case A with Thread Id:- 1
Class 2 - Test Case B with Thread Id:- 1


正如您在最后一个示例中看到的那样,所有 运行 都在同一线程上,因为您要求每个测试(方法组)并行进行 运行 测试。