当前位置:网站首页>In automated testing, test data is separated from scripts and parameterized methods
In automated testing, test data is separated from scripts and parameterized methods
2022-08-10 02:52:00 【Test the imp】
The test data and test code are in the actual test work,Often stored separately,Conducive to separate maintenance of test data and test scripts.比如,Add several new sets of test data for the test case,Only the test data file needs to be modified,The test code does not need to be moved;If it is a test case, a new checkpoint is added,Just need to modify the test script,No modification of test data files is required.
—、准备测试数据
现在在data/Create a directory to store test dataYaml文件test_in_theaters.yaml,内容如下:
---
tests:
- case: 验证响应中start和countConsistent with the parameters in the request
input:
method: GET
path: /v2/movie/in_theaters
headers:
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36
params:
apikey: 0df993c66c0c636e29ecbb5344252a4a
start: 0
count: 10
expected:
response:
title: 正在上映的电影-上海
count: 10
start: 0
- case: 验证响应中title"正在上映的电影-北京"
input:
method: GET
path: /v2/movie/in_theaters
headers:
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36
params:
apikey: 0df993c66c0c636e29ecbb5344252a4a
start: 1
count: 5
expected:
response:
title: 正在上映的电影-北京
count: 5
start: 1
熟悉Yaml格式的同学,It should be easy to understand the contents of the test data file above.in this test data file,有一个数组tests,It contains two complete test data.A complete test data consists of three parts:
case,表示测试用例名称.
input,表示输入参数.
expected,表示预期结果.
上面的input输入参数,是一个http请求对象,Contains all parameters of the interface under test,包括请求方法、请求路径、请求头、请求参数.expected表示预期结果,in the test data above,Only the expected values for the request response are listed,实际测试中,You can also list expected values for the database.
二、编写测试脚本
新建一个测试脚本test_paramtrize_by_data_driven.py,内容如下:
import pytest
class TestDataDriven:
@pytest.mark.datafile('data/test_in_theaters.yaml') # A relative path relative to the project root directory
def test_data_driven(self, parameters):
print(parameters['input'])
print(parameters['expected'])
First look at the test function,Above the test function a method called datafile的markerto provide data for the test function.The path to the test data is relative to the project root path,This means that the test data is in the root path of the projectdata目录中的test_in_theaters.yaml.The test function passesparameters这个fixtureGet the test data content.
三、实现pytest_generate_tests Hook
The above test script uses the test data method,是在tests/conftest.py文件中的pytest_generate_tests函数中实现的.
import yaml
import json
def pytest_generate_tests(metafunc):
ids = []
markers = metafunc.definition.own_markers
for marker in markers:
if marker.name == 'datafile': # Read foreign data
test_data_path = os.path.join(metafunc.config.rootdir, marker.args[0]) # Splicing test data paths
with open(test_data_path) as f:
ext = os.path.splitext(test_data_path)[-1]
if ext in ['.yaml', '.yml']:
test_data = yaml.safe_load(f)
elif ext == '.json':
test_data = json.load(f)
else:
raise TypeError('datafile must be yaml or json,root must be tests')
if "parameters" in metafunc.fixturenames: # Parameterize with external data
for data in test_data['tests']: # 用test_data中的caseas the test case name
ids.append(data['case'])
# 用test_datathis listparameters进行参数化.
metafunc.parametrize("parameters", test_data['tests'], ids=ids, scope=
metafunc.definition.own_markersCan read all the test functionsmarker,如果存在datafile这个marker,Indicates that test data needs to be read externally,Test data can beYAML格式也可以是Json格式.If the test function'smetafunc.fixturenames中含有parameters这个fixture函数,Just parameterize it with external test data.
四、Parametric routines
有了pytest_generate_tests这个Hook函数后,Parameterizing the test function requires three changes:
在data目录下新加一个YAML或者Json格式文件.Refer to the example above for the file content format.
Specifies the path to the test data file
@pytest.mark.datafile('data/test_in_theaters.yaml')
Provide a name for the test data
parameters
的fixture
现在,The directory structure of the entire project should be as follows:
$ tree
.
├── Pipfile
├── Pipfile.lock
├── data
│ └── test_in_theaters.yaml
├── tests
│ └── conftest.py
│ └── test_data_driven.py
五、总结
This paper implements the core of the separation of test scripts and test data,还是实现了Hook函数,在Hookfunction to read the test data file,and for the test functionfixture进行参数化.
Just done with a small examplepostmanAn introduction to parameterization,Of course, you need to do it yourself to deepen your memory
最后在我的QQ技术交流群里整理了我这10几年软件测试生涯整理的一些技术资料,包括:电子书,简历模块,各种工作模板,面试宝典,自学项目等.如果在学习或工作中遇到问题,群里也会有大神帮忙解答,群号 798478386 ( 备注 今日头条555 )
全套软件测试自动化测试教学视频
300G教程资料下载【视频教程+PPT+项目源码】
全套软件测试自动化测试大厂面经
边栏推荐
猜你喜欢
组件的使用
unity 报错 Unsafe code may only appear if compiling with /unsafe. Enable “Allow ‘unsafe‘ code“ in Pla
Janus actual production case
Chip Information|Semiconductor revenue growth expected to slow to 7%, Bluetooth chip demand still growing steadily
阿里云OSS文件上传
Fusion Compute网络虚拟化
OpenCV图像处理学习三,Mat对象构造函数与常用方法
[论文阅读] Multimodal Unsupervised Image-to-Image Translation
3dmax如何制作模型走路动画
OptiFDTD应用:纳米盘型谐振腔等离子体波导滤波器
随机推荐
hint: Updates were rejected because the tip of your current branch is behind hint: its remote counte
Unity碰撞和触发
阿里云OSS文件上传
进程管理和任务管理
Not, even the volume of the king to write code in the company are copying and pasting it reasonable?
[网鼎杯 2020 青龙组]AreUSerialz
小程序开发的报价为什么有差别?需要多少钱?
FusionCompute产品介绍
微透镜阵列后光传播的研究
桌面云组件介绍与安装
DP 优化方法合集
[QNX Hypervisor 2.2用户手册]10.14 smmu
【论文笔记】基于深度学习的机器人抓取虚拟仿真实验教学系统
HCIP——综合交换实验
C# 正则表达式分组查询
数组(一)
openpose脚部标注问题梳理
2022杭电多校联赛第七场 题解
grafana9配置邮箱告警
ImportError: Unable to import required dependencies: numpy