Java自动化测试(TestNg参数化 11)

Java自动化测试(TestNg参数化 11)

基础例子

编写测试函数

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.zhongxin.day02;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class TestNGParameter {

@Test
@Parameters({"type", "sheetIndex"})
public void f(String type, int index) {
System.out.println("type = " + type + ", index = " + index);
}
}

编写执行xml

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="xxx项目">
<test name="xx接口">
<classes>
<class name="com.zhongxin.day02.TestNGParameter"></class>
</classes>
</test>
<parameter name="type" value="chrome"></parameter>
<parameter name="sheetIndex" value="1"></parameter>
</suite>

执行截图

为不同接口设置不同参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="xxx项目">

<test name="xx接口1">
<classes>
<class name="com.zhongxin.day02.TestNGParameter"></class>
<parameter name="type" value="chrome"></parameter>
<parameter name="sheetIndex" value="1"></parameter>
</classes>
</test>

<test name="xx接口2">
<classes>
<class name="com.zhongxin.day02.TestNGParameter2"></class>
<parameter name="type" value="IE"></parameter>
<parameter name="sheetIndex" value="2"></parameter>
</classes>
</test>

</suite>

执行结果

DataProvider数据驱动

定义二维数组遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.zhongxin.day02;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestNGDataProvider {

@Test(dataProvider = "d")
public void f(String username, String password) {
System.out.println("username = " + username + ", password = " + password);
}

@DataProvider(name = "d")
public Object[][] datas() {
Object[][] datas = {
{"zhangsan", "123456"},
{"lisi", "123456"},
{"wangwu", "123456"}
};
return datas;
}

}

执行结果

也可以使用new Object来定义后添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@DataProvider(name = "d2")
public Object[][] datas2() {
Object[][] datas = new Object[3][2];
datas[0][0] = "zhangsan";
datas[0][1] = "123456";

datas[1][0] = "lisi";
datas[1][1] = "123456";

datas[2][0] = "wangwu";
datas[2][1] = "123456";

return datas;

}

编写一个类

Student

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.zhongxin.day02;

public class Student {
private String username;
private String password;

public Student(String username, String password) {
this.username = username;
this.password = password;
}

public Student() {
}

@Override
public String toString() {
return "Student{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}

测试函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.zhongxin.day02;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestNGDataProvider2 {

@Test(dataProvider = "d")
public void f(Student s) {
System.out.println("s = " + s);
}

@DataProvider(name = "d")
public Object[] datas() {
Student s1 = new Student("zhangsan", "123456");
Student s2 = new Student("lisi", "123456");
Student s3 = new Student("wangwu", "123456");
Object[] datas = {s1, s2, s3};
return datas;
}


}

测试结果

接口测试基础

后续进行接口自动化,需要了解一下接口相关的基础知识

HTTP协议

  • 分为客户端请求和服务端响应,无状态的协议。
  • HTTP协议重点包含报文。

报文内容

  • URL:接口地址
  • method:接口的请求方式(接口的功能决定请求方式。例如:查询是get。增加是post,删除delete,全部更新put,部分更新:patch)
  • 头部字段:描述请求或者响应的详细信息。
  • 状态码:
    100 请求中
    200 正确
    300 缓存、重定向
    400 客户端问题
    500 服务端问题
 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
您的支持将鼓励我继续创作!