当前位置:网站首页>Pytest basic self-study series (I)
Pytest basic self-study series (I)
2022-07-04 04:25:00 【COCOgsta】
Video source :B standing 《2022 newest pytest Interface automation test framework , Three days to master pytest, Take you to write the best code !》
Organize the teacher's course content and test notes while studying , And share it with you , Infringement is deleted , Thank you for your support !
One 、pytest Unit test framework
(1) What is a unit test framework
Unit testing refers to software development , The smallest unit for software ( function , Method ) Check and test the correctness .
(2) Unit test framework
java:junit and testing
python:unittest and pytest
(3) What does the unit test framework mainly do ?
- Tests found : Find our test cases from multiple files
- The test execution : Execute in a certain order and rules , And generate results
- Test judgment : Judge the difference between the expected results and the actual results through assertions
- Test report : Statistical test progress , Time consuming , Passing rate , Generate test reports .
Two 、 What is the relationship between unit test framework and automation test framework ?
(1) What is automated testing framework
In order to complete the automatic test of the specified system , And a complete set of encapsulated code framework . It mainly encapsulates basic automation modules , Automatic management module , Automatic test statistics module .
(2) effect
- Improve test efficiency , Reduce maintenance costs
- Reduce human intervention , Improve the accuracy of the test , Increase code reuse
- The core idea is to enable people who do not understand the code to realize automatic testing through this framework
(3)pytest The relationship between unit test framework and automation test framework
Unit test framework : Just one of the components of the automated testing framework .
pom Design patterns
Data driven
Keyword Driven
Encapsulation of global configuration files
Log monitoring
selenium,requests Secondary packaging
Assertion
Report mail
more ...
3、 ... and 、pytest brief introduction
- pytest It's a very mature python Unit framework , Than unittest More flexible , Easy to use
- pytest You can talk to selenium,requests,appium In combination with implementation web automation , Interface automation ,app automation
- pytest You can skip test cases and reruns Failed use case retries
- pytest You can talk to allure Generate very beautiful test reports
- pytest You can talk to Jenkins Continuous integration
- pytest There are many very powerful plug-ins , And these plug-ins can realize many practical operations pytestpytest-xdist Distributed execution of test cases . many CPU distribution .pytest-ordering Used to change the execution order of test cases pytest-rerunfailure Run again after the use case fails pytest-html( Generate html Automated test report in )allure-pytest Used to generate beautiful test reports
pytest Installation method :
Put the installation package name in requirements.txt in , adopt pip install -r requirements.txt Install everything
pytest
pytest-html
pytest-xdist
pytest-ordering
pytest-rerunfailures
allure-pytestAfter the installation , see pytest Version information
(venv) guoliangs-MacBook-Pro-15-inch:pytest guoliang$ pytest --version
pytest 7.1.2Four 、 Use pytest, Default test case rules and basic application
- Module name must be test_ The beginning or _test ending
- The test class must be Test start , And there can be no init Method
- The test method must be test start
5、 ... and 、pytest How test cases run
- Main function mode
(1) Run all :pytest.main()
import pytest
class TestLogin:
def test_01_baili(self):
print(" Test Baili ")
if __name__ == '__main__':
pytest.main()Execution results :
"/Users/guoliang/Documents/Source Code/pytest/venv/bin/python" "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --path "/Users/guoliang/Documents/Source Code/pytest/testcase/test_login.py"
Testing started at Afternoon 9:23 ...
Launching pytest with arguments /Users/guoliang/Documents/Source Code/pytest/testcase/test_login.py --no-header --no-summary -q in /Users/guoliang/Documents/Source Code/pytest/testcase
============================= test session starts ==============================
collecting ... collected 1 item
test_login.py::TestLogin::test_01_baili PASSED [100%] Test Baili
============================== 1 passed in 0.01s ===============================
Process finished with exit code 0
(2) Specify modules :pytest.main(['-vs', 'test_login.py'])
(3) Specify the directory :pytest.main(['-vs', './interface_testcase'])
(4) adopt nodeid Specify the use case run :nodeid By module name , Separator , Class name , Method name , The function name consists of .
pytest.main(['-vs', './interface_testcase/test_interface.py::test_04_func'])
pytest.main(['-vs', './interface_testcase/test_interface.py::TestInterface::test_01_zhiliao'])
- Command line mode
(1) Run all :pytest
(venv) guoliangs-MacBook-Pro-15-inch:pytest guoliang$ pytest
========================================================================== test session starts ===========================================================================
platform darwin -- Python 3.7.9, pytest-7.1.2, pluggy-1.0.0
rootdir: /Users/guoliang/Documents/Source Code/pytest
plugins: xdist-2.5.0, forked-1.4.0, metadata-2.0.1, allure-pytest-2.9.45, rerunfailures-10.2, html-3.1.1, ordering-0.6
collected 1 item
testcase/test_login.py . [100%]
=========================================================================== 1 passed in 0.02s ============================================================================
(venv) guoliangs-MacBook-Pro-15-inch:pytest guoliang$
(2) Specify modules :pytest -vs ./testcase/test_login.py
(3) Specify the directory :pytest -vs ./interface_testcase
(4) adopt nodeid Specify the use case run :
pytest -vs ./interface_testcase/test_interface.py::test_04_func
pytest -vs ./interface_testcase/test_interface.py::TestInterface::test_01_zhiliao
Parameters, :
-s: Indicates that the output debugging is new , Include print Printed information
-v: Show more details
-vs: Two parameters are used together
-n: Support multithreading or distributed running of test cases
- Such as :pytest.main(['-vs', './testcase', '-n=4'])、pytest -vs ./testcase/test_login.py -n 2
--reruns: Failed use case rerun
- Such as :pytest.main(['-vs', './testcase', '--reruns=2'])、pytest -vs ./testcase/test_login.py --reruns 2
-x: It means that as long as there is an error in one use case , Then the test stops
- Such as :pytest.main(['-vs', './testcase', '-x'])、pytest -vs ./testcase/test_login.py -x
--maxfail: Stop when the specified number of use cases fails
- Such as :pytest.main(['-vs', './testcase', '--maxfail=2'])、pytest -vs ./testcase/test_login.py --maxfail 2
-k: Specify the test case according to the partial string of the test case .
- Such as :pytest.main(['-vs', './testcase', '-k=ao'])、pytest -vs ./testcase/test_login.py -k "ao"
--html: Generate html The test report of
- Such as :addopts = -vs --html ./report/report.html
- By reading the pytest.ini Profile run
pytest.ini This file is pytest The core configuration file of the unit test framework .
1) Location : It is usually placed in the root directory of the project
2) Coding format : Must be ANSI, have access to notepad++ Modify encoding format
3) effect : change pytest The default behavior
4) Rules of operation : Whether it's running in the mode of the main function , Still run in command line mode , Will read this configuration file
[pytest]
addopts = -vs # Command line arguments , Separate... With spaces
testpaths = ./testcase # Path of test case
python_files = test_*.py # Rules for module names
python_classes = Test* # Rules for class names
python_functions = test # Rules for method names 6、 ... and 、pytest What is the order of executing test cases ?
unittest:ascii To determine the order of execution
pytest: Default from top to bottom
Change the default execution order : Use mark Mark ,@pytest.mark.run(order=1)
7、 ... and 、 How to execute in groups ( smoking , Execute in modules , Sub interface and web perform )
smoke: Smoke use case , Distributed in each module pytest -vs -m "smoke or usermanage"
pytest -m "smoke"
pytest -m "smoke or usermanage"
8、 ... and 、pytest Skip test cases
- Unconditional
@pytest.mark.skip(reason=' Skip use cases ')
- Conditional
@pytest.mark.skipif(age >= 18, reason=' Conditional skipping ')
边栏推荐
- 一个漂亮的API文档生成工具
- [microservices openfeign] two degradation methods of feign | fallback | fallbackfactory
- Brief explanation of depth first search (with basic questions)
- (pointer) write a function to compare the size of strings by yourself, which is similar to StrCmp.
- Exercises in quantum mechanics
- *. No main manifest attribute in jar
- Myslq delete followed by limit
- Leetcode skimming: binary tree 08 (maximum depth of n-ary tree)
- Redis:有序集合zset类型数据操作命令
- 96% of the collected traffic is prevented by bubble mart of cloud hosting
猜你喜欢

架构实战营 - 第 6 期 模块九之毕业设计

RHCSA 04 - 进程管理

leetcode刷题:二叉树06(对称二叉树)

Flink learning 6: programming model

ModStartBlog 现代化个人博客系统 v5.2.0 源码下载

Evolution of MySQL database architecture

Leetcode skimming: binary tree 08 (maximum depth of n-ary tree)

dried food! Generation of rare samples based on GaN

一个漂亮的API文档生成工具

laravel admin里百度编辑器自定义路径和文件名
随机推荐
Graduation project: design seckill e-commerce system
Unity 绘制弹球和台球的运动轨迹
一位毕业生的自我分享
Myslq delete followed by limit
【罗技】m720
Krypton saikr daily question - CTF
RHCSA 04 - 进程管理
2020 Bioinformatics | TransformerCPI
(指針)自己寫一個比較字符串大小的函數,功能與strcmp類似。
Flink learning 7: application structure
分布式系统:what、why、how
“找工作不要太在意工资”,这是我听过最大的谎言
Three years of graduation, half a year of distance | community essay solicitation
RHCSA 01 - 创建分区与文件系统
VIM mapping command
STM32 external DHT11 display temperature and humidity
The interactive solution of JS and app in the H5 page embedded in app (parameters can be transferred and callbacks can be made)
程序员远程办公喜忧参半| 社区征文
Interpretation of leveldb source code skiplist
Redis:哈希hash类型数据操作命令