TestNG 中 ParallelMode.METHODS,ParallelMode.CLASSES,ParallelMode.TESTS區別

在 TestNG 中,可以設置testNG 是否是多線程的,這時候 需要用到ParallelMode來定義
那麼ParallelMode.METHODS,ParallelMode.CLASSES, ParallelMode.TESTS 有什麼區別呢?

結論:
ParallelMode.METHODS:一個線程負責一個@Test標籤的程序,多個並行時,每個class,method之間的線程ID都不一樣
ParallelMode.CLASSES:一個線程 負責一個class裏面所有的帶有@Test標籤的method,多個並行時,每個class之間的線程id不一樣,但是同一個class裏的@Test的method線程id是一樣的
ParallelMode.TESTS:一個線程負責一個TestNG.xml中的一個< Test >標籤,多個並行時,每個 < Test > 所包含的class,method之間的線程ID 是一致的,但是 每個< Test > 之間的線程ID是不一致的

做個實驗:
ParallelMode.METHODS:

package com.howtodoinjava.parallelism;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

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

    @BeforeMethod
    public void beforeMethod() {
        long id = Thread.currentThread().getId();
        System.out.println("222 Before test-method. Thread id is: " + id);
    }

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

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

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

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

對應的testNG.xml 如下,

<suite name="Test-method Suite" parallel="methods" thread-count="2">
    <test name="Test-method test" group-by-instances="true">
        <classes>
        <class name="com.howtodoinjava.parallelism.ParallelClassesTestTwo" />

        </classes>
    </test>
</suite>

得到的結果爲:

[TestNG] Running:
  /home/fiona/UIAutomation-fiona-new/Test/src/methods-test-testng.xml

222 Before test-class. Thread id is: 11
222 Before test-method. Thread id is: 10
222 Before test-method. Thread id is: 11
222 Sample test-method Two. Thread id is: 11
222 Sample test-method One. Thread id is: 10
222 After test-method. Thread id is: 10
222 After test-method. Thread id is: 11
222 After test-class. Thread id is: 11

===============================================
Test-method Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

ParallelMode.CLASSES:

package com.howtodoinjava.parallelism;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

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

    @BeforeMethod
    public void beforeMethod() {
        long id = Thread.currentThread().getId();
        System.out.println("222 Before test-method. Thread id is: " + id);
    }

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

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

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

    @AfterClass
    public void afterClass() {
        long id = Thread.currentThread().getId();
        System.out.println("222 After test-class. Thread id is: " + id);
    }
}
package com.howtodoinjava.parallelism;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

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

    }
     @BeforeMethod
        public void beforeMethod() {
            long id = Thread.currentThread().getId();
            System.out.println("111 Before test-method. Thread id is: " + id);
        }

        @Test
        public void testMethodsOne() {
            long id = Thread.currentThread().getId();
            System.out.println("111 Simple test-method One. Thread id is: " + id);
        }

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

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

        }
}
<suite name="Test-method Suite" parallel="classes" thread-count="2">
    <test name="Test-method test" group-by-instances="true">
        <classes>
            <class name="com.howtodoinjava.parallelism.ParallelMethodTest" />
            <class name="com.howtodoinjava.parallelism.ParallelClassesTestTwo" />

        </classes>
    </test>
</suite>

In the result , you could clearly to see that thread 10 is only working for the 111, and thread 11 working for the 222.

[TestNG] Running:
  /home/fiona/UIAutomation-fiona-new/Test/src/methods-test-testng.xml

111 Before class-method. Thread id is: 10
111 Before test-method. Thread id is: 10
111 Simple test-method One. Thread id is: 10
111 After test-method. Thread id is: 10
111 After class-method. Thread id is: 10
222 Before test-class. Thread id is: 11
222 Before test-method. Thread id is: 11
222 Sample test-method One. Thread id is: 11
222 After test-method. Thread id is: 11
222 Before test-method. Thread id is: 11
222 Sample test-method Two. Thread id is: 11
222 After test-method. Thread id is: 11
222 After test-class. Thread id is: 11

===============================================
Test-method Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

ParallelMode.TESTS:
Same java code as above, with different testNG.xml:
1> 在同一個test標籤中:

<suite name="Test-method Suite" parallel="tests" thread-count="2">
    <test name="Test-method test" group-by-instances="true">
        <classes>
            <class name="com.howtodoinjava.parallelism.ParallelMethodTest" />
            <class name="com.howtodoinjava.parallelism.ParallelClassesTestTwo" />

        </classes>
    </test>
    </suite>

得到如下結果:

[TestNG] Running:
  /home/fiona/UIAutomation-fiona-new/Test/src/methods-test-testng.xml

111 Before class-method. Thread id is: 10
111 Before test-method. Thread id is: 10
111 Simple test-method One. Thread id is: 10
111 After test-method. Thread id is: 10
111 After class-method. Thread id is: 10
222 Before test-class. Thread id is: 10
222 Before test-method. Thread id is: 10
222 Sample test-method One. Thread id is: 10
222 After test-method. Thread id is: 10
222 Before test-method. Thread id is: 10
222 Sample test-method Two. Thread id is: 10
222 After test-method. Thread id is: 10
222 After test-class. Thread id is: 10

===============================================
Test-method Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

2> 在不同的Test標籤中

<suite name="Test-method Suite" parallel="tests" thread-count="2">
    <test name="Test-method test" group-by-instances="true">
        <classes>
            <class name="com.howtodoinjava.parallelism.ParallelMethodTest" />

        </classes>
    </test>

    <test name="Test-method test2" group-by-instances="true">
        <classes>
            <class name="com.howtodoinjava.parallelism.ParallelClassesTestTwo" />
        </classes>
    </test>
</suite>

得到結果如下:多線程出現

[TestNG] Running:
  /home/fiona/UIAutomation-fiona-new/Test/src/methods-test-testng.xml

222 Before test-class. Thread id is: 11
111 Before class-method. Thread id is: 10
111 Before test-method. Thread id is: 10
222 Before test-method. Thread id is: 11
111 Simple test-method One. Thread id is: 10
222 Sample test-method One. Thread id is: 11
222 After test-method. Thread id is: 11
111 After test-method. Thread id is: 10
111 After class-method. Thread id is: 10
222 Before test-method. Thread id is: 11
222 Sample test-method Two. Thread id is: 11
222 After test-method. Thread id is: 11
222 After test-class. Thread id is: 11

===============================================
Test-method Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章