使用postman进行接口测试

1、利用postman发送合法登录请求

  1. 新建collection
  2. 新建request
  3. 参照接口文档,输入URL
  4. 选择该接口的method
  5. 确定请求的Content-Type:x-www-form-urlencoded
  6. 按照接口文档输入参数,KEY和VALUE前后都不能有空格
  7. 点击保存
  8. 点击send
  9. 观察响应的正文

2、在postman中如何断言

断言要写在Tests中:
1. 使用postman代码片段进行断言

// JavaScript代码,而且是被postman封装过的js代码
//登录结果的验证
//SNIPPETS Response body:is equal to a string
pm.test("正常的账号登录测试",function(){
	pm.response.to.have.body("successful");
});
//SNIPPETS Response time is less than 200ms
pm.test("Response time is less than 2000ms",function(){
	pm.expect(pm.response.responseTime).to.be.blow(2000);
});
//SNIPPETS Status Code is 200
pm.test("Status code is 200",function{
	pm.response.to.have.status(200);
});

actual = pm.response.text();
expected = "success";
tests["登录测试"] = expected == actual;

2. 自己编写js代码

actual = pm.response.text();
expected = "success";
tests["登录测试"] = expected == actual;
tests["Status code is 200"] = responseCode.code === 200;
tests["Response time is less than 2000ms"] = responseTime < 2000;

3、登录接口的测试

1. 登录接口设计测试用例思路
在这里插入图片描述
评审时需要考虑以下几点:
a. 针对返回的情况,除了successful,是否还有其它情况?
b. 该请求的URL地址还对应其它的请求方式吗?
c. 记住密码是true,不记住密码是false?
d. 必填的不填或者不给,会怎样?
e. 正文的数据类型是text,还是表单数据类型application/x-www-form-urlencoded?
f. 响应的状态码除了200ok,还有其它的吗?
用例:
1)username=admin&password=admin successful
2)username=admin123&password=admin user_invalid
3)username=admin&password=admin123 password_invalid
4)username=&password=
5)password=admin
6)username=admin
7)把POST请求改成GET请求

2. 在postman中编写用例
requests的名称最好包含测试要点,如:
1、agileone_login
2、agileone_login_username_err
3、agileone_login_password_err
4、agileone_login_no_username
5、agileone_login_no_password
6、agileone_login_username_password_empty
7、agileone_login_method_get

3. 在postman中执行用例

4、查询公告接口的测试

1. 查询公告接口设计测试用例
在这里插入图片描述
在这里插入图片描述
7个非必填的查询条件,可以考虑正交表的方式设计测试用例。
写断言的时候,要验证根据查询条件得到的结果是否是按条件查询出来的公告。
比如,若是按照scope=1进行查询,要验证返回的公告中,所有公告的scope是不是都为1.
2.编辑用例并执行
1)agileone_querynotice
Tests断言

var result = pm.response.json();
//console.log(result);
//console.log(result[1].noticeid);
actual = result[1].noticeid
expected = "346";
tests["查询公告测试_id"] = expected === actual;

actual = result[1].creator;
expected = admin;
tests["查询公告测试_creator"] = expected === actual;

pm.test("查询公告测试_id_by_snippets",function(){
	var jsonData = pm.response.json();
	pm.expect(jsonData[1].noticeid).to.eql("346");
});

2)agileone_querynotice_by_scope
按照scope=0进行查询
Tests断言

var restult = pm.response.json();
//result是一个list,获取list的长度
length = Object.keys(result).length;
//用for循环验证得到的每一条记录中的scope是否为0
for(i=0,i<length-1,i++){
	console.log("第"+ i + "条公告是:",result[i]);
	actual = result[i].scope;
	expected = "0";
	tests["第" + (i+1) + "次查询scope为0的测试"] = expected === actual;
};

3)agileone_querynotice_by_headline
按照headline=hello123456进行查询
Tests断言

var restult = pm.response.json();
//result是一个list,获取list的长度
length = Object.keys(result).length;
//用for循环验证得到的每一条记录中的scope是否为0
for(i=0,i<length-1,i++){
	console.log("第"+ i + "条公告是:",result[i]);
	actual = result[i].headline;
	expected = "hello123456";
	tests["第" + (i+1) + "次查询headline为hello123456的测试"] = expected === actual;
};

5、新增公告接口的测试

1)按照新增公告接口文档描述设计测试用例
在这里插入图片描述
a. agileone_login——生成PHP_SESSID
b. agileone_querynotice_for_lastnoticeid——获取目前最新公告的id,然后+1,存入环境变量,提供给新增公告接口当期望值使用
Tests断言

var result = pm.response.json();
expected = parseInt(result[0].noticeid)+1;
//console.log(expected);
pm.environment.set("expected",expected);

c. agileone_add_notice
Tests断言

var actual = pm.response.json();
//console.log("实际值",actual);
expected = pm.environment.get("expected");
//console.log("期望值",expected);
tests["新增公告测试"] = expected === actual;

d. query_addnotice
Pre-request-script

//此处的ip是环境变量中的数据
queryStoryUrl = pm.environment.get("ip") + "agileone/index.php/notice/query";
queryStoryRequest = {
	url:queryStoryUrl,
	method:"POST",
	body:{
		mode:"application/x-www-form-urlencoded",
		currentpage:1
	}
}
pm.sendRequest(queryStoryRequest,function(err,response) {
	if(err) {
		console.log(err);
	}
	else {
		lastNoticeId = response.json[0].noticeid;
		console.log(lastNoticeId);
		pm.environment.set("id",parseInt(lastNoticeId)+1);
	}
});

Tests断言

//新增公告的断言
var actual = pm.response.json();
//console.log("实际值",actual);
expected = pm.environment.get("id");
//console.log("期望值",expected);
tests["新增公告测试"] = expected === actual;
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值