当前位置:网站首页>Pytest testing framework
Pytest testing framework
2022-07-02 02:03:00 【Lao Xiao of Buddhism】
List of articles
brief introduction
pytest be based on unittest Encapsulated third-party testing framework , Easy to learn and easy to use , Very good compatibility , Support UI Automated script development , Support interface automation script development , Support continuous integration jenkins, Support distributed execution , Support error debugging , Support error rerun mechanism , Support allure Reporting framework
Environment configuration ( install )
pip install pytest
— Script development and executionpip install pytest-html
— The script runs to generate the test reportpip install pytest-xdist
— Script runs distributed execution
Rule of grammar
- pytest Script name naming : With test Beginning or test ending
- pytest Class name in script : With Test start
- pytest Functions in scripts / Method name : With test start
- pytest The package of the script must have __init__.py
- pytest Constructors are not allowed in classes in scripts __init__
- pytest Scripts act on functions , It can also act on classes and methods
- pytest When script running is not set , Install functions or classes and methods ASCII Sequential execution
Example :
- Guide pack import pytest
- Defining classes / function test
- Assertion :assert
- perform :pytest.main([‘test005.py’]), The execution parameter must be list Tabular form
Way of execution
pytest.main(['[ Script name ]','-s'])
Execute the use case under the current script , Print printpytest.main(['[ Script name ]:: The name of the class under the script :: Method name under class ','-v'])
Specify the method execution use case under the classpytest.main(['[ Script name ]:: The name of the class under the script ', '-v','--html=report.html'])
Specify the test class to execute the use casepytest.main([__file__])
# Specify the current module to perform the test
Execution parameter
-s
: perform print()pytest.main(['[ Package name ]:: Class name ','-s'])
-v
: Detailed output logpytest.main(['[ Package name ]:: Class name :: Method name ','-v'])
-n
: Specify the number of distributed executions ‘-n’ ‘2’pytest.main(['[ Package name ]','-n','2'])
-q
: Quiet mode , Minimalist mode , Do not load environment configuration information and log information , Try to be with -v Don't use... At the same timepytest.main(['[ Package name ]','-q'])
-m
: Mark execution , Support for logical operators-k
: Mark execution , Fuzzy matching Support for logical operators-x
: If the execution fails once, the test will be terminated--maxfail=1
: Set the maximum number of failures--rerun=2
: The specified number is twice Need to install pytest-rerunfailures
A parameterized
Single parameter
@pytest.mark.parametrize(' Parameters ',[' The corresponding value of the parameter '])
Example :
@pytest.mark.parametrize('username',['admin'])--- Single parameter
A set of parameters
@pytest.mark.parametrize(' Parameters 1, Parameters 2',[[' Parameters 1 Corresponding value ',' Parameters 2 Corresponding value ']])
Example :
@pytest.mark.parametrize('username,password',[['admin','milor123']]) A set of parameters
Multiple sets of parameters
@pytest.mark.parametrize(' Parameters 1, Parameters 2',[[' Parameters 1 Corresponding value ',' Parameters 2 Corresponding value '],[' Parameters 1 Corresponding value ',' Parameters 2 Corresponding value ']])
Example :
@pytest.mark.parametrize('username,password',[['admin','milor123'],['lm','LiuM123']])
Skip execution
- Jump unconditionally :@pytest.mark.skip(reason=‘ There is no reason to ’)
- Conditional skip :@pytest.mark.skipif(1<2,reason=‘ When 1<2 when , skip ’)
- Unconditionally skip inside a method or function :pytest.skip(msg=‘skip’)
Mark execution
-k
Fuzzy match mark Mark the test case containing a string to perform the operation ( Support for logical operators and or )example
pytest.main([__file__,'-s','-k','login']) # The name of the execution case contains login Test cases for pytest.main([__file__, '-s', '-k', 'login and index ']) # The name of the execution case contains login and index Test cases for pytest.main([__file__, '-s', '-k', 'login or index ']) # The name of the execution case contains login or index Test cases for
-m
Mark Set up markers , In profile pytest.ini Set up markers, Support setting multiple tags , Application scenarios : Smoke testing 、 Regression tests, etc , Support for logical operators and or1、
pytest.ini
Put in pytest In the working directory , Create this file , Edit content2、 Use decorators on test cases that need to be marked
@pytest.mark.smoke
3、 During test execution , Input
-m
Parameters are marked for executionTips : If the following warnings appear during operation, it is not pytest Set the marker in
ytestUnknownMarkWarning: Unknown pytest.mark.smoke
Code instance :
Parameters can also be written like this :
Front and back
setup / teardown
unittest Test the front and rear in the framework , stay pytest compatible ,function and method The function is similar to the structure __init__ And Deconstruction __del__
Each test case should be executed once before execution setup
Each test case should be executed once after execution teardown
Applicable to methods under functions or classes — The test case
setup_function /teardown_function
Apply to functions , Before each test function is executed setup initialization , After execution teardown Recycle resources , Restore the scene
setup_module / teardown_module
Apply to functions , For the current module , Before all test functions are executed setup initialization , After execution teardown Recycle resources , Restore the scene
setup_class / teardown_class
Apply to classes , Before all test functions are executed setup initialization , After execution teardown Recycle resources , Restore the scene
setup_method / teardown_methon
Apply to classes , Before the execution of each test method setup initialization , After execution teardown Recycle resources , Restore the scene
summary :
- Test scenarios : The repeated actions to be done before the execution of each test case , Repeated actions to be performed after execution , such as : When the same preconditions are required for testing a single module , This method can be applied . Test login , Login use cases cover 10 Use cases , You need to open the browser before each execution , You need to close the browser after execution setup setup_function setup_method
Test scenarios : An object is required for the execution of all test cases dr Or some kind of state session, Need continuous use , This method can be applied , such as : Test the module after login , Need to log in driver Or conversation session setup_module setup_class
The firmware
grammar :
@pytest.fixture(scope='function',name='myfix',autouse=True)
- scope: Scope of application , Default function, Support class,method,module
- autouse: The default value is False, Set to True, Will automatically execute the firmware decorator
- name: Alias firmware decorator -
Code :
- Application scenarios : Similar to the pre post usage , such as :UI In automated testing driver And resource recycling , Initialization and resource recovery in interface automation testing
Code demonstration :
Script execution mode
In the tools run Script , go main entrance
Run in command line mode pytest Script name
pytest [ Script name .py]::test_login -s -m smoke
The test case under the specified module is marked for execution , And print print Content
Execution order
From top to bottom , have access to order Parameters to sort
@pytest.mark.run(order=2)
@pytest.mark.run(order=1)
边栏推荐
- Niuke - Huawei question bank (51~60)
- leetcode373. Find and minimum k-pair numbers (medium)
- 并发编程的三大核心问题
- 734. Energy stone (greed, backpack)
- golang---锁
- Sword finger offer 31 Stack push in and pop-up sequence
- MySQL如何解决delete大量数据后空间不释放的问题
- 开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?
- How to debug apps remotely and online?
- Open that kind of construction document
猜你喜欢
What are the necessary things for students to start school? Ranking list of Bluetooth headsets with good sound quality
leetcode2310. The one digit number is the sum of integers of K (medium, weekly)
The concept, function, characteristics, creation and deletion of MySQL constraints
What is AQS and its principle
leetcode2310. 个位数字为 K 的整数之和(中等,周赛)
leetcode373. Find and minimum k-pair numbers (medium)
Cross domain? Homology? Understand what is cross domain at once
剑指 Offer 62. 圆圈中最后剩下的数字
leetcode2312. Selling wood blocks (difficult, weekly race)
How to solve MySQL master-slave delay problem
随机推荐
What are the skills of spot gold analysis?
Six lessons to be learned for the successful implementation of edge coding
The concepts and differences between MySQL stored procedures and stored functions, as well as how to create them, the role of delimiter, the viewing, modification, deletion of stored procedures and fu
Volume compression, decompression
【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
AR增强现实可应用的场景
leetcode2309. The best English letters with both upper and lower case (simple, weekly)
Design and implementation of key value storage engine based on LSM tree
WebGPU(一):基本概念
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
Construction and maintenance of business websites [12]
【C#】使用正则校验内容
Makefile simple induction
Open that kind of construction document
2022 Q2 - 提升技能的技巧总结
Iterative unified writing method of binary tree
With the innovation and upgrading of development tools, Kunpeng promotes the "bamboo forest" growth of the computing industry
This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
剑指 Offer 47. 礼物的最大价值
This is the form of the K-line diagram (pithy formula)