当前位置:网站首页>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 .
边栏推荐
- 关于GBase 自动关闭连接问题
- 新库上线| CnOpenDataA股上市公司股东信息数据
- 分布式爬虫中的增量爬虫
- 数据提交类型 Request Payload 与 Form Data 的区别总结
- CTF Crypto---RSA KCS1_OAEP模式
- Vscode saves setting configuration parameters to the difference between users and workspaces
- for循环与if判断语句的运用
- Matlab self programming series (1) -- angular distribution function
- [semidrive source code analysis] [drive bringup] 38 - norflash & EMMC partition configuration
- BOM概述
猜你喜欢

PADS导出gerber文件

CTF Crypto---RSA KCS1_OAEP模式

一日千里,追风逐月 | 深势科技发布极致加速版分子对接引擎Uni-Docking

From the era of portal to the era of information flow, good content has been ignored?

Luo min's backwater battle in qudian

2022 Tiangong cup ctf--- crypto1 WP

Xinku online | cnopendata shareholder information data of A-share listed companies

钉钉最新版,怎么清除登录手机号历史记录数据

华为无线设备配置WPA2-802.1X-AES安全策略

A domestic open source redis visualization tool that is super easy to use, with a high-value UI, which is really fragrant!!
随机推荐
What if Oracle 19C migration encounters large lob tables?
How to use network installation to deploy multiple virtual servers in KVM environment
js无法获取headers中Content-Disposition
[cloud native] the ribbon is no longer used at the bottom of openfeign, which started in 2020.0.x
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)
leetcode刷题:动态规划06(整数拆分)
如何在KVM环境中使用网络安装部署多台虚拟服务器
Gan series of confrontation generation network -- Gan principle and small case of handwritten digit generation
When providing digital talent services, Xi Zhi quickly opened its own digital school for each organization
Purpose of SQL square brackets
Paper reading: UNET 3+: a full-scale connected UNET for medical image segmentation
【PyTorch】最常见的view的作用
Wechat applet wx.request interface
Devops has been practiced for many years. What is the most painful thing?
When importing data in batches, you always prompt "failure reason: SQL parsing failure: parsing file failure:: null". What's the matter?
冰冰学习笔记:类与对象(上)
Leetcode skimming: dynamic programming 06 (integer splitting)
Ideal L9, can't cross a pit on the road?
Talk about practice, do solid work, and become practical: tour the digitalized land of China
Luo min's backwater battle in qudian