postman requests to publish topic assertions
var jsonData = pm.response.json();
pm.test("success value is true", function () {
pm.expect(jsonData["success"]).to.eql(true);
});
pm.test("topic id value is not null",function(){
var topic_id = jsonData['topic_id'];
// 更多断言方式操作,查看http://www.chaijs.com/api/
bdd/
pm.expect(topic_id).to.not.eql(null);
})
How to automate interface association?
1. Interface association: 2 or more than 3 directly transfer values through a certain data as a hub.
Solutions:
1. The first interface gets the response result from the server, extracts the value from the response result, and sets it as a variable.
2. The following interface can directly call the variable value.
Pre-request-script
Perform operations before sending the request, for example, you can set some variables here
// 发送请求执行执行的脚本
pm.environment.set("title", "1111111111111111111111111111");
pm.environment.set("tab", "ask");
pm.environment.set("content", "22222222222222222222222222");
Variables can be called in the script, or in the form of {{variable name}}
In the [Tests] script of other requests, variables can be obtained and cannot be called by { {variable name}}. To use the way to get variables to call.
var jsonData = pm.response.json();
var data = jsonData['data'];
// 获取发布话题中设置环境变量 title
var title = pm.environment.get("title");
pm.test("title 的值应该为"+title, function () {
pm.expect(data["title"]).to.eql(title);
});
// 获取发布话题中设置环境变量content
var content = pm.environment.get("content");
pm.test("content 的值应该为"+content, function () {
pm.expect(data["content"]).to.eql(content);
});
// 获取发布话题中设置环境变量tab
var tab = pm.environment.get("tab");
pm.test("tab 的值应该为"+tab, function () {
pm.expect(data["tab"]).to.eql(tab);
});
Run result in Runner
data driven testing
Core: Automated testing based on data file content
1 Create a test data file data.csv
Solve the Chinese garbled problem:
Notepad opens the data.csv file, [File] - [Save As] select utf8 encoding - [Save]
2 design script
Variables are used in the script, and the variables must be saved together with the fields of the csv file.
3 Series Assertion Section
// 获取变量
var except_val = pm.variables.get("except_val");
pm.test("error_msg value should be "+except_val, function () {
var jsonData = pm.response.json();
pm.expect(jsonData['error_msg']).to.eql(except_val);
});
Be sure to save the script after editing.
4 Run in Runner
implement
python script
1 interface return value
import requests
"""
"""
base_url = "http://39.107.96.138:3000/api/v1/"
testdata = {
"accesstoken":"49b2e830-4305-475d-b6b5-52287cc
5daaa",
"title":"2313131231231232",
"tab":"ask",
"content":"xxxxxxxxxxxxx"
}
def test_new_topic():
"""
测试发布话题
:return:
"""
url = base_url+'topics'
r = requests.post(url,json=testdata)
jsonData = r.json()
assert r.status_code == 200
assert jsonData['success']
print("test_new_topic, topicid:",jsonData['topic_id'])
assert jsonData['topic_id'] is not None
return jsonData['topic_id']
def test_topic_detail():
topic_id = test_new_topic()
print("test_topic_detail topicid:",topic_id)
url = base_url+"/topic/"+topic_id
testparmas = {
'mdrender':'false'
}
r = requests.get(url,params=testparmas)
print("rjson :",r.json())
jsonData = r.json()
data = jsonData['data']
assert data['title'] == testdata['title']
assert data['tab'] == testdata['tab']
assert data['content'] == testdata['content']
def test_update_topic():
pass
2 using fixtures
Pass arguments via @pytest.fixture
@pytest.fixture
scope: scope
autouse: whether to load automatically
name : reference name
parmas: parameters
ids: execute display function name suffix
import pytest
import requests
base_url = "http://39.107.96.138:3000/api/v1/"
testdata = {
"accesstoken":"49b2e830-4305-475d-b6b5-52287c
c5daaa",
"title":"2313131231231232",
"tab":"ask",
"content":"xxxxxxxxxxxxx"
}
@pytest.fixture(scope="module",autouse=True,name="topic_id")
def newtopic():
url = base_url + 'topics'
r = requests.post(url, json=testdata)
jsonData = r.json()
return jsonData['topic_id']
def test_update_topic(topic_id):
"""
编辑话题
:return:
"""
print("test_update_topic",topic_id)
def test_collect_topic(topic_id):
"""
收藏话题
:return:
"""
print("test_collect_topic",topic_id)
def test_reply_topic(topic_id):
"""
回复话题
:return:
"""
print("test_reply_topic",topic_id)
homework 1
The three APIs of the above code are implemented in python.
1 csv data driven
Pay attention to the path of the csv file
import pytest
import os
import csv
import requests
"""
使用csv中的数据进行数据驱动测试
"""
dir=os.path.dirname(os.path.dirname( os.path.dirname(__file__)))
csvfile = os.path.join(dir,'data/data.csv')
csvdata = []
with open(csvfile,encoding='utf8') as file:
filedata= csv.reader(file)
next(filedata)
for x in filedata:
csvdata.append(x)
print(csvdata)
"""
[['0418178a-b80c-4e15-aa8f-bab03a3491cb', '1111111111', 'ask', '22222222222', '错误的accessToken'], ['49b2e830-4305-475d-b6b5-52287cc5daaa', '', 'ask', '2222222222', '标题不能为空'], ['49b2e830-4305-475d-b6b5-52287cc5daaa', '1', 'ask', '2222222222', '标题字数太多或太少'], ['49b2e830-4305-475d-b6b5-52287cc5daaa', '1111111111', '', '2222222222', '必须选择一个版块'], ['49b2e830-4305-475d-b6b5-52287cc5daaa', '1111111111', 'ask', '', '内容不可为空']]
|| 数据格式转换一下
\/
[{"token":"0418178a-b80c-4e15-aa8f-bab03a3491cb","tab":"ask"},{....}]
"""
@pytest.fixture(params=csvdata)
def data(request):
return request.param
base_url = "http://39.107.96.138:3000/api/v1/"
def test_topics(data):
url = base_url + 'topics'
testdata={
"accesstoken": data[0],
"title": data[1],
"tab": data[2],
"content": data[3]
}
r = requests.post(url, json=testdata)
jsonData = r.json()
assert jsonData['error_msg'] == data[4]
homework 2
Data format conversion
Reference data format conversion
http://docs.python.org/zh-cn/3.7/tutorial/inputoutput.html#saving-structured-data-with-json
2 Excel as data-driven
import pytest
import os
import requests
from openpyxl import load_workbook
from openpyxl.worksheet.worksheet import Worksheet
"""
使用Excel中的数据进行数据驱动测试
"""
dir=os.path.dirname(os.path.dirname( os.path.dirname(__file__)))
excelfile = os.path.join(dir,'data/data.xlsx')
workbook = load_workbook(excelfile)worksheet:
Worksheet = workbook['topic_data']
exceldata = []
for row in worksheet.iter_rows(min_row=2,max_row=6,min_col=1,max_col=5,values_only=True):
# print(row)
exceldata.append(row)
"""
[['0418178a-b80c-4e15-aa8f-bab03a3491cb', '1111111111', 'ask', '22222222222', '错误的accessToken'], ['49b2e830-4305-475d-b6b5-52287cc5daaa', '', 'ask', '2222222222', '标题不能为空'], ['49b2e830-4305-475d-b6b5-52287cc5daaa', '1', 'ask', '2222222222', '标题字数太多或太少'], ['49b2e830-4305-475d-b6b5-52287cc5daaa', '1111111111', '', '2222222222', '必须选择一个版块'], ['49b2e830-4305-475d-b6b5-52287cc5daaa', '1111111111', 'ask', '', '内容不可为空']]
|| 数据格式转换一下
\/
[{"token":"0418178a-b80c-4e15-aa8f-bab03a3491cb","tab":"ask"},{....}]
"""
@pytest.fixture(params=exceldata)
def data(request):
return request.param
base_url = "http://39.107.96.138:3000/api/v1/"
def test_topics(data):
url = base_url + 'topics'
testdata={
"accesstoken": data[0],
"title": data[1],
"tab": data[2],
"content": data[3]
}
r = requests.post(url, json=testdata)
jsonData = r.json()
assert jsonData['error_msg'] == data[4]
Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:
These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey, and I hope it can help you! Partners can click the small card below to receive