当前位置:网站首页>Pytest framework
Pytest framework
2022-06-21 17:02:00 【Expert of explosive liver fist】
One 、Pytest brief introduction
1.pytest It's a python Unit framework , It's a third party library .
2.pytest Framework is one of the components of automation framework , You can talk to selenium,requests,appium Combine to realize interface automation ,web automation ,app automation .
3.pytest You can skip test cases and reruns Failed use case retries .
4.pytest You can talk to allure Generate very beautiful test reports .
5.pytest You can talk to Jenkins Continuous integration .
6.pytest There are many very powerful plug-ins , And these plug-ins can realize many practical operations . for example :
pytest-html( Generate html Automated test report in )
pytest-xdist( Distributed execution of test cases , many CPU distribution )
pytest-ordering( Used to change the execution order of test cases )
pytest-rerunfailures ( Run again after the use case fails )
allure-pytest ( Used to generate beautiful test reports )
Installation method :pip install pytest
Two 、Pytest Default usage rules for
1. Module name must be test_ Begin with or with _test ending ;
2. The test class name must begin with Test start , And there can be no init Method ;
3. The test method name must begin with test start ;
4. Assertions use basic assert that will do .
3、 ... and 、Pytest And Unittest The difference between 
Four 、Pytest How test cases run
Coincidence point “.” Means that the test passes ,“F” Means not to pass
1. Main function mode
(1) Run all :pytest.main()
(2) Run the specified module :pytest.main(['-vs','test_login.py'])
(3) Run the specified folder :pytest.main(['-vs','./interface_testcase'])
(4) adopt nodeid Specify use case execution :nodeid from ./ file name / Module name :: Class name :: Method name / Function name form .
# function test_04func function
pytest.main(['-vs','./interface_testcase/test_interface.py::test_04func'])
# function test_03_zhiliao Method
pytest.main(['-vs','./interface_testcase/test_interface.py::TestInterface::test_03_zhiliao'])
notes :#interface For folder name
#test_interface.py For the module name
#TestInterface For class name
#test_04_func And test_03_zhiliao For a method or function
Parameters, :
-s: Indicates output debugging information , Include print Printed information
-v: Show more details ( Such as : The directory where the file is located , Whether the test passes, etc )
-vs: Merge display
-n: Support multithreading or distributed running of test cases .
Such as :pytest -vs ./testcase/test_login.py -n 2( It means running in two threads )
–reruns: --reruns=2( Failed use case rerun 2 Time )
-x: ( It means that as long as there is an error in one use case , Then the test stops )
–maxfail=2:( Stop testing when two use cases fail )
-k:( Specify the test case according to the partial string of the test case )
Such as :pytest.main(['-vs','./test_01.py::TestAA','-k=aa']) The execution contains aa Test cases for
2. By reading the pytest.ini Profile run
(1) Location : It is usually placed in the root directory of the project
(2) code : Must be ANSI, have access to notpad++ Modify encoding format
(3) effect : change pytest The default behavior
(4) Rules of operation : Whether running in main function mode , Still run in command line mode , Will read this configuration file
pytest.ini The contents of the configuration file are as follows :
[pytest]
# Command line arguments , When there are multiple parameters, separate them with spaces
addopts=-vs -reruns
# Test case folder ( You can configure it yourself )
testpaths=../pytestproject
# Module file name for configuration test search (test start , Modifiable )
python_files=test*.py
# Configure the test class name for the test search ( With Test or AAA start , Modifiable )
python_classes=Test* AAA*
# Test function name for configuring test search (test start , Modifiable )
python_functions=test
# Manage tags , The tag name can be set by itself
markers=
smoke: Smoke use case
usermanage: User management module
productmanage: Commodity management module
5、 ... and 、 The execution order of use cases
Unittest: Is in accordance with the Ascll To execute
Pytest: By default, it is executed from top to bottom
modify Pytest Execution sequence method of :
Enter... Before the method that is executed first @pytest.mark.run(order=1) ( Press order From 1…n Sequential execution )
6、 ... and 、 Group execution ( smoking , Execute in modules , Sub interface and web perform )
1. When only smoking cases are executed
Use case code :
class TestAA:
@pytest.mark.smoke # smoking
def testaa(self):
time_loc=time.strftime('%H-%M-%S',time.localtime())
time.sleep(2)
print(time_loc+' Hello! bb ah ')
pytest.ini The configuration file :
[pytest]
# Command line arguments , When there are multiple parameters, separate them with spaces
addopts=-vs -m=smoke
# Test case folder ( You can configure it yourself )
testpaths=./ #(./ Represents all files in the root directory )
.....
.....
# Manage tags , The tag name can be set by itself
markers=
smoke: Smoke use case
Run code :pytest -m ' smoke '
2. When executing multiple use cases with different tags
for example : Both implementation smoke The use case executes again usermanage Use cases
pytest.ini The configuration file :
[pytest]
# Command line arguments , When there are multiple parameters, separate them with spaces
addopts=-vs
# Test case folder ( You can configure it yourself )
testpaths=./ #(./ Represents all files in the root directory )
.....
.....
# Manage tags , The tag name can be set by itself
markers=
smoke: Smoke use case
usermanage: User management module
Run code :pytest -m 'smoke or usermanage'
7、 ... and 、 Skip test cases
1. Jump unconditionally
@pytest.mark.skip(reason=' Overbearing to protect his wife ')
2. Conditional skip
# If age Is greater than or equal to 18 when , Skip execution
@pytest.mark.skipif(age>=18,reason=' adult ')
边栏推荐
- Pytest--生成测试报告
- The "learning link" database of the learning software is suspected to have leaked information, revealing more than 100million pieces of student information
- Serious illness insurance covers serious illness. Which product is the best in the market? Please recommend it
- What are the differences between SVN and VSS
- 带你区分几种并行
- HUAWEI(13)——路由引入
- 华为云发布《云原生2.0架构白皮书》,GaussDB技术再升级
- Wechat applet tabbar usage
- 快来围观–TPT18新版报到
- 学习软件“学习通”数据库疑似发生信息泄露,泄露学生信息达1亿多条
猜你喜欢

2021数据库市场,Aerospike与顶级厂商争锋

Standing at the digital tuyere, how can tooling enterprises "fly"

The release of autok3s v0.5.0 continues to be simple and friendly

Online shopping website (final assignment)

Online text list batch add line number tool

Cisco(35)——BGP入门实验

Cisco (35) -- BGP introduction experiment

机器学习中的概念漂移(Aporia)

How to write test cases

在线文本列表批量添加行号工具
随机推荐
The new download window of Google Chrome browser supports file dragging, and edge already supports
首批入围企业公示!年度TOP100智能网联供应商评选
Some thoughts learned recently are attached with answers, and further study and development of knowledge are required in the future.
使用 Guzzle 中间件进行优雅的请求重试
带你区分几种并行
Reinforcement learning introductory project spinning up (1) installation
Serious illness insurance covers serious illness. Which product is the best in the market? Please recommend it
[observation] Microsoft's "cloud + end" comprehensive innovation makes hybrid cloud simpler, more flexible and more secure
【直播预告】6月24日 19点 HCSD大咖直播--就业指南,针对即将到来的秋招和暑期实习,带你梳理面试要点~~
ESP8266/ESP32 通过TimeLib库获取NTP时间方法
【观察】微软“云+端”全面创新,让混合云更简单、更灵活、更安全
关于修订《北京市共有产权住房规划设计宜居建设导则(试行)》的通知
【SQLite】解决unrecognized token:“‘“
Cloud native hybrid cloud network interconnection
Pingcap was selected as the "voice of customers" of Gartner cloud database in 2022, and won the highest score of "outstanding performer"
Online JSON to yaml tool
[1108. Invalidation de l'adresse IP]
Characteristics of a good product
The first atlas showing the development history of the database in China was officially released!
华为云发布《云原生2.0架构白皮书》,GaussDB技术再升级