当前位置:网站首页>Hard goods | write all the codes as soon as you change the test steps? Why not try yaml to realize data-driven?
Hard goods | write all the codes as soon as you change the test steps? Why not try yaml to realize data-driven?
2022-07-03 10:57:00 【Software testing Intelligence Agency】
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 .
The principle is the same as that in the previous chapter “UI Automated testing framework ” Medium “ Data driven test data ” Be the same in essentials while differing in minor points . 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 .
Last : It can be in the official account : Sad spicy bar ! Get one by yourself 216 Page software testing engineer interview guide document information 【 Free of charge 】. And the corresponding video learning tutorial is free to share !, It includes basic knowledge 、Linux necessary 、Shell、 The principles of the Internet 、Mysql database 、 Special topic of bag capturing tools 、 Interface testing tool 、 Test advanced -Python Programming 、Web automated testing 、APP automated testing 、 Interface automation testing 、 Testing advanced continuous integration 、 Test architecture development test framework 、 Performance testing 、 Safety test, etc. .
Learn not to fight alone , It's best to keep warm , Achieve each other and grow together , The effect of mass effect is very powerful , Let's learn together , Punch in together , Will be more motivated to learn , And you can stick to it . You can join our testing technology exchange group :914172719( There are various software testing resources and technical discussions )
Friends who like software testing , If my blog helps you 、 If you like my blog content , please “ give the thumbs-up ” “ Comment on ” “ Collection ” One Key triple connection !
边栏推荐
猜你喜欢
How to make a blood bar in the game
公司测试部门来了个00后卷王之王,老油条感叹真干不过,但是...
《通信软件开发与应用》
Redis 笔记 01:入门篇
“测试人”,有哪些厉害之处?
Set ArrayList nested map set loop traversal
What is the salary level of 17k? Let's take a look at the whole interview process of post-95 Test Engineers
Uni app learning 1 bottom menu and parent-child components
Traversal of map set
Day 7 small exercise
随机推荐
缓存路由组件
Unity group engineering practice project "the strongest takeaway" planning case & error correction document
Mysql--索引原理+如何使用
Cache routing component
多路IO转接——前导
C language project: student achievement system
Differences among norm, normalize and normalized in eigen
Qt:qss custom qspinbox instance
DAY 7 小练习
ByteDance layoffs, test engineers were almost destroyed: how terrible is the routine behind the recruitment of large factories?
公司测试部门来了个00后卷王之王,老油条感叹真干不过,但是...
年中了,准备了少量的自动化面试题,欢迎来自测
How to realize automatic testing in embedded software testing?
First line of code kotlin notes
Flink -- 内置函数(ALL)
现在零基础转行软件测试还OK吗?
Wechat applet training 2
How does MySQL find the latest data row that meets the conditions?
If you always feel that you need to persist in learning English
Strategic management of project organization