当前位置:网站首页>Once you change the test steps, write all the code. Why not try yaml to realize data-driven?
Once you change the test steps, write all the code. Why not try yaml to realize data-driven?
2022-07-06 09:01:00 【Automated test seventh uncle】
Preface
Same idea and “UI Automated testing framework ” Medium “ Data driven test steps ” identical , The data-driven of the test steps in the interface is to set the parameters of the interface ( such as method、url、param etc. ) Package to yaml Manage in file . When the test steps change , It just needs to be modified yaml The configuration in the file is OK .
Data driven is the change of data, which drives the execution of automatic test , Finally, it causes the test results to change . Simply speaking , It's the application of parameterization . Test cases with small amount of data can use code parameterization to realize data-driven , In case of large amount of data, it is recommended to use a structured file ( for example yaml,json etc. ) To store data , Then read the data in the test case .
Still used @pytest.mark.parametrize Decorator to parameterize , Use parameterization to realize data-driven .
Through parameterization , Judge separately id by 2,3 Department of parentid by 1:
import pytest
class TestDepartment:
department = Department()
@pytest.mark.parametrize("id", [2, 3])
def test_department_list(self, id):
r = self.department.list(id)
assert self.department.jsonpath(expr="$..parentid")[0] == 1
The above code first uses @pytest.mark.parametrize Decorator , Two sets of data were transmitted , The test results show that two test cases have been executed , Instead of a test case . That is to say pytest Two corresponding test cases will be automatically generated from the two sets of test data and executed , Generate two test results .
When the amount of test data is large , Consider storing data in structured files . Read out the data in the required format in the code from the file , Pass to the test case to execute . This actual battle is based on YAML demonstrate .YAML To structure using dynamic fields , It's data centric , Than excel、csv、Json、XML More suitable for data-driven .
Store the two sets of data parameterized above in yaml In file , Create a data/department_list.yml file , The code is as follows :
-2
-3
The code above defines one yaml Format data file department_list.yml, A list is defined in the file , There are two data in the list , Finally, this data format is generated :[1,2]. Transform the parameterized data in the test case into from department_list.yml File read , The code is as follows :
class TestDepartment:
department = Department()
@pytest.mark.parametrize("id", \
yaml.safe_load(open("../data/department_list.yml")))
def test_department_list(self, id):
r = self.department.list(id)
assert self.department.jsonpath(expr="$..parentid")[0] == 1
The above code , Just use yaml.safe_load() Method , Read department_list.yml The data in the file , Pass in to the use case respectively test_department_list() Method to complete the verification of input and result .
In practice , For environment switching and configuration , For maintenance purposes , It is not usually done in hard coded form . stay “ Interface testing in multiple environments ” It has been introduced in the chapter , How to use environment switching as a configurable option . This chapter will reconstruct this part , Use the data-driven method to complete the configuration of multiple environments .
according to “ Interface testing in multiple environments ” chapter , Change the environment configuration section in this chapter to data-driven mode
The code is as follows :
# hold host It is amended as follows ip, And add host header
env={
"docker.testing-studio.com": {
"dev": "127.0.0.1",
"test": "1.1.1.2"
},
"default": "dev"
}
data["url"]=str(data["url"]).replace(
"docker.testing-studio.com",
env["docker.testing-studio.com"][env["default"]]
)
data["headers"]["Host"]="docker.testing-studio.com"
Still in yaml For example , Put all the environment configuration information into env.yml In file . If you are afraid of making mistakes , You can use it first yaml.safe_dump(env) take dict The code of the format is converted to yaml.
As shown below , Printed , Is the successful conversion yaml Format configuration information :
def test_send(self):
env={
"docker.testing-studio.com": {
"dev": "127.0.0.1",
"test": "1.1.1.2"
},
"default": "dev"
}
yaml2 = yaml.safe_dump(env)
print("")
print(yaml2)
Paste the printed content into env.yml In file : env.yml
docker.testing-studio.com:
dev: "127.0.0.1"
test: "1.1.1.2"
level: 4
default:
"dev"
Slightly modify the code in the environment preparation , hold env Variables from a typical dict Change it to , Use yaml.safe_load Read env.yml:
# hold host It is amended as follows ip, And add host header
env = yaml.safe_load(open("./env.yml"))
data["url"] = str(data["url"]).\
replace("docker.testing-studio.com",
env["docker.testing-studio.com"][env["default"]])
data["headers"]["Host"] = "docker.testing-studio.com"
In this way , You can use a data-driven approach , By modifying the env.yml File to directly modify the configuration information
summary
That's all for today's article , Favorite friends can like collections and pay attention
边栏推荐
- opencv+dlib实现给蒙娜丽莎“配”眼镜
- Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
- LeetCode:214. 最短回文串
- Revit secondary development Hof method calls transaction
- 注意力机制的一种卷积替代方式
- [text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth
- [Hacker News Weekly] data visualization artifact; Top 10 Web hacker technologies; Postman supports grpc
- TDengine 社区问题双周精选 | 第三期
- UML diagram memory skills
- Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
猜你喜欢
CUDA realizes focal_ loss
Problems encountered in connecting the database of the project and their solutions
KDD 2022论文合集(持续更新中)
LeetCode:124. Maximum path sum in binary tree
甘肃旅游产品预订增四倍:“绿马”走红,甘肃博物馆周边民宿一房难求
[embedded] cortex m4f DSP Library
UML圖記憶技巧
【嵌入式】Cortex M4F DSP库
Pytest之收集用例规则与运行指定用例
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
随机推荐
Revit secondary development Hof method calls transaction
[text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth
UML图记忆技巧
How to effectively conduct automated testing?
vb.net 随窗口改变,缩放控件大小以及保持相对位置
LeetCode:剑指 Offer 04. 二维数组中的查找
R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus
Using C language to complete a simple calculator (function pointer array and callback function)
Implement window blocking on QWidget
LeetCode:221. Largest Square
Navicat Premium 创建MySql 创建存储过程
LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串
LeetCode:剑指 Offer 03. 数组中重复的数字
[OC foundation framework] - [set array]
TP-LINK enterprise router PPTP configuration
LeetCode:41. 缺失的第一个正数
[OC]-<UI入门>--常用控件的学习
Niuke winter vacation training 6 maze 2
LeetCode:221. 最大正方形
Intel distiller Toolkit - Quantitative implementation 1