当前位置:网站首页>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
-3The 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

边栏推荐
- Implement window blocking on QWidget
- Booking of tourism products in Gansu quadrupled: "green horse" became popular, and one room of B & B around Gansu museum was hard to find
- 甘肃旅游产品预订增四倍:“绿马”走红,甘肃博物馆周边民宿一房难求
- Mise en œuvre de la quantification post - formation du bminf
- 【嵌入式】使用JLINK RTT打印log
- Selenium+Pytest自动化测试框架实战
- [OC]-<UI入门>--常用控件-提示对话框 And 等待提示器(圈)
- LeetCode:214. 最短回文串
- Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
猜你喜欢
![[text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth](/img/10/c0545cb34621ad4c6fdb5d26b495ee.jpg)
[text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth

BMINF的後訓練量化實現

【嵌入式】使用JLINK RTT打印log

项目连接数据库遇到的问题及解决

Advance Computer Network Review(1)——FatTree
![[embedded] print log using JLINK RTT](/img/22/c37f6e0f3fb76bab48a9a5a3bb3fe5.png)
[embedded] print log using JLINK RTT

BN folding and its quantification
![[embedded] cortex m4f DSP Library](/img/83/ab421d5cc18e907056ec2bdaeb7d5c.png)
[embedded] cortex m4f DSP Library

Unsupported operation exception

LeetCode:221. Largest Square
随机推荐
Advanced Computer Network Review(4)——Congestion Control of MPTCP
BN folding and its quantification
LeetCode:劍指 Offer 42. 連續子數組的最大和
LeetCode:236. 二叉树的最近公共祖先
LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串
LeetCode:214. Shortest palindrome string
UML圖記憶技巧
LeetCode:221. 最大正方形
LeetCode:673. 最长递增子序列的个数
ant-design的走马灯(Carousel)组件在TS(typescript)环境中调用prev以及next方法
TP-LINK 企业路由器 PPTP 配置
Detailed explanation of dynamic planning
可变长参数
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
LeetCode:41. Missing first positive number
LeetCode:剑指 Offer 04. 二维数组中的查找
Intel Distiller工具包-量化实现1
Leetcode: Sword Finger offer 42. Somme maximale des sous - tableaux consécutifs
I-BERT
LeetCode:498. 对角线遍历