当前位置:网站首页>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 .
边栏推荐
- Robot framework mobile terminal Automation Test ----- 01 environment installation
- Beijing internal promotion | Microsoft STCA recruits nlp/ir/dl research interns (remote)
- Devops has been practiced for many years. What is the most painful thing?
- Paper reading: UNET 3+: a full-scale connected UNET for medical image segmentation
- New functions of shixizhi are online. These new functions are online in June. Can you use them?
- Learn no when playing 9 | enterprise knowledge management is so simple because it uses
- DJI push code (one code for one use, limited time push)
- When importing data in batches, you always prompt "failure reason: SQL parsing failure: parsing file failure:: null". What's the matter?
- LeetCode118. 杨辉三角
- How to do a good job in safety development?
猜你喜欢

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

Openatom xuprechain open source biweekly report | 2022.7.11-2022.7.22

New tea, start "fighting in groups"

CTF Crypto---RSA KCS1_ Oaep mode

曼哈顿距离简介

《游戏机图鉴》:一份献给游戏玩家的回忆录

Beijing internal promotion | Microsoft STCA recruits nlp/ir/dl research interns (remote)

Meta is in a deep quagmire: advertisers reduce spending and withdraw from the platform

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

如何在KVM环境中使用网络安装部署多台虚拟服务器
随机推荐
LeetCode118. 杨辉三角
冰冰学习笔记:类与对象(上)
Flinkcdc2.0 uses flinksql to collect MySQL
第一启富金怎么样
9 best engineering construction project management systems
Paper reading: UNET 3+: a full-scale connected UNET for medical image segmentation
【程序员2公务员】三、资源搜集
OpenAtom XuperChain 开源双周报 |2022.7.11-2022.7.22
Alibaba cloud image address & Netease cloud image
北京内推 | 微软STCA招聘NLP/IR/DL方向研究型实习生(可远程)
【电脑讲解】NVIDIA发布GeForce RTX SUPER系列显卡,游戏玩家福利来了!
[programmer 2 Civil Servant] I. Basic Knowledge
Matlab self programming series (1) -- angular distribution function
【刷题笔记】搜索插入位置(二分法的活用)
Ask the bosses: MySQL CDC stores configuration data, and Kafka has history
scrapy定时爬虫的思路
[programmer 2 Civil Servant] III. resource collection
[programmer 2 Civil Servant] IV. common problems
Summary of differences between data submission type request payload and form data
阿里云镜像地址&网易云镜像