当前位置:网站首页>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-pytest
After the installation , see pytest Version information
(venv) guoliangs-MacBook-Pro-15-inch:pytest guoliang$ pytest --version
pytest 7.1.2
Four 、 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 ')
边栏推荐
猜你喜欢
Unity 绘制弹球和台球的运动轨迹
The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO
idea修改主体颜色
Flink学习7:应用程序结构
Leetcode skimming: binary tree 07 (maximum depth of binary tree)
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
R语言中如何查看已安装的R包
leetcode刷题:二叉树04(二叉树的层序遍历)
TCP-三次握手和四次挥手简单理解
I was tortured by my colleague's null pointer for a long time, and finally learned how to deal with null pointer
随机推荐
Keysight N9320B射频频谱分析仪解决轮胎压力监测方案
leetcode刷题:二叉树04(二叉树的层序遍历)
01 QEMU starts the compiled image vfs: unable to mount root FS on unknown block (0,0)
vim映射命令
Distributed system: what, why, how
C语言单向链表练习
[microservice openfeign] @feignclient detailed explanation
Confession code collection, who says program apes don't understand romance
指针数组和数组指针
微信脑力比拼答题小程序_支持流量主带最新题库文件
Interpretation of leveldb source code skiplist
“找工作不要太在意工资”,这是我听过最大的谎言
The difference between bagging and boosting in machine learning
10 reasons for not choosing to use free virtual hosts
领导:谁再用redis过期监听实现关闭订单,立马滚蛋!
【微服务|openfeign】feign的两种降级方式|Fallback|FallbackFactory
如何远程办公更有效率 | 社区征文
软件测试是干什么的 发现缺陷错误,提高软件的质量
Leetcode brush questions: binary tree 05 (flip binary tree)
Katalon uses script to query list size