本系列汇总,请查看这里:https://www.cnblogs.com/uncleyong/p/18033074
实现目标
把用例yaml文件中数据读取出来,依次把每条用例数据传给测试方法。
安装模块
安装操作yaml的模块pyyaml
pip install pyyaml
测试数据文件放data目录
case.yaml
内容:
--- - epic: 全栈测试笔记 feature: user story: register title: 用户注册成功 description: 这是用户注册成功的用例 severity: blocker request: url: /qzcsbj/user/register method: post headers: {'Content-Type':'application/json'} cookies: files: params: {"username":"#{username}","password":"#{password_correct}","realName":"#{username}","sex":"1","birthday":"1990-06-16","phone":"13500000006","utype":"1","adduser":"#{username}"} initSql: ["delete from user where username = '#{username}';"] globalVariables: assertFields: $.msg=注册成功; - epic: 全栈测试笔记 feature: user story: login title: 用户登录成功 description: 这是用户登录成功的用例 severity: blocker request: url: /qzcsbj/user/login method: post headers: {'Content-Type':'application/json'} cookies: files: params: {"username":"#{username}", "password":"#{password_correct}"} initSql: globalVariables: token=$.data.token; assertFields: $.msg=登录成功; - epic: 全栈测试笔记 feature: product story: add title: 添加商品成功 description: 这是添加商品成功的用例 severity: blocker request: url: /qzcsbj/product/add method: post headers: {'Content-Type':'application/json'} cookies: files: params: {"product":{"price":9999,"productName":"#{productname}"},"token":"${token}"} initSql: ["delete from product where product_name = '#{productname}';"] globalVariables: assertFields: $.msg=添加商品成功; - epic: 全栈测试笔记 feature: product story: findByName title: 查询商品成功 description: 这是查询商品成功的用例 severity: normal request: url: /qzcsbj/product/findByName method: get headers: {'Content-Type':'application/json'} cookies: files: params: {"name":"#{productname}"} initSql: globalVariables: assertFields: $.msg=查询商品成功; - epic: 全栈测试笔记 feature: product story: findByName title: 查询商品失败 description: 这是查询商品失败的用例 severity: normal request: url: /qzcsbj/product/findByName method: get headers: {'Content-Type':'application/json'} cookies: files: params: {"name":"#{productname}"} initSql: globalVariables: assertFields: $.msg=查询商品成功啦;
项目配置文件
conf下创建settings.py,定义测试用例数据文件路径
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author: 韧 # @wx: ren168632201 # @Blog: https://www.cnblogs.com/uncleyong/ import os import logging logger = logging.getLogger(__name__) # 获取项目路径 BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 定义测试用例数据文件路径 CASE_DATA_PATH = os.path.join(BASE_PATH,'data/case.yaml')
创建工具类
tools下创建yaml_tool.py,读取yaml文件中的数据
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author: 韧 # @wx: ren168632201 # @Blog: https://www.cnblogs.com/uncleyong/ import yaml # 读取yaml中用例/变量数据 def read_data_from_yaml(file_path): f = open(file_path, "r", encoding="utf-8") res = yaml.load(f, yaml.FullLoader) return res
修改测试类
test_cases.py
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author: 韧 # @wx: ren168632201 # @Blog: https://www.cnblogs.com/uncleyong/ import pytest import logging logger = logging.getLogger(__name__) from conf.settings import CASE_DATA_PATH from utils.yaml_tool import read_data_from_yaml class TestCase: @pytest.mark.parametrize("casedata", read_data_from_yaml(CASE_DATA_PATH)) def test_case(self, casedata): logger.info(f"---当前测试用例数据是:{casedata}") assert 1 == 1
运行结果
可以看到,测试用例数据已经读取并成功传给测试方法。
标签:username,product,读取数据,04,用例,python,成功,yaml,import From: https://www.cnblogs.com/uncleyong/p/18033026