转载

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

基础例子

编写测试函数

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

<?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>
Java自动化测试(TestNg参数化 11)
执行截图

为不同接口设置不同参数

<?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>
Java自动化测试(TestNg参数化 11)
执行结果

DataProvider数据驱动

定义二维数组遍历

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;
    }

}
Java自动化测试(TestNg参数化 11)
执行结果

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

@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

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;
    }
}

测试函数

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;
    }


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

接口测试基础

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

HTTP协议

  • 分为客户端请求和服务端响应,无状态的协议。

  • HTTP协议重点包含报文。

报文内容

  • URL:接口地址

  • method:接口的请求方式(接口的功能决定请求方式。例如:查询是get。增加是post,删除delete,全部更新put,部分更新:patch)

  • 头部字段:描述请求或者响应的详细信息。

  • 状态码:

    100    请求中

    200    正确

    300    缓存、重定向

    400 客户端问题

    500    服务端问题

原文  http://mp.weixin.qq.com/s?__biz=MzU5NTI3NDcxOA==&mid=2247484736&idx=1&sn=e00be1f9823cfdceef20ac9ef630ed84
正文到此结束
Loading...