testng 控制case运行顺序
Testing.xml 文档结构:
<test name="xxxx" preserve-order="false">
<!-- 参数定义的方法 -->
<parameter name="first-name" value="Cedric"/>
<!-- groups的用法,前提是需要存在classes的组,否则所有方法不被运行 -->
<groups>
<!-- 定义组中组的方法 -->
<define name="groups_name">
<include name="group1"/>
<include name="group2"/>
</define>
<run>
<!-- 此处用组名来区别 -->
<inclue name="groups_name" />
<exclue name="groups_name" />
</run>
</groups>
<!-- classes的用法,classes中包含类名,类名底下可以包含方法名或排除方法名 -->
<classes>
<class name="class1">
<methods>
<!-- 此处用方法名来区别 -->
<inclue name="method_name" />
<exclue name="method_name" />
</methods>
</class>
</classes>
</test>
1.例如以下代码
package yycuser13.test.testcase;
import org.testng.annotations.*;
public class Test1{
@Test(groups={"login"})
public void login(){
System.out.println("my name id login");
}
@Test(groups={"login"})
public void login1(){
System.out.println("my name id login1");
}
@Test(groups={"logout"})
public void logout(){
System.out.println("my name id logout");
}
@Test(groups={"logout"})
public void logout1(){
System.out.println("my name id logout1");
}
}
可以将测试用例分组,执行测试时,使用组来管理
Testng.xml 使用下列写法
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="testloginandlogout ">
<groups>
<run>
<include name="logout"/>
<exclue name="login"/>
</run>
</groups>
<classes>
<class name="yycuser13.test.testcase.Test1">
</class>
</classes>
</test>
<!-- Test -->
</suite> <!-- Suite -->
以上的xml中设置了 只运行logout组
2.例如下代码、
package yycuser13.test.testcase;
import org.testng.annotations.Test;
public class Test1{
@Test
public void login(){
System.out.println("my name id login");
}
@Test
public void login1(){
System.out.println("my name id login1");
}
@Test
public void logout(){
System.out.println("my name id logout");
}
@Test
public void logout1(){
System.out.println("my name id logout1");
}
}
Testing.xml 文件配置如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none" >
<test name="Test">
<classes>
<class name="yycuser13.test.testcase.Test1" preserve-order="true">
<methods>
<include name="login"/>
<exclude name="login1"/>
<exclude name="logout"/>
<include name="logout1"/>
</methods>
</class>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
当preserve-order="true"是,可以保证节点下面的方法是按顺序执行的
运行结果为:
my name id login
my name id logout1
===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
3.例如下代码
package yycuser13.test.testcase;
import org.testng.annotations.Test;
import org.testng.annotations.Test;
public class Test1{
@Test(priority=3)
public void login(){
System.out.println("my name id login");
}
@Test(priority=2)
public void login1(){
System.out.println("my name id login1");
}
@Test(priority=1)
public void logout(){
System.out.println("my name id logout");
}
@Test(priority=4)
public void logout1(){
System.out.println("my name id logout1");
}
}
运行结果为:
my name id logout
my name id login1
my name id login
my name id logout1
===============================================
Suite
Total tests run: 4, Failures: 0, Skips: 0
4.例如下代码 加入了 dependsOnGroups
package yycuser13.test.testcase;
import org.testng.annotations.Test;
public class Test1{
@Test(groups={"login"},dependsOnGroups={"logout"})
public void login(){
System.out.println("my name id login");
}
@Test(groups={"login"},dependsOnGroups={"logout"})
public void login1(){
System.out.println("my name id login1");
}
@Test(groups={"logout"})
public void logout(){
System.out.println("my name id logout");
}
@Test(groups={"logout"})
public void logout1(){
System.out.println("my name id logout1");
}
}
运行结果为:
my name id logout
my name id logout1
my name id login
my name id login1
PASSED: logout
PASSED: logout1
PASSED: login
PASSED: login1
===============================================
Default test
Tests run: 4, Failures: 0, Skips: 0
5.例如下代码 使用dependsOnMethods
package yycuser13.test.testcase;
import org.testng.annotations.Test;
public class Test1{
@Test(dependsOnMethods={"login1"})
public void login(){
System.out.println("my name id login");
}
@Test(dependsOnMethods={"logout"})
public void login1(){
System.out.println("my name id login1");
}
@Test
public void logout(){
System.out.println("my name id logout");
}
@Test(dependsOnMethods={"login"})
public void logout1(){
System.out.println("my name id logout1");
}
}
运行结果为:
my name id logout
my name id login1
my name id login
my name id logout1
PASSED: logout
PASSED: login1
PASSED: login
PASSED: logout1
===============================================
Default test
Tests run: 4, Failures: 0, Skips: 0
===============================================