继续学习第五章 testng基本介绍
package com.course.testng.groups;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
public class GroupsOnMethod {
@Test(groups = "local")
public void test1(){
System.out.println("本地测试");
}
@Test(groups = "Online")
public void test2(){
System.out.println("线上测试");
}
@BeforeGroups("Online")
public void beforeGroups1(){
System.out.println("beforeGroups线上测试");
}
@AfterGroups("Online")
public void afterGroups1(){
System.out.println("afterGroups线上测试");
}
@BeforeGroups("local")
public void beforeGroups2(){
System.out.println("beforeGroups本地测试");
}
@AfterGroups("local")
public void afterGroups2(){
System.out.println("beforeGroups本地测试");
}
}
package com.course.testng.groups;
import org.testng.annotations.Test;
@Test(groups = "stu2")
public class GroupsOnCalss2 {
public void stu1(){
System.out.println("class2, stu1");
}
public void stu2(){
System.out.println("class2, stu2");
}
}
//组为stu1
//组为stu2
//组为teh
什么时候会用到异常测试?在我们期望结果为某一个异常的时候,比如传入了某些不合法的参数,
程序抛出了异常,也就是说我们的期望结果就是这个异常。
@Test(expectedExceptions = { IOException.class })
public void exceptionTestOne() throws Exception {
System.out.println("这是一个成功的exception");
throw new IOException();
}
@Test(expectedExceptions = { IOException.class })
public void exceptionTestOne() throws Exception {
System.out.println("这是一个失败的exception");
//这个用例会跑失败
}
主要场景是后一个test用例执行的前提是所依赖的用例执行成功,若依赖用例失败,该用例被忽略执行。
成功的案例:
@Test(expectedExceptions = { IOException.class })
public void testOne() throws Exception {
System.out.println("被依赖的用例");
throw new IOException();
}
@Test(dependsOnMethods = {"testOne"})
public void exceptionTestTwo() throws Exception {
System.out.println("依赖用例");
}
失败的案例:
@Test(expectedExceptions = { IOException.class })
public void testOne() throws Exception {
System.out.println("被依赖的用例");
}
@Test(dependsOnMethods = {"testOne"})
public void exceptionTestTwo() throws Exception {
System.out.println("依赖用例");
}
@Test
@Parameters({"name", "age"})
public void testPara(String name, String age){
System.out.println("name = "+name +", age = "+age);
}
xml文件如下:
运行该xml文件如下:
方法一:
@Test(dataProvider ="test")
public void testPara1(String name, int age){
System.out.println("name = "+name +", age = "+age);
}
@DataProvider(name = "test")
public Object[][] dataProvider(){
Object [][] o = new Object [][]{
{
"张三", 10
},
{
"lisi", 20
},
{
"wangwu",30
}
};
return o;
}
执行后:
方法二:
//通过方法名不同传递不同参数
@Test(dataProvider ="methodData")
public void testPara2(String name, int age){
System.out.println("方法一;name = "+name +", age = "+age);
}
@Test(dataProvider ="methodData")
public void testPara3(String name, int age){
System.out.println("方法二;name = "+name +", age = "+age);
}
@DataProvider(name = "methodData")
public Object[][] methodDataTest(Method method){
Object[][] result = null;
if (method.getName().equals("testPara2")){
result = new Object[][]{
{"zhangsan", 20},
{"lisi", 24}
};
} else if (method.getName().equals("testPara3")){
result = new Object[][]{
{"wangwu", 50},
{"zhaoli", 70}
};
}
return result;
}
执行结果: