当前位置:网站首页>Have you got the advanced usage of pytest?
Have you got the advanced usage of pytest?
2022-07-25 07:22:00 【Code classmate software test】
picture
At present, we have heard a lot about unit testing frameworks , does pytest and unittest.unittest It's the old framework , Many people use it for Automation , Whether it's UI Or interface .pytest Is based on unittest Developed another more advanced and more useful unit testing framework , It is used frequently in both interview and work , The momentum is slightly better unittest.

pytest The official website of , It has the following characteristics :
l Very easy to get started , Easy entry , The document is rich , There are many examples in the document
l Support simple unit testing and complex functional testing
l Support parameterization
l Some tests can be skipped during test execution (skip), Or for something that fails in anticipation case Marked as failed
l Support for repeat execution (rerun) The failure of the case
l Support run by nose, unittest Written tests case
l Generative html The report
l Convenient and continuous integration tools jenkins Integrate
l It can support the execution of partial use cases
l There are many third-party plug-ins , And you can customize the extension
I believe you should have used more or less pytest frame , So today's article will share with you what you may not know pytest Advanced usage of . Let's talk about it conftest.py and fixture Device function .
conftest.py What is it? ? It can be understood as a special storage fixture Configuration file for . You can manage some global fixture.
conftest.py To configure fixture matters needing attention
· pytest Will read by default conftest.py Everything in it fixture
· conftest.py File name is fixed , Can not change
· conftest.py Only for the same package All of the test cases under are valid
· Different directories can have their own conftest.py, There can be more than one in a project conftest.py
· There is no need to manually import conftest.py,pytest It will look up automatically
that fixture What is a function ? Children, do you have many question marks ?
fixture Let's customize the preconditions of test cases
fixture The advantages of
· Flexible naming , Not limited to setup and teardown These names
· conftest.py Data sharing can be realized in the configuration , Unwanted import You can find it automatically fixture
· scope=“module” Can achieve more than one .py Cross file sharing front
· scope=“session” To achieve multiple .py Use a... Across files session To complete multiple use cases
fixture The syntax of the function is as follows

Parameter meaning :
scope: Can be interpreted as fixture Scope of action , Default :function, also class、module、package、session four 【 Commonly used 】
autouse: Default :False, You need the use case to call this manually fixture; If it is True, This is automatically called by all test cases in the scope fixture
name: Default : Name of decorator , The first mock exam fixture Calling each other suggests writing a different name
Calling method :
Just reference fixture The test case of the function is passed in by @pytest.fixture The name of the function decorated by this decorator , Will call fixture Functions defined in the function , use yield Keyword to divide is setup still teardown,yield The previously implemented function is setup Initialization function ,yield The later functions are teardown Clearing function .
Next, write the test case of Douban login , The project structure is as follows

case— The test case , The code is as follows :

base_driver— encapsulation app Of driver

conftest.py It defines the global fixture function

The command line executes the following command to execute case Implementation :

In this way, the login function of Douban can be realized , After the function level test case is executed , wait for 3 Second ,driver Will automatically exit .
边栏推荐
- Price reduction, game, bitterness, etc., vc/pe rushed to queue up and quit in 2022
- Analysis of common classes of Servlet
- JS note 17: the whole process of jest project configuration of typescript project
- Meta is in a deep quagmire: advertisers reduce spending and withdraw from the platform
- paddlepaddle 34 调整模型的layer结构与forward流程(实现layer的增删改与forward的修改)
- Enable the free pan domain SSL certificate for kubesphere cluster and realize the automatic update and distribution of certificates
- runtimecompiler 和 runtimeonly是什么
- 新库上线| CnOpenDataA股上市公司股东信息数据
- From the era of portal to the era of information flow, good content has been ignored?
- LeetCode118. 杨辉三角
猜你喜欢

Huawei wireless device configuration wpa2-802.1x-aes security policy

QT6 with vs Code: compiling source code and basic configuration

Wechat applet switchtab transmit parameters and receive parameters

冰冰学习笔记:类与对象(上)

RPC communication principle and project technology selection

线代(矩阵‘)

What if Oracle 19C migration encounters large lob tables?

从ACL 2022 Onsite经历看NLP热点

With apple not making money, the 2trillion "fruit chain" abandons "fruit" and embraces "special"

Paper reading: UNET 3+: a full-scale connected UNET for medical image segmentation
随机推荐
3. Promise
【程序员2公务员】四、常见问题
PADS导出gerber文件
Beijing internal promotion | Microsoft STCA recruits nlp/ir/dl research interns (remote)
列表推导式
分布式爬虫中的增量爬虫
微信小程序switchTab传参以及接收参数
From the era of portal to the era of information flow, good content has been ignored?
Tp5.1 foreach adds a new field in the controller record, and there is no need to write all the other fields again without changing them (not operating in the template) (paging)
The relationship between Informatics, mathematics and Mathematical Olympiad (July 19, 2022) C
Configuring WAPI certificate security policy for Huawei wireless devices
[cloud native] the ribbon is no longer used at the bottom of openfeign, which started in 2020.0.x
Bingbing's learning notes: classes and objects (Part 1)
On the peak night of the 8 Oracle ace gathering, what technology hotspots did you talk about?
Wechat applet wx.request interface
Pads export Gerber file
YUV player
Decrypting numpy is a key difficulty in solving the gradient
【刷题笔记】搜索旋转排序数组
When providing digital talent services, Xi Zhi quickly opened its own digital school for each organization