当前位置:网站首页>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 )
边栏推荐
- kivy教程之在 Kivy 中支持中文以构建跨平台应用程序(教程含源码)
- Xin'an Second Edition: Chapter 23 cloud computing security requirements analysis and security protection engineering learning notes
- Flink analysis (I): basic concept analysis
- Flink parsing (VII): time window
- Reppoints: advanced order of deformable convolution
- pip install pyodbc : ERROR: Command errored out with exit status 1
- 远程代码执行渗透测试——B模块测试
- Precipitated database operation class - version C (SQL Server)
- After entering Alibaba for the interview and returning with a salary of 35K, I summarized an interview question of Alibaba test engineer
- How uipath determines that an object is null
猜你喜欢

Zen integration nails, bugs, needs, etc. are reminded by nails

10 advanced concepts that must be understood in learning SQL

Wordcloud colormap color set and custom colors

Flink parsing (IV): recovery mechanism

历史上的今天:Google 之母出生;同一天诞生的两位图灵奖先驱

【MySQL入门】第三话 · MySQL中常见的数据类型

Unity粒子特效系列-闪星星的宝箱
![[getting started with MySQL] fourth, explore operators in MySQL with Kiko](/img/11/66b4908ed8f253d599942f35bde96a.png)
[getting started with MySQL] fourth, explore operators in MySQL with Kiko

偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”

BearPi-HM_ Nano development environment
随机推荐
Cool Lehman has a variety of AI digital human images to create a vr virtual exhibition hall with a sense of technology
Establishment of graphical monitoring grafana
Unity particle special effects series - treasure chest of shining stars
EasyCVR授权到期页面无法登录,该如何解决?
视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备
TCP连接不止用TCP协议沟通
Openharmony developer documentation open source project
【MySQL入门】第四话 · 和kiko一起探索MySQL中的运算符
Alibaba brand data bank: introduction to the most complete data bank
BearPi-HM_ Nano development board "flower protector" case
Development and practice of lightweight planning service tools
一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升
Summary of Android interview questions of Dachang in 2022 (I) (including answers)
Xin'an Second Edition: Chapter 23 cloud computing security requirements analysis and security protection engineering learning notes
C WinForm series button easy to use
网络分层概念及基本知识
偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
Guidelines for preparing for the 2022 soft exam information security engineer exam
[introduction to MySQL] the first sentence · first time in the "database" Mainland
EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?