天天看點

【TestNG】——自動化測試架構

背景

随着科技的發展,業務做的也是越來越大,越來越複雜,是以導緻我們的業務編碼也是越來越複雜,對于程式員和測試來說無疑是增加更多的測試難度,是以需要一個成熟的測試架構勢在必行,TestNG應該是程式員和測試人員一個比較好的選擇,這裡就不說在這個基礎上衍生的自動化測試架構了,隻是說程式員怎麼借助TestNG來更好的友善我們做單元測試,一次性搞定各種情況。

一、IDEA+SpringBoot+TestNG

1、依賴

<dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.0.0</version>
            <scope>test</scope>
        </dependency>
           

2、IDEA下載下傳插件

【TestNG】——自動化測試架構

二、代碼示範

1、建立測試類

public class TeatNGTest {
    @Test(groups = "test1")
    @Parameters({"param1","param2"})
    public void test1(String param1,String param2){
        System.out.println("test1 ceshi");
        System.out.println("param1="+param1+",param2="+param2);
    }

    @Test(groups = "test2")
    @Parameters({"param1","param2"})
    public void test2(String param1,String param2){
        System.out.println("test2 ceshi");
        System.out.println("param1="+param1+",param2="+param2);
    }

    @Test(groups = "test3")
    @Parameters({"param1","param2"})
    public void test3(String param1,String param2){
        System.out.println("test3 ceshi");
        System.out.println("param1="+param1+",param2="+param2);
    }


}
           

2、點選需要測試的類右鍵選擇Create TestNG XML

【TestNG】——自動化測試架構

3、編輯TestNG.xml檔案

三、配置檔案

1、需要測試的類需要加上TestNG的注解

【TestNG】——自動化測試架構

2、TestNG.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
    <!--分組對應測試方法上的分組,name屬性對應測試方法上定義的group名,
    如果包含就是include,不包含就是exclude,如果添加分組了,下面的
    methods就不需要了,直接通過分組來控制粒度,如果不需要分組就不需要groups
    标簽,直接放開methods标簽,指定執行的方法,同時該可以傳簡單的參數,
    但是複雜的參數不行,parameter标簽的參數與方法上的注解屬性相同,
    如果有不同的案例,那就建立不同的test标簽-->
    <groups>
        <run>
            <include name="test1"/>
            <exclude name="test2"/>
            <exclude name="test3"/>
        </run>
    </groups>

    <test verbose="2" preserve-order="true"
          name="測試用例1">
        <parameter name="param1" value="1"/>
        <parameter name="param2" value="2"/>
        <classes>
            <class name="測試類的全限定名">
                <!--<methods>-->
                    <!--<include name="test1"/>-->
                    <!--<include name="test2"/>-->
                    <!--<include name="test3"/>-->
                <!--</methods>-->
            </class>
        </classes>
    </test>

    <test verbose="2" preserve-order="true"
          name="測試用例2">
        <parameter name="param1" value="1"/>
        <parameter name="param2" value="2"/>
        <classes>
            <class name="測試類的全限定名">
                <!--<methods>-->
                    <!--<include name="test1"/>-->
                    <!--<include name="test2"/>-->
                    <!--<include name="test3"/>-->
                <!--</methods>-->
            </class>
        </classes>
    </test>
</suite>
           

四、執行

1、點選testNG.xml,右鍵執行

【TestNG】——自動化測試架構

2、結果

Connected to the target VM, address: '127.0.0.1:52363', transport: 'socket'
test1 ceshi
param1=1,param2=2
Disconnected from the target VM, address: '127.0.0.1:52363', transport: 'socket'
test1 ceshi
param1=1,param2=2

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

3、導出檔案

【TestNG】——自動化測試架構

參考文章:https://www.yiibai.com/testng/basic-annotations.html