接口自动化框架(java)--2.接口用例POST请求,参数配置
这套框架的报告是自己封装的
Post类型的接口通常有请求参数,请求参数也是json类型,所以需要写一个类将请求参数序列化成json对象
以常见的登录接口为例
新建一个package,和postParameters类

package com.qa.Parameters;
public class postParameters {
private String userName;
private String password;
public postParameters(){
}
//login
public postParameters(String userName , String password){
this.userName = userName;
this.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;
}
}
2.在config.properties中配置根url,以及excel地址
#TestCase1
Host = https://xxxxxx.cn
testCase1data = F:\\gitcode\\API_AutoFramework\\src\\main\\java\\com\\qa\\data\\testCase1data.xlsx

4.在tests目录下建一个测试类继承TestBase,再引入testng,编写测试用例
package com.qa.tests; import com.alibaba.fastjson.JSON;
import com.qa.base.TestBase;
import com.qa.Parameters.postParameters;
import com.qa.restclient.RestClient;
import com.qa.util.TestUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; import java.io.IOException;
import java.util.HashMap; import static com.qa.util.TestUtil.dtt; public class testCase1 extends TestBase {
TestBase testBase;
RestClient restClient;
CloseableHttpResponse closeableHttpResponse;
//host根url
String host;
//Excel路径
String testCaseExcel;
//header
HashMap<String ,String> postHeader = new HashMap<String, String>();
@BeforeClass
public void setUp(){
testBase = new TestBase();
restClient = new RestClient();
postHeader.put("Content-Type","application/json");
//载入配置文件,接口endpoint
host = prop.getProperty("Host");
//载入配置文件,post接口参数
testCaseExcel=prop.getProperty("testCase1data"); } @DataProvider(name = "postData")
public Object[][] post() throws IOException {
return dtt(testCaseExcel,0); } @Test(dataProvider = "postData")
public void login(String loginUrl,String username, String passWord) throws Exception {
//使用构造函数将传入的用户名密码初始化成登录请求参数
postParameters loginParameters = new postParameters(username,passWord);
//将登录请求对象序列化成json对象
String userJsonString = JSON.toJSONString(loginParameters);
//发送登录请求
closeableHttpResponse = restClient.postApi(host+loginUrl,userJsonString,postHeader);
//从返回结果中获取状态码
int statusCode = TestUtil.getStatusCode(closeableHttpResponse);
Assert.assertEquals(statusCode,200); }
@BeforeClass
public void endTest(){
System.out.print("测试结束");
} }

原文地址https://blog.csdn.net/qq_34693151/article/details/81874656
接口自动化框架(java)--2.接口用例POST请求,参数配置的更多相关文章
- 接口自动化框架(java)--4.接口Token传递
这套框架的报告是自己封装的 一般token会在登录接口返回结果中呈现,从代码层面获取token的方式有很多种,我是使用jsonpath这个json路径语言去匹配token所在路径的key值 packa ...
- 接口自动化框架(java)--3.get,delete请求,Excel管理多种请求类型
这套框架的报告是自己封装的 每种请求类型放入不同的sheet中,就可以避免新建太多的excel去做数据驱动. XSSFSheet类提供了一个读取sheet的方法,getSheetAt(int),通过下 ...
- 接口自动化框架(java)--5.通过testng.xml生成extentreport测试报告
这套框架的报告是自己封装的 由于之前已经通过Extentreport插件实现了Testng的IReport接口,所以在testng.xml中使用listener标签并指向实现IReport接口的那个类 ...
- 接口自动化框架(java)--1.项目概述
项目github地址: https://github.com/tianchiTester/API_AutoFramework 这套框架的报告是自己封装的 1.测试基类TestBase: 接口请求的te ...
- Python+Pytest+Allure+Git+Jenkins接口自动化框架
Python+Pytest+Allure+Git+Jenkins接口自动化框架 一.接口基础 接口测试是对系统和组件之间的接口进行测试,主要是效验数据的交换,传递和控制管理过程,以及相互逻辑依赖关系. ...
- 转载:python + requests实现的接口自动化框架详细教程
转自https://my.oschina.net/u/3041656/blog/820023 摘要: python + requests实现的接口自动化框架详细教程 前段时间由于公司测试方向的转型,由 ...
- python + requests实现的接口自动化框架详细教程
前段时间由于公司测试方向的转型,由原来的web页面功能测试转变成接口测试,之前大多都是手工进行,利用postman和jmeter进行的接口测试,后来,组内有人讲原先web自动化的测试框架移驾成接口的自 ...
- Jmeter+ant+Jenkins接口自动化框架搭建
摘自:https://testerhome.com/topics/13389 一.背景 上一篇讲了Jmeter 接口自动化-脚本数据分离实例,我们知道怎么利用Jmeter去编写接口自动化脚本,但是接 ...
- python+request接口自动化框架
python+request接口自动化框架搭建 1.数据准备2.用python获取Excel文件中测试用例数据3.通过requests测试接口4.根据接口返回的code值和Excel对比 但本章只讲整 ...
随机推荐
- Vue中父子组件通讯——组件todolist
一.todolist功能开发 <div id="root"> <div> <input type="text" v-model=& ...
- nginx+apache动静分离/负载均衡
[主从] [Mysql-Master] log-bin=mysql-bin server-id = MariaDB [(none)]> grant replication slave on *. ...
- 网络编程-Mysql-2、各种查询
1.先创建一个学生表 create table students ( id int auto_increment not null primary key, name varchar(20) not ...
- Linux 安装 JDK 8
1. step1: 下载最新的 Java Archive # wget --no-cookies --no-check-certificate --header "Cookie: gpw_ ...
- c#一步一步实现ORM(二)
c#一步一步实现ORM(二) 上一篇描述了简单的思路,这一片我们来稍微细化一下 1插入的时候忽略某些字段 public int Insert<T>(T o, params string[] ...
- Django——Ajax
1.Ajax简介 AJAX(Asynchronous Javascript And XML)--"异步的JavaScript与XML". Ajax使用Javascript语言与服务 ...
- 手机号验证正则表达式+Demo(亲测完毕)
以下为本人亲测过的验证手机号格式的demo,需要的小伙伴拿走不谢~<!DOCTYPE html><html><head><meta charset=" ...
- Chapter 2 Basic Elements of JAVA
elaborate:详细说明 Data TypesJava categorizes data into different types, and only certain operationscan ...
- ECMA Script 6_模块加载方案 ES6 Module 模块语法_import_export
1. 模块加载方案 commonJS 背景: 历史上,JavaScript 一直没有模块(module)体系, 无法将一个大程序拆分成互相依赖的小文件,再用简单的方法拼装起来. 其他语言都有这项功能: ...
- [LeetCode] Random Pick with Blacklist 带黑名单的随机选取
Given a blacklist B containing unique integers from [0, N), write a function to return a uniform ran ...