一.将要测试的接口域名写入到文件中,如下
macname@MacdeMacBook-Pro Interface-automation-test % cat conf/hostname.yaml data: {default_baseurl: 'https://baike.baidu.com', is_passed: true, new_baseurl: 'https://baike.baidu.com'} macname@MacdeMacBook-Pro Interface-automation-test %
default_baseurl以及new_baseurl 替换为待测项目的接口域名
二.接下来编写接口用例,所有接口用例都在case文件夹中,里面的每个文件都是一个功能模块,每个文件中有该功能相关的多个接口,
之所以写在一个文件中是因为接口间数据传递比较简单,比如创建公司后返回公司的id,用dic字典传递给后面的创建订单接口使用,比如下面是一个功能的接口用例
# coding=utf-8 import json import logging import os import sys sys.path.append("/".join(os.path.dirname(os.path.abspath(__file__)).split("/")[:-1])+'/lib') import requests sys.path.append("../") from util.getinfolib import GetInfo
#日志使用 log = logging.getLogger(__name__) class TestUM: def setup_class(self): self.url = GetInfo().go() self.dic = {} self.headers = { 'Accept': 'application/json, text/plain, */*' , 'client': 'ios' , 'Content-Type': 'application/json;charset=UTF-8' , 'Origin': self.url , 'Referer': self.url ,'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36' } # 1 login def test_login(self): service_url = self.url + "/item/%E5%8E%A8%E8%89%BA/2375541?fr=aladdin" payload = {} r = requests.get(service_url, data=json.dumps(payload), headers=self.headers) assert r.status_code == 200 #参考写法 ''' # 1 login def test_login(self): service_url = self.url + "/login/app" payload = { "username": "shouji(勿删)", "password": "123" } r = requests.post(service_url, data=json.dumps(payload), headers=self.headers) assert r.status_code == 200 assert r.json()["code"] == 200 self.headers["token"] = r.json()['data']['token'] # 收油人员接受联单 def test_collections(self): service_url = self.url + "/collections" payload = { "page": 1, "size": 10 } r = requests.get(service_url, data=json.dumps(payload), headers=self.headers) self.dic["collectionId"] = r.json()["data"]["content"][0]["id"] assert r.status_code == 200 assert r.json()["code"] == 200 # 收油人员收油记录详情 def test_collectionsDetail(self): service_url = self.url + "/users/collections/" + str(self.dic["collectionId"]) payload = { } r = requests.get(service_url, data=json.dumps(payload), headers=self.headers) assert r.status_code == 200 assert r.json()["code"] == 200 # 收油人员入仓记录 def test_storage(self): service_url = self.url + "/storage" payload = { "page": 1, "size": 10 } r = requests.get(service_url, data=json.dumps(payload), headers=self.headers) self.dic["storageId"] = r.json()["data"]["content"][0]["id"] assert r.status_code == 200 assert r.json()["code"] == 200 '''
本地查看接口执行结果
github地址
https://github.com/fusu192/Interface-automation-test