当前位置:网站首页>Getting started with pytest ----- test case pre post, firmware
Getting started with pytest ----- test case pre post, firmware
2022-07-06 17:49:00 【Peng Yuyan in the testing industry】
1、 Back and forth
The so-called pre post , Is similar to unittest In the framework setup and teardown—— Open the browser before executing the use case ( In front of ), Close the browser after execution ( After ). for example :

The printed part of the code above is setup、setup_class、teardown、teardown_class Description of each method
But if there is another file , These operations are also required 【 such as ui To automate each use case, open the browser and execute 】 We need to encapsulate these common methods . notes : Public Library , Customarily, it will be defined as common
We have a new one called common Of package, Then create... In it common_util file , The new class CommonUtil Cut public methods into this class

Then other classes only need to inherit this class 【python Basic knowledge of 】

The above is what we learn unittest Time thinking . But now if there is The new demand : Only one use case requires preconditions , The rest don't need . What should I do at this time ? It seems that this method of inheritance is not so flexible .
This leads to fixture Come on Realize partial pre post
grammar :@pytest.fixture()
Parameters :
scope -- Scope
function: Before and after the function
class: class After and before execution
package/session: The whole project session After and before execution
autouse -- Automatic execution
Default :False; If instead True, You don't need to call , In the corresponding scope, it will automatically execute
...
Let's start with a popular example :
hold CommonUtil The code inside is commented out , direct pass, Let it no longer take effect

Then modify the test file :

After execution, you can see , Only test_py Method is called exe_sql Method .
Add the second parameter autouse The value is True, After execution, you can see that all use cases reference exe_sql Method

up to now , All are “ Before ” perform ,“ after ” How to do it?
“ after ” Use yield

Execution results :

notes : In a file There can be multiple fixture, You can add as many as you need
however fixture This firmware Only effective in the current document , Other documents will not take effect , If other files are to take effect, configure conftest.py The file .
And there are many other parameters , We'll talk about these tomorrow .
The following is the supporting information , For doing 【 software test 】 For our friends, it should be the most comprehensive and complete war preparation warehouse , This warehouse also accompanied me through the most difficult journey , I hope it can help you !
Last : It can be in the official account : Programmer Xiaohao ! Get a free copy of 216 Page software testing engineer interview guide document information . 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. .
If my blog helps you 、 If you like my blog content , please “ give the thumbs-up ” “ Comment on ” “ Collection ” One button, three links ! Friends who like software testing , You can join our testing technology exchange group :779450660 There are various software testing resources and technical discussions )
边栏推荐
- FlutterWeb浏览器刷新后无法回退的解决方案
- MySQL stored procedure
- Interpretation of Flink source code (II): Interpretation of jobgraph source code
- kivy教程之在 Kivy 中支持中文以构建跨平台应用程序(教程含源码)
- Xin'an Second Edition: Chapter 23 cloud computing security requirements analysis and security protection engineering learning notes
- Smart street lamp based on stm32+ Huawei cloud IOT design
- Xin'an Second Edition: Chapter 25 mobile application security requirements analysis and security protection engineering learning notes
- 偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
- BearPi-HM_ Nano development board "flower protector" case
- Example of batch update statement combining update and inner join in SQL Server
猜你喜欢

Virtual machine startup prompt probing EDD (edd=off to disable) error

10 advanced concepts that must be understood in learning SQL

Establishment of graphical monitoring grafana

Unity tips - draw aiming Center

Automatic operation and maintenance sharp weapon ansible Foundation

在一台服务器上部署多个EasyCVR出现报错“Press any to exit”,如何解决?

EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
![[introduction to MySQL] third, common data types in MySQL](/img/11/66b4908ed8f253d599942f35bde96a.png)
[introduction to MySQL] third, common data types in MySQL

Interview assault 63: how to remove duplication in MySQL?

The most complete tcpdump and Wireshark packet capturing practice in the whole network
随机推荐
Xin'an Second Edition: Chapter 12 network security audit technology principle and application learning notes
2021-03-22 "display login screen during recovery" can't be canceled. The appearance of lock screen interface leads to the solution that the remotely connected virtual machine can't work normally
视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备
EasyCVR授权到期页面无法登录,该如何解决?
node の SQLite
MySQL 8 sub database and table backup database shell script
Shell input a string of numbers to determine whether it is a mobile phone number
一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升
【MySQL入门】第一话 · 初入“数据库”大陆
Solr appears write Lock, solrexception: could not get leader props in the log
【Elastic】Elastic缺少xpack无法创建模板 unknown setting index.lifecycle.name index.lifecycle.rollover_alias
Alertmanager sends the alarm email and specifies it as the Alibaba mailbox of the company
Display picture of DataGridView cell in C WinForm
[ASM] introduction and use of bytecode operation classwriter class
自动化运维利器ansible基础
远程代码执行渗透测试——B模块测试
开源与安全的“冰与火之歌”
Nodejs 开发者路线图 2022 零基础学习指南
JMeter interface test response data garbled
Single responsibility principle