TestNG中group的用法

TestNG中的組能夠從多個類中篩選組屬性相同的方法執行。

好比有兩個類A和B,A中有1個方法a屬於組1,B中有1個方法b也屬於組1,那麼咱們能夠經過配置TestNG文件實現把這兩個類中都屬於1組的方法抽取出來執行。java

示例代碼

car1ui

package ngtest;

import org.testng.annotations.Test;

public class Car1 {
    @Test(groups={"driver"})//定義該方法屬於driver組
    public void driverWork(){
        System.out.println("car1's driver is driving");
    }
    
    @Test(groups={"boss"})//定義該方法屬於boss組
    public void bossWork(){
        System.out.println("car1's boss is talking");
    }
    
}

car2spa

package ngtest;

import org.testng.annotations.Test;

public class Car2 {
    @Test(groups={"driver"})//定義該方法屬於driver組
    public void driverWork(){
        System.out.println("car2's driver is driving");
    }
    
    @Test(groups={"boss"})//定義該方法屬於boss組
    public void bossWork(){
        System.out.println("car2's boss is talking");
    }
}

配置文件testng.xmlcode

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <groups>
        <run>
            <include name="driver"/><!--篩選driver組的方法來執行-->
        </run>
    </groups>
  <test name="Test">
    <classes>
      <class name="ngtest.Car1"/>
      <class name="ngtest.Car2"/>
    </classes>
  </test> 
</suite>

右鍵點擊testng.xml,選擇run as testNG suite,console輸出:xml

[TestNG] Running:
  D:\workspace\tester\testng.xml

car1's driver is driving
car2's driver is driving

===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

經過上面的運行結果能夠看出,配置文件中配置了兩個類Car1和Car2,經過groups標籤選擇了運行driver分組,因此兩個類中屬於該分組的方法獲得了執行。
額外知識:在java代碼中,@Test(groups={"driver"})能夠在大括號裏指定多個組,中間用逗號分開就行。在testng.xml中 標籤下還能夠書寫 標籤,表示不執行屬於abc組的用例。 ip

相關文章
相關標籤/搜索