一、什么是group

利用group可以对test method进行分类,还可以定义group的group。group这个parameter和@Test 、@BeforeSuite
@AfterSuite、@BeforeTest、
@AfterTest、
@BeforeGroups、@AfterGroups、@BeforeClass
@AfterClass、@BeforeMethod
@AfterMethod
一起使用,可以用来定义修饰method,也可以用来修饰class。

对于分类和配置testcase, 提供了极大的灵活和便利。

二、group使用例子

  • group 作用在 method level:
    @Test(groups = {"linux","functionTest"})
    public void testFunction_1() {
        System.out.println("functionTest_linux");
        Assert.assertTrue(true, "functionTest_linux pass");    
    }

    @Test(groups = {"windows","functionTest"})
    public void testFunction_2() {
        System.out.println("functionTest_windows");
        Assert.assertTrue(true, "functionTest_windows pass");    
    }

    @Test(groups = {"linux","regressionTest"})
    public void testRegression_1() {
        System.out.println("regressionTest_linux");
        Assert.assertTrue(true, "regressionTest_linux pass");    
    }
    
    @Test(groups = {"windows","regressionTest"})
    public void testRegression_2() {
        System.out.println("regressionTest_windows");
        Assert.assertTrue(true, "regressionTest_windows pass");    
    }

 

  • group 作用在class level:
@Test( groups = {"group on class"})
public class SampleGroupOnClass {
    @Test
    public void testGroupOnClass_1() {
        System.out.println("testGroupOnClass_1");
        Assert.assertTrue(true, "testGroupOnClass_1 pass");    
    }
    
    @Test
    public void testGroupOnClass_2() {
        System.out.println("testGroupOnClass_2");
        Assert.assertTrue(true, "testGroupOnClass_2 pass");    
    }
}

 

  • group在suite file中的定义,group的group
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestNG annotation Suite" parallel="false">
<!--     <listeners>
        <listener class-name="com.testNG.TestNGListener" />
    </listeners> -->

    <test name="Test on group concept">
        <groups>
            <define name="testPerpective">
                <include name="functionTest"/>
                <include name="performanceTest"/>
                <include name="regressionTest"/>   
            </define>
            <define name="platform">
                   <include name="windows"/>
                <include name="linux"/>
                <include name="solaris"/>
            </define>
            <define name="all">
                 <include name="platform"/>  
                 <include name="testPerpective"/>    
            </define>
            <run>
                <include name="testPerpective" />  
                <include name="group on class" />          
            </run>
        </groups>
        <classes>
            <class name="com.testNG.SampleGroup"/>
             <class name="com.testNG.SampleGroupOnClass"/>
        </classes>
    </test>
</suite>

 

三、通过例子直观理解

SampleGroup.java:  对method进行group,分为linux windows solaris, functionTest, performanceTest, regressionTest组;

SampleGroupOnClass.java: group作用在class level, 整个class被@Test修饰的method都属于一类;

group.xml:定义了group的group。

 

1)直接运行SampleGroup.java: 8个testcase

[RemoteTestNG] detected TestNG version 6.14.15
functionTest_linux
functionTest_windows
performanceTest_linux
performanceTest_windows
regressionTest_linux
regressionTest_windows
solaris_test1
solaris_test2
PASSED: testFunction_1
PASSED: testFunction_2
PASSED: testPerformance_1
PASSED: testPerformance_2
PASSED: testRegression_1
PASSED: testRegression_2
PASSED: test_1
PASSED: test_2

===============================================
     Default test
     Tests run: 8, Failures: 0, Skips: 0
===============================================

 

2) 直接运行SampleGroupOnClass.java:2个testcase

输出略:

3) 运行:group.xml

定义:

        <groups>
             <define name="testPerpective">
                 <include name="functionTest"/>
                 <include name="performanceTest"/>
                 <include name="regressionTest"/>  
             </define>
             <define name="platform">
                    <include name="windows"/>
                 <include name="linux"/>
                 <include name="solaris"/>
             </define>
             <define name="all">
                  <include name="platform"/> 
                  <include name="testPerpective"/>   
             </define>
             <run>
                 <include name="testPerpective" />  <!--  包含 SampleGroup.java 中的6个test method--> 
                 <include name="group on class" />  <!--  包含 SampleGroupOnClass.java 中的2个test method-->         
             </run>
         </groups>

输出:

functionTest_linux
functionTest_windows
performanceTest_linux
performanceTest_windows
regressionTest_linux
regressionTest_windows
testGroupOnClass_1
testGroupOnClass_2

===============================================
TestNG annotation Suite
Total tests run: 8, Failures: 0, Skips: 0
===============================================

solaris group的例子没有被包含在内,故不运行

 

四、groups的其他概念

1) run标签下面, 还有exclude标签;

2) groups标签不仅可以放到test标签的下面,还可以放到suite标签的下面。如果放到suite标签的下面,那么作用域就变大为,适用于所有的test。

具体可以参照:

https://testng.org/doc/documentation-main.html#test-groups

posted on 2021-09-17 15:30  维也纳下午茶  阅读(227)  评论(0编辑  收藏  举报