最近写了三个接口,很不想写了,觉得好麻烦呀,用postman比这个简洁多了,为什么要写代码呀,为都要学习代码呀呀,我现在还没觉得手写代码比用工具工作上优势体现在哪里了~~知道的告诉我下吧 因为担心我以后很长一段时间不会写代码,所以这三个仅有的接口代码也贴上来做个笔记吧 - <?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原生报告,上个帖子已经说过了,请自行查看 然后下载几个封装好的jar包----稍后我会附上jar包 然后开始在test里写代码了,在test里写是因为使用testng可以单个执行任意一个方法,不需要main入口 其中有一个@beforemethod是获取token用的,在任意一个方法执行前都会先去执行获取token这个接口 - 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自动化也不太合适这时候动工;不带着目的性的学习果然没有效率哇
|