创建测试用例testlogin.yml
#登录测试用例 --- #测试用例名称 "case_name": "登录成功测试用例" #url地址 "url": "/authorizations/" #data "data": username: "python" password: "12345678" #期望结果 "expect": code: 200 body: '"username": "python", "user_id": 1' --- #测试用例名称 "case_name": "登录失败测试用例" #url地址 "url": "/authorizations/" #data "data": username: "pyth" password: "12345678" #期望结果 "expect": code: 400 body: "non_field_errors"
Conf.py定义路径
#定义data目录的路径 _data_path = base_DIR+os.sep+"data" def get_data_path(): return _data_path
编写测试用例
# -*- coding: utf-8 -*- # @Time : 2021/12/11 9:21 # @Author : jeffky # @File : test_login.py import os from utils.RequestsUtil import Request import pytest from config import Conf from config.Conf import ConfigYaml from utils.YamlUtil import YamlReader from utils.AssertUtil import AssertUtil #1、获取测试用例内容list # 获取testlogin.yml文件路径 test_file = os.path.join(Conf.get_data_path(),'testlogin.yml') # print(test_file) # 使用工具类来读取多个文档内容 data_list = YamlReader(test_file).data_all() # print(data_list) conf_y = ConfigYaml() url_path = conf_y .get_conf_url() # 2、参数化执行测试用例 @pytest.mark.parametrize("login",data_list) def test_yaml(login): # 初始化数据url、data url = url_path+login['url'] data = login['data'] #post请求 request = Request() r = request.post(url, json=data) #打印结果 print(r) # 断言 code = r['code'] AssertUtil().assert_code(code, login["expect"]['code']) body = r["body"] expected_body = login["expect"]['body'] AssertUtil().assert_in_body(body, expected_body) if __name__ == '__main__': pytest.main(["-s","test_login.py"])
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)