java+testng接口測試

最近寫了三個接口,很不想寫了,以爲好麻煩呀,用postman比這個簡潔多了,爲何要寫代碼呀,爲都要學習代碼呀呀,我如今還沒以爲手寫代碼比用工具工做上優點體如今哪裏了~~知道的告訴我下吧java

由於擔憂我之後很長一段時間不會寫代碼,因此這三個僅有的接口代碼也貼上來作個筆記吧ios

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>interfaceAPI</groupId>
    <artifactId>interfaceAPI</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.29</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.5</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <!--<testFailureIgnore>true</testFailureIgnore>-->
                    <forkMode>never</forkMode>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                    <suiteXmlFiles>
                        <suiteXmlFile>xml/testNG.xml</suiteXmlFile>  //testng報告
                        <suite name="interfaceAPI" parallel="false">
                            <test name="testGetHomePage_success">
                                <classes>
                                    <class name="com.xzt.testng.Test_api"></class>
                                </classes>
                            </test>
                        </suite>

                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

導入testng後,怎麼使用testng原生報告,上個帖子已經說過了,請自行查看apache

而後下載幾個封裝好的jar包----稍後我會附上jar包json

而後開始在test裏寫代碼了,在test裏寫是由於使用testng能夠單個執行任意一個方法,不須要main入口api

其中有一個@beforemethod是獲取token用的,在任意一個方法執行前都會先去執行獲取token這個接口maven

package com.xzt.testng;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.xzt.testng.util.EncryptUtils;
import com.xzt.testng.util.HttpUtil;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Test_api {

    private String baseUrl = "http:/要測試的host/";

    private static String TOKEN;

    private static String SUCCESS = "0000";

    private static String terminalType ="ios";
    private static int responseCode = 201;
    private static String name;

    @BeforeMethod
    public void getToken(){
        String tokenUrl = baseUrl + "接口請求url";
        String responseStr = HttpUtil.get(tokenUrl, null);
        JSONObject object = JSON.parseObject(responseStr);
        TOKEN = object.getJSONObject("content").getString("token");
        System.out.println(TOKEN);

    }
    @Test
    public void testGetHomePage_success(){
        Map<String,Object> params = new HashMap<>();
        params.put("nowTime",new Date().getTime());
        params.put("terminalType", terminalType);
        params.put("sex", 0);
        params.put("uId", "ad634938-14ce-4f8f-b15f-9af87d1ad73a");
        params.put("userId", "ad634938-14ce-4f8f-b15f-9af87d1ad73a");

        String url = baseUrl + "organizationMsz-service/manage/v1/getHomePageInfo.do";
        String responseStr = HttpUtil.postJson(url, params);
        System.out.println(responseStr);
        JSONObject object = JSON.parseObject(responseStr);
        Assert.assertEquals(object.getString("returnNo"), SUCCESS);//校驗接口返回碼,咱們的程序正常代碼是0000
    }

    @Test
    public void testGetUserDetailInfo_success() {
        Map<String, Object> params = new HashMap<>();
        params.put("nowTime", new Date().getTime());
        params.put("sex", 0);
        params.put("token", TOKEN);
        params.put("uId", "ad634938-14ce-4f8f-b15f-9af87d1ad73a");
        params.put("userId", "ad634938-14ce-4f8f-b15f-9af87d1ad73a");

        String url = baseUrl + "接口請求url";
        String responseStr = HttpUtil.postJson(url, params);
        System.out.println(responseStr);//打印出接口返回參數
        JSONObject object = JSON.parseObject(responseStr);//轉換成json格式好去執行斷言

        Assert.assertEquals(object.getString("returnNo"), SUCCESS);
    }


    @Test
    public void testGetUserDetailInfo_userNotLogin() {
        Map<String, Object> params = new HashMap<>();
        params.put("nowTime", new Date().getTime());
        params.put("sex", 0);
        params.put("token", TOKEN);
        params.put("uId", "ad634938-14ce-4f8f-b15f-9af87d1a");
        params.put("userId", "ad634938-14ce-4f8f-b15f-9af87d");

        String url = baseUrl + "請求url";
        String responseStr = HttpUtil.postJson(url, params);
        System.out.println(responseStr);
        JSONObject object = JSON.parseObject(responseStr);

        Assert.assertEquals(object.getString("returnNo"), "0031");//這個是期待的錯誤返回碼
    }

    @Test
     public void xztlogin() throws Exception{
        String loginUrl = baseUrl + "url";
        Map<String, Object> loginParams = new HashMap<>();
        loginParams.put("mobile", "133~~~~");
        loginParams.put("password", "123456");
        String content = JSON.toJSONString(loginParams);//這個有一點點複雜,咱們登陸的請求接口是加密過的,因此入參你只會看到一個content:"裏面是一堆加密過的用戶手機號+密碼的信息",

        Map<String, Object> params = new HashMap<>();
        params.put("content",EncryptUtils.bytesToHexString(EncryptUtils.encrypt(content.getBytes())));//因此這裏照咱們程序的加密格式,把content裏的信息轉換下再發send出去
        String response = HttpUtil.postJson(loginUrl, params);
        System.out.println(response);
        JSONObject loginResponse = JSON.parseObject(response);
        Assert.assertEquals(loginResponse.getString("returnNo"), SUCCESS);

    }


}

我最近有點沒有學習的動力了,項目停滯,無所事事;寫了三個接口,比我當初用postman作整個APP接口自動化的時間都要長,一時沒有get到代碼測試的優點,大概也是由於目前沒有代碼發揮的餘地(心裏一直對會寫代碼的人保持敬畏之心的);後臺目前也處於重構時期,WEBUI自動化也不太合適這時候動工;不帶着目的性的學習果真沒有效率哇工具

相關文章
相關標籤/搜索