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

你可能感兴趣的:(TestNG)