当前位置:网站首页>Pytest test framework Basics
Pytest test framework Basics
2022-06-11 16:17:00 【Abstinence Libai lisage】
Today I'd like to share with you pytest Basics of test framework , The content includes :
- understand pytest Features of the test framework
- master pytest Basic use
- master pytest Common scenes
- master pytest Operating parameters and configuration methods
Catalog
One 、 The role and type of testing framework
1.2 About Python Test framework for
Two 、pytest The test framework
2.3 Write test cases 【 a key 】
2.4 Operation parameters and their configuration
One 、 The role and type of testing framework
1.1 The test framework
Concept : The test framework , Discover test cases 、 organization 、 perform 、 Record 、 Report and other management operations , So as to help testers better design 、 Develop automated test scripts .
1.2 About Python Test framework for
- UnitTest
- pytest
- Robot Framwork == RF
| frame | advantage | shortcoming |
|---|---|---|
| RF | Keyword Driven Rich plug-ins Good scalability | It is not convenient to customize the test report Inconvenient to test |
| UnitTest | python built-in xUnit usage Easy to use | Naming style and Python atypism Too much support for abstraction , object-oriented Missing plug-in , There's no way to expand |
| Pytest | Object orientation is not required Very flexible to use Rich plug-ins | Incompatible with other frameworks Requirements python Basics |
Common ground :
1. Tag and filter use cases
2. Execute use cases
3. Record the test process
4. You can manage the test environment
5. Generate test reports
Two 、pytest The test framework
pytest It's more pythonic Test framework for , With strong style characteristics .
pythonic, A simple translation is python style ,pythonic code, That is to say, the code you write should have a strong python Normative and python style
2.1 install pytest
pip install pytestBe careful :pytest The ecology of pytest Itself and the pytest Plug-ins together , In addition to itself, you also need some plug-ins .
pytest:pytest Frame body
pytest-html: Generate HTML Test report
pytest-xdist: Parallelize execution of test cases
pytest-rerunfailures: Fail to run again
pytest-ordering: Sort the use cases
allure-pytest: Generate allure Test report ( The more advanced )
One click installation of all :
pip install pytest pytest-html pytest-xdist pytest-rerunfailures pytest-ordering allure-pytest2.2 start-up pytest
1. Execute enter... On the command line pytest ---- Where to install , Just type in... Somewhere ( Suggest )
2.python Execute in code pytest.main()
3.pycharm Provides testrunner( Not recommended )
- Execute enter... On the command line pytest effect

Text Test report
1. Report header :
- Platform and version information
- root directory 、 The configuration file 、 Special options
- Plug in list
2. Collect information
- The number of test cases
3. Execution status
- The execution result of the use case
- Test execution progress
4. Abstract
- Error message 、 Captured output
- Results statistics
- Time consuming Statistics
2.3 Write test cases 【 a key 】
2.3.1 Discovery rules
pytest Test case discovery rules :
1. Start from the current directory , Traverse each directory
2. Search the directory for test_*.py and *_test.py, And import
3. In the imported file , Collect test cases :
- test Initial function
- Test Initial class , And its test Opening method
- unittest.TestCase Subclasses of , And its test Opening method (unitest Test cases can be pytest compatible , Equivalent to using pytest Framework to execute unittest The use case )
The following are examples to illustrate
be based on test Initial function
def test_login(self):
passbe based on Test Initial class
class TestAPI:
def test_login(self):
passbe based on unittest.TestCase Subclasses of
import unittest
class test(unittest.TestCase): # If it is unittest.Testcase Subclasses of , Naming is not required
def test_login(self):
pass2.3.2 Practice method
1. establish test_ Opening file :test_api.py
2. In the document , establish test_ Initial function :test_login
3. Use the assertion keyword in a function to assert :assert
Expand :
class TestAPI:
def __init__(self): # As a test case class No instantiation is allowed __init__ Method
pass
def test_login(self):
passThe effect of the above code is as follows :

This means that classes used for testing cannot have initialization methods __init__.
reflection :
If there are too many files , What should I do if I want to classify ?
Too many files can be put in any folder , Does not affect loading and execution ,pytest There are no requirements for folders .
practice : If the folder has everything related to the test , Use test perhaps test_ Let's start with .
2.4 Operation parameters and their configuration
2.4.1 Configuration item
It can be here pytest Read and view on the official website
pytest Official website :pytest: helps you write better programs — pytest documentation/
Or you can enter a command to help view
pytest -h
Here are some common configuration items :
-v Show details
-s Turn off output capture
-n X: Use X process , Parallelized running test cases ( A computer can write as many cores as it needs )
-n auto: Automatically select the number of processes to execute
--html=Path: Generate HTML Test report , And keep it in Path route
--self-contained-html:HTML File self-contained
--reruns X: After the test case fails , retry X Time
2.4.2 Configuration change
adopt pytest.ini Profile to configure
[pytest]
addopts = -v -s -n 1 --html=report.html
The operation effect is as follows :

Each effect in our configuration parameters will be displayed
3、 ... and 、 Conclusion
Today's sharing is mainly about pytest Explain the basics , I will share it later pytest Advanced and practical test framework , Interested friends continue to pay attention to , It's not easy to create , Look at the third company .
边栏推荐
- 减肥期间可以吃肉吗?会胖吗?
- [learn FPGA programming from scratch -18]: quick start chapter - operation steps 2-6- VerilogHDL sequential circuit syntax analysis (taking the counter as an example)
- 无心剑英汉双语诗001. 《春游》
- CLP information - No. 1 central document on Strengthening Rural Revitalization financial services
- Ai4db: AI slow SQL root cause analysis
- 基于ssm框架实现的企业进销存管理系统【源码+数据库+毕设】
- Take you in-depth understanding of AGC cloud database
- Will you be punished for not wearing seat belts in the back row?
- 项目工作区创建步骤-泽众AR自动化测试工具
- How can the project manager repel the fear of being dominated by work reports?
猜你喜欢

Opengauss database ODBC environment connection configuration (Windows)

1267_ FreeRTOS starts the first task interface prvportstartfirsttask implementation analysis

如何优化 Compose 的性能?通过「底层原理」寻找答案 | 开发者说·DTalk

This "invisible" robot may be your future colleague

20 full knowledge maps of HD data analysis have been completed. It is strongly recommended to collect them!

laravel 2020-01-01T00:00:00.000000Z 日期转化

Collection | can explain the development and common methods of machine learning!

Pytest测试框架基础篇
![Complete test process [Hangzhou multi tester] [Hangzhou multi tester \wang Sir]](/img/f7/d9bdd667e6e34b99940b9c2ecac061.png)
Complete test process [Hangzhou multi tester] [Hangzhou multi tester \wang Sir]

C# 启动一个外部exe文件,并传入参数
随机推荐
MSDN download win11 method, simple and easy to operate
Laravel listening mode
Nat Commun|语言模型可以学习复杂的分子分布
Yiwenjiaohui your database system tuning
one hundred and twenty-three thousand one hundred and twenty-three
postgresql创建数据库
Nat Commun|語言模型可以學習複雜的分子分布
Opengauss version 3.0.0 was officially released, and immediately experience the first lightweight version in the community
leetcode463. 岛屿的周长(简单)
leetcode-141. Circular linked list
整了20张高清数据分析全知识地图,强烈建议收藏!
After reading the book reading methods
Using cloud DB to build apps quick start - quick games
Classmate, have you heard of mot?
wget命令使用
Frontier technology exploration deepsql: in Library AI algorithm
电脑下面的任务栏怎么显示打开的程序
High concurrency pseudo sharing and cache line filling (cache line alignment) (@contained)
真香,华为主动离职也给 N+1
Application of AI in index recommendation