接口自动化(TestNG)

数据驱动概念: 用户输入输出数据来判断测试用例是否通过从而验证需求的测试.

一、接口自动化框架搭建(TestNG数据驱动) ---parameter

  关键代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestNG" verbose="1">
    <parameter name="loginPhone" value="xxx"></parameter>
    <parameter name="isMobile" value= "false"></parameter>
    <test name="apiTest">
        <classes>
            <class name="com.demo.Body" />
        </classes>
    </test>
</suite> 
@Test
    @Parameters({"loginPhone", "isMobile"})
    public static void postLogin(String loginPhone, String isMobile) {

 

二、data provider 自定义二位对象数组

       注意事项: 1、定义DataProvider name, 如果没有定义name属性则匹配二维数组名

        2、test annotation 要指定dataProvider name和步骤1 的name值相同

        3、数组数据类型要和测试方法参数类型匹配。

官网模板:

//https://testng.org/doc/documentation-main.html
//
This method will provide data to any test method that declares that its Data Provider //is named "test1" @DataProvider(name = "test1") public Object[][] createData1() { return new Object[][] { { "Cedric", new Integer(36) }, { "Anne", new Integer(37)}, }; } //This test method declares that its data should be supplied by the Data Provider //named "test1" @Test(dataProvider = "test1") public void verifyData1(String n1, Integer n2) { System.out.println(n1 + " " + n2); }

关键代码方法一:

package ddt;

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

import io.restassured.RestAssured;
import io.restassured.response.Response;

public class DataProTest {

    @DataProvider(name = "test1")
    public Object[][] createData1() {
        return new Object[][] { 
                { "loginPhone", "xxxx" },
                { "isMobile", "false" },
                };
    }

    @Test(dataProvider = "test1")
    public static void postLogin(String loginPhone, String isMobile) {
        RestAssured.baseURI = "https://ipassport.damai.cn/newlogin/account/check.do";
        RestAssured.basePath = "";
        @SuppressWarnings("deprecation"

方法二、

    

package ddt;

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

import io.restassured.RestAssured;
import io.restassured.response.Response;

public class DataProTest {

    @DataProvider
    public Object[][] createData1() {
        return new Object[][] { 
                { "loginPhone", "xxxx" },
                { "isMobile", "false" },
                };
    }

    @Test(dataProvider = "createData1")
    public static void postLogin(String loginPhone, String isMobile) {

四、响应解析-响应状态码、响应头域,响应Cookie

关键代码:

System.out.println("响应状态码:" + String.valueOf(responseCode));
String responseHearder = response.getHeader("token");
System.out.println("响应头域:" + String.valueOf(responseHearder));
String responseCookie = response.getCookie("JSESSIONID");
System.out.println("响应Cookie值:" + String.valueOf(responseCookie));
String responseContent = response.getBody().print();

响应解析Body 

        //方法一
        JsonPath getJsonValue = response.jsonPath();
        String nameValue = getJsonValue.get("name");
        System.out.println("namevalue: " + nameValue);
        //方法二
        JsonPath getResponseValue = response.jsonPath();
        String value = getResponseValue.get("$.data.file[1].version");

五、请求封装  

  封装类:

package com.tools;

import io.restassured.RestAssured;
import io.restassured.response.Response;

public class ApiTool {

    public static String getWebCookie() {
        RestAssured.baseURI = "https://www.douyin.com";
        RestAssured.basePath = "/web/api/v2/platform/user/?id=1561475827829";
        Response response = RestAssured
                .given()
                .header("accept",
                        "application/json, text/plain, */*",
                        "user-agent",
                        "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36")
                .param("id", "1561475827829").when().log().all().get();
        String responseContent = response.getBody().print();
        System.out.println(responseContent);
        String Cookie = response.getCookie("JSESSIONID");
        return Cookie;
    }

    public static Response webGet(String url) {
        String cookie = ApiTool.getWebCookie();
        Response response = RestAssured.given().cookie("JSESSIONID", cookie)
                .when().log().all().post(url);
        return response;
    }

}

  实现类

import io.restassured.response.Response;

import org.testng.annotations.Test;

import com.tools.ApiTool;

public class GetHomePageInfo {
    @Test
    public void getWebHomepage() {
        Response response = ApiTool.webGet("www.baidu");
        response.getBody().print();

    }

}

六、断言

  Assert

七、用例管理

  从class, method ,group 三个维度,依托功能测试设计按模板、功能点,用例优先级

   用例分类运行基础是:解耦

        测试一个api的增删改查

   如果不解耦,新增失败,会导致后续的删改查用例全部失败

class维度:

method维度:

group维度

原文地址:https://www.cnblogs.com/Shanghai-vame/p/11135374.html