testng笔记3

1 testng最强大的功能就是分组了,先来看基本的分组功能,如:
      @Test(groups={"test-group"})
public void testMethodOne(){
System.out.println("Test method one belonging to group.");
}

@Test
public void testMethodTwo(){
System.out.println("Test method two not belonging to group.");
}

@Test(groups={"test-group"})
public void testMethodThree(){
System.out.println("Test method three belonging to group.");
}

  然后testng.xml中配置
      <test name="Group Test">
<groups>
<run>
<include name="test-group" />
</run>
</groups>
<classes>
<class name="test.groups.TestGroup" />
</classes>
</test>
会执行所有包含(groups={"test-group"})的测试方法
  
2 测试属于多个组的类
<suite name="Multi Group Suite" verbose="1">
<test name="Group Test one">
<groups>
<run>
<include name="group-one" />
</run>
</groups>
<classes>
<class name="test.groups.MultiGroup" />
</classes>
</test>
<test name="Group Test two">
<groups>
<run>
<include name="group-two" />
</run>
</groups>
<classes>
<class name="test.groups.MultiGroup" />
</classes>
</test>
</suite>
      @Test(groups={"group-one"})
public void testMethodOne(){
System.out.println("Test method one belonging to group.");
}

@Test(groups={"group-one","group-two"})
public void testMethodTwo(){
System.out.println("Test method two belonging to both group.");
}

@Test(groups={"group-two"})
public void testMethodThree(){
System.out.println("Test method three belonging to group.");
}

则输出:
   Test method one belonging to group.
Test method two belonging to both group.
Test method three belonging to group.
Test method two belonging to both group.

3  包含或排除groups
     <suite name="Exlude Group Suite" verbose="1">
<test name="Exclude Group Test">
<groups>
<run>
<include name="include-group" />
<exclude name="exclude-group" />
</run>
</groups>
<classes>
<class name="test.groups.ExcludeGroup" />
</classes>
</test>
</suite>
     @Test(groups={"include-group"})
public void testMethodOne(){
System.out.println("Test method one belonging to group.");
}

@Test(groups={"include-group"})
public void testMethodTwo(){
System.out.println("Test method two belonging to a group.");
}

@Test(groups={"include-group","exclude-group"})
public void testMethodThree(){
System.out.println("Test method three belonging to two groups.");
}
则可以看到,<exclude name="exclude-group" />,所以只输出:
  Test method one belonging to group.
Test method two belonging to a group.


4 也可以使用正则,如:
     <suite name="Regular Exp. Group Suite" verbose="1">
<test name="Regular Exp. Test">
<groups>
<run>
<include name="include.*" />
<exclude name=".*exclude" />
</run>
</groups>
<classes>
<class name="test.groups.RegularExpressionGroup" />
</classes>
</test>
</suite>
   @Test(groups={"include-test-one"})
public void testMethodOne(){
System.out.println("Test method one");
}

@Test(groups={"include-test-two"})
public void testMethodTwo(){
System.out.println("Test method two");
}

@Test(groups={"test-one-exclude"})
public void testMethodThree(){
System.out.println("Test method three");
}

@Test(groups={"test-two-exclude"})
public void testMethodFour(){
System.out.println("Test method Four");
}

输出:Test method one
Test method two


5  默认的组,在类的层次上指定组,比如:
    <suite name="Default Group Suite" verbose="1">
<test name="Default Group Test one">
<groups>
<run>
<include name="default-group" />
</run>
</groups>
<classes>
<class name="test.groups.DefaultGroup" />
</classes>
</test>
<test name="Default Group Test two">
<groups>
<run>
<include name="test-group" />
</run>
</groups>
<classes>
<class name="test.groups.DefaultGroup" />
</classes>
</test>
</suite>
  
@Test(groups={"default-group"})
public class DefaultGroup {
public void testMethodOne(){
System.out.println("Test method one.");
}

public void testMethodTwo(){
System.out.println("Test method two.");
}

@Test(groups={"test-group"})
public void testMethodThree(){
System.out.println("Test method three.");
}

}
     输出:
Test method one.
Test method three.
Test method two.
Test method three.

6  多个组的嵌套
    <suite name="Group of group Suite" verbose="1">
<test name="Group of group Test">
<groups>
<define name="include-group">
<include name="test-one-include" />
<include name="test-two-include" />
</define>
<define name="exclude-group">
<include name="test-one-exclude" />
<include name="test-two-exclude" />
</define>
<run>
<include name="include-group" />
<exclude name="exclude-group" />
</run>
</groups>
<classes>
<class name="test.groups.RegularExpressionGroup" />
</classes>
</test>
</suite>
    

你可能感兴趣的:(TestNG)