Testing.xml 文檔結(jié)構(gòu):
<test name="xxxx" preserve-order="false">
<!-- 參數(shù)定義的方法 -->
<parameter name="first-name" value="Cedric"/>
<!-- groups的用法,前提是需要存在classes的組,否則所有方法不被運(yùn)行 -->
<groups>
<!-- 定義組中組的方法 -->
<define name="groups_name">
<include name="group1"/>
<include name="group2"/>
</define>
<run>
<!-- 此處用組名來區(qū)別 -->
<inclue name="groups_name" />
<exclue name="groups_name" />
</run>
</groups>
<!-- classes的用法,classes中包含類名,類名底下可以包含方法名或排除方法名 -->
<classes>
<class name="class1">
<methods>
<!-- 此處用方法名來區(qū)別 -->
<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");
}
}
可以將測試用例分組,執(zhí)行測試時(shí),使用組來管理
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中設(shè)置了 只運(yùn)行l(wèi)ogout組
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 -->
當(dāng)preserve-order="true"是,可以保證節(jié)點(diǎn)下面的方法是按順序執(zhí)行的
運(yùn)行結(jié)果為:
my name id login
my name id logout1
===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================