接口自动化测试单元测试Unit Test 自动化

(八)TestNG学习之路—注解详述之并发

2018-09-04  本文已影响3人  Tomandy

前言

TestNG支持多线程(并发)的方式执行测试,相对于传统的单线程方式,一个显而易见的优势便是提升测试执行效率。

并发

TestNG官方文档给出了以下几种并发执行测试的方式。

1.suite并发

一个xml文件只包含一个suite标签,如果想多线程执行不同的suite,可通过以下方式来实现。

java org.testng.TestNG -suitethreadpoolsize 3 testng1.xml testng2.xml testng3.xml
2.tests,classes,methods,instances并发

testng.xml测试套件下的test,classe,method级别的并发,可通过以下方式配置。

<suite name="My suite" parallel="tests" thread-count="5">

示例:
编写parallel1,parallel2,parallel3类如下。

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

public class parallel1 {

    @BeforeClass
    public void bfClass(){
        long id = Thread.currentThread().getId();
        System.out.println("parallel1 BeforeClass threadId: "+id);
    }

    @Test
    public void test(){
        long id = Thread.currentThread().getId();
        System.out.println("parallel1 Test threadId: "+id);
    }

    @AfterClass
    public void afClass(){
        long id = Thread.currentThread().getId();
        System.out.println("parallel1 AfterClass threadId: "+id);
    }
}
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class parallel2 {
    @BeforeClass
    public void bfClass(){
        long id = Thread.currentThread().getId();
        System.out.println("parallel2 BeforeClass threadId: "+id);
    }

    @Test
    public void test(){
        long id = Thread.currentThread().getId();
        System.out.println("parallel2 Test threadId: "+id);
    }

    @AfterClass
    public void afClass(){
        long id = Thread.currentThread().getId();
        System.out.println("parallel2 AfterClass threadId: "+id);
    }
}
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class parallel3 {

    @BeforeClass
    public void bfClass(){
        long id = Thread.currentThread().getId();
        System.out.println("parallel3 BeforeClass threadId: "+id);
    }

    @Test
    public void test(){
        long id = Thread.currentThread().getId();
        System.out.println("parallel3 Test threadId: "+id);
    }

    @AfterClass
    public void afClass(){
        long id = Thread.currentThread().getId();
        System.out.println("parallel3 AfterClass threadId: "+id);
    }
}

testng.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" parallel="tests">
    <test name="test1">
        <classes>
            <class name="parallel1"/>
            <class name="parallel3"/>
        </classes>
    </test>

    <test name="test2">
        <classes>
            <class name="parallel2"/>
        </classes>
    </test>

</suite>

执行结果如下:

parallel1 BeforeClass threadId: 9
parallel2 BeforeClass threadId: 10
parallel2 Test threadId: 10
parallel1 Test threadId: 9
parallel2 AfterClass threadId: 10
parallel1 AfterClass threadId: 9
parallel3 BeforeClass threadId: 9
parallel3 Test threadId: 9
parallel3 AfterClass threadId: 9

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

根据执行结果输出的线程id可以判断:TestNG将在同一个线程中运行相同test标签中的所有方法,不同test 标签下的用例在不同的线程执行。

<suite name="My suite" parallel="classes" thread-count="5">

示例:
修改testng.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" parallel="classes">
    <test name="test1">
        <classes>
            <class name="parallel1"/>
            <class name="parallel3"/>
        </classes>
    </test>

    <test name="test2">
        <classes>
            <class name="parallel2"/>
        </classes>
    </test>

</suite>

执行结果:

parallel3 BeforeClass threadId: 10
parallel1 BeforeClass threadId: 9
parallel1 Test threadId: 9parallel3 Test threadId: 10
parallel3 AfterClass threadId: 10
parallel1 AfterClass threadId: 9
parallel2 BeforeClass threadId: 11
parallel2 Test threadId: 11
parallel2 AfterClass threadId: 11

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

如上所示,不同的class在不同的线程中执行,同一class的所有方法在同一线程中执行。

<suite name="My suite" parallel="methods" thread-count="5">

示例:
paralle1类增加test1方法:

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

public class parallel1 {

    @BeforeClass
    public void bfClass(){
        long id = Thread.currentThread().getId();
        System.out.println("parallel1 BeforeClass threadId: "+id);
    }

    @Test
    public void test(){
        long id = Thread.currentThread().getId();
        System.out.println("parallel1 Test threadId: "+id);
    }

    @Test
    public void test1(){
        long id = Thread.currentThread().getId();
        System.out.println("parallel1 Test1 threadId: "+id);
    }

    @AfterClass
    public void afClass(){
        long id = Thread.currentThread().getId();
        System.out.println("parallel1 AfterClass threadId: "+id);
    }
}

修改testng.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" parallel="methods">
    <test name="test1">
        <classes>
            <class name="parallel1">
                <methods>
                    <include name="test"/>
                    <include name="test1"/>
                </methods>
            </class>
            <class name="parallel3"/>
        </classes>
    </test>

    <test name="test2">
        <classes>
            <class name="parallel2"/>
        </classes>
    </test>

</suite>

执行结果如下:

parallel3 BeforeClass threadId: 11
parallel1 BeforeClass threadId: 9
parallel1 Test threadId: 9
parallel3 Test threadId: 11
parallel1 Test1 threadId: 10
parallel3 AfterClass threadId: 11
parallel1 AfterClass threadId: 9
parallel2 BeforeClass threadId: 12
parallel2 Test threadId: 12
parallel2 AfterClass threadId: 12

===============================================
All Test Suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================

如上结果所示,可发现每一个被@Test标注的方法都运行在单独的线程。

<suite name="My suite" parallel="instances" thread-count="5">

示例:
修改parallel1类如下:

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

public class parallel1 {

    @BeforeClass
    public void bfClass(){
        long id = Thread.currentThread().getId();
        System.out.println(this.toString()+" "+ "parallel1 BeforeClass threadId: "+id);
    }

    @Test
    public void test(){
        long id = Thread.currentThread().getId();
        System.out.println(this.toString()+" "+ "parallel1 Test threadId: "+id);
    }

    @Test
    public void test1(){
        long id = Thread.currentThread().getId();
        System.out.println(this.toString()+" "+ "parallel1 Test1 threadId: "+id);
    }

    @AfterClass
    public void afClass(){
        long id = Thread.currentThread().getId();
        System.out.println(this.toString()+" "+ "parallel1 AfterClass threadId: "+id);
    }
}

增加一个工厂类TestParallel1Factory 如下:

import org.testng.annotations.Factory;

public class TestParallel1Factory {
    @Factory
    public Object[] testParallel1(){
        Object[] objects = new Object[3];
        for(int i=0;i<3;i++){
            objects[i] = new parallel1();
        }
        return objects;
    }
}

修改testng.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" parallel="instances" >
    <test name="test">
        <classes>
            <class name="TestParallel1Factory"/>
        </classes>
    </test>
</suite>

执行结果如下:

parallel1@1ff8b8f parallel1 BeforeClass threadId: 9
parallel1@1c6a36e parallel1 BeforeClass threadId: 11
parallel1@7c703b parallel1 BeforeClass threadId: 10
parallel1@1ff8b8f parallel1 Test threadId: 9
parallel1@7c703b parallel1 Test threadId: 10
parallel1@1c6a36e parallel1 Test threadId: 11
parallel1@7c703b parallel1 Test1 threadId: 10
parallel1@1ff8b8f parallel1 Test1 threadId: 9
parallel1@1c6a36e parallel1 Test1 threadId: 11
parallel1@1c6a36e parallel1 AfterClass threadId: 11
parallel1@1ff8b8f parallel1 AfterClass threadId: 9
parallel1@7c703b parallel1 AfterClass threadId: 10

===============================================
All Test Suite
Total tests run: 6, Failures: 0, Skips: 0
===============================================

如上结果所示,每个实例的所有方法都运行在同一个线程内,但是不同实例运行在不同线程内。

3.同一个方法的并发

示例如下,各属性的用法在前面的文章有详细介绍。

@Test(threadPoolSize = 3, invocationCount = 10,  timeOut = 10000)
public void testServer() {
}
4.dataProvider并发

《(五)TestNG学习之路—注解详述之参数化》文章末有提及,此处不再详述。

上一篇 下一篇

猜你喜欢

热点阅读