当前位置:网站首页>Pytest基础自学系列(一)
Pytest基础自学系列(一)
2022-07-04 03:52:00 【COCOgsta】
视频来源:B站《2022最新pytest接口自动化测试框架,三天带你精通pytest,带你写出最好的代码!》
一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!
一、pytest单元测试框架
(1)什么是单元测试框架
单元测试是指在软件开发当中,针对软件的最小单位(函数,方法)进行正确性的检查测试。
(2)单元测试框架
java:junit和testing
python:unittest和pytest
(3)单元测试框架主要做什么?
- 测试发现:从多个文件里面去找到我们测试用例
- 测试执行:按照一定的顺序和规则去执行,并生成结果
- 测试判断:通过断言判断预期结果和实际结果的差异
- 测试报告:统计测试进度,耗时,通过率,生成测试报告。
二、单元测试框架和自动化测试框架有什么关系?
(1)什么是自动化测试框架
为了完成指定系统的自动化测试,而封装的一整套完善的代码框架。主要封装自动化基础模块,自动化管理模块,自动化测试统计模块。
(2)作用
- 提高测试效率,降低维护成本
- 减少人工干预,提高测试的准确性,增加代码的重用性
- 核心思想是让不懂代码的人也能够通过这个框架去实现自动化测试
(3)pytest单元测试框架和自动化测试框架的关系
单元测试框架:只是自动化测试框架中的组成部分之一。
pom设计模式
数据驱动
关键字驱动
全局配置文件的封装
日志监控
selenium,requests二次封装
断言
报告邮件
更多。。。
三、pytest简介
- pytest是一个非常成熟的python的单元框架,比unittest更灵活,容易上手
- pytest可以和selenium,requests,appium结合实现web自动化,接口自动化,app自动化
- pytest可以实现测试用例的跳过以及reruns失败用例重试
- pytest可以和allure生成非常美观的测试报告
- pytest可以和Jenkins持续集成
- pytest有很多非常强大的插件,并且这些插件能够实现很多的实用的操作pytestpytest-xdist 测试用例分布式执行。多CPU分发。pytest-ordering 用于改变测试用例的执行顺序pytest-rerunfailure 用例失败后重跑pytest-html(生成html格式的自动化测试报告)allure-pytest 用于生成美观的测试报告
pytest安装方法:
将安装包名放到requirements.txt中,通过pip install -r requirements.txt 安装全部内容
pytest
pytest-html
pytest-xdist
pytest-ordering
pytest-rerunfailures
allure-pytest安装完后,查看pytest版本信息
(venv) guoliangs-MacBook-Pro-15-inch:pytest guoliang$ pytest --version
pytest 7.1.2四、使用pytest,默认的测试用例的规则以及基础应用
- 模块名必须以test_开头或者_test结尾
- 测试类必须以Test开头,并且不能有init方法
- 测试方法必须以test开头
五、pytest测试用例的运行方式
- 主函数模式
(1)运行所有:pytest.main()
import pytest
class TestLogin:
def test_01_baili(self):
print("测试百里")
if __name__ == '__main__':
pytest.main()执行结果:
"/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 下午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%]测试百里
============================== 1 passed in 0.01s ===============================
Process finished with exit code 0
(2)指定模块:pytest.main(['-vs', 'test_login.py'])
(3)指定目录:pytest.main(['-vs', './interface_testcase'])
(4)通过nodeid指定用例运行:nodeid由模块名,分隔符,类名,方法名,函数名组成。
pytest.main(['-vs', './interface_testcase/test_interface.py::test_04_func'])
pytest.main(['-vs', './interface_testcase/test_interface.py::TestInterface::test_01_zhiliao'])
- 命令行模式
(1)运行所有: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)指定模块:pytest -vs ./testcase/test_login.py
(3)指定目录:pytest -vs ./interface_testcase
(4)通过nodeid指定用例运行:
pytest -vs ./interface_testcase/test_interface.py::test_04_func
pytest -vs ./interface_testcase/test_interface.py::TestInterface::test_01_zhiliao
参数详解:
-s:表示输出调试新新,包括print打印的信息
-v:显示更详细的信息
-vs:两个参数一起用
-n:支持多线程或者分布式运行测试用例
- 如:pytest.main(['-vs', './testcase', '-n=4'])、pytest -vs ./testcase/test_login.py -n 2
--reruns:失败用例重跑
- 如:pytest.main(['-vs', './testcase', '--reruns=2'])、pytest -vs ./testcase/test_login.py --reruns 2
-x:表示只要有一个用例报错,那么测试停止
- 如:pytest.main(['-vs', './testcase', '-x'])、pytest -vs ./testcase/test_login.py -x
--maxfail:出现指定用例数失败就停止
- 如:pytest.main(['-vs', './testcase', '--maxfail=2'])、pytest -vs ./testcase/test_login.py --maxfail 2
-k:根据测试用例的部分字符串指定测试用例。
- 如:pytest.main(['-vs', './testcase', '-k=ao'])、pytest -vs ./testcase/test_login.py -k "ao"
--html:生成html的测试报告
- 如:addopts = -vs --html ./report/report.html
- 通过读取pytest.ini配置文件运行
pytest.ini这个文件它是pytest单元测试框架的核心配置文件。
1)位置:一般放在项目的根目录
2)编码格式:必须是ANSI,可以使用notepad++修改编码格式
3)作用:改变pytest默认的行为
4)运行的规则:不管是主函数的模式运行,还是命令行模式运行,都会去读取这个配置文件
[pytest]
addopts = -vs # 命令行的参数,用空格分隔
testpaths = ./testcase # 测试用例的路径
python_files = test_*.py # 模块名的规则
python_classes = Test* # 类名的规则
python_functions = test # 方法名的规则六、pytest执行测试用例的顺序是怎样的?
unittest:ascii的大小来决定执行的顺序
pytest:默认从上到下
改变默认的执行顺序:使用mark标记,@pytest.mark.run(order=1)
七、如何分组执行(冒烟,分模块执行,分接口和web执行)
smoke:冒烟用例,分布在各个模块里面pytest -vs -m "smoke or usermanage"
pytest -m "smoke"
pytest -m "smoke or usermanage"
八、pytest跳过测试用例
- 无条件
@pytest.mark.skip(reason='跳过用例')
- 有条件
@pytest.mark.skipif(age >= 18, reason='有条件的跳过')
边栏推荐
- Keysight N9320B射频频谱分析仪解决轮胎压力监测方案
- Three years of graduation, half a year of distance | community essay solicitation
- TCP-三次握手和四次挥手简单理解
- [book club issue 13] multimedia processing tool ffmpeg tool set
- ROS2中CMake编译选项的设置
- [microservices openfeign] two degradation methods of feign | fallback | fallbackfactory
- The difference between bagging and boosting in machine learning
- There is a problem that the package cannot be parsed in the like project
- Katalon中控件的参数化
- Leetcode brush question: binary tree 06 (symmetric binary tree)
猜你喜欢
Tcp- simple understanding of three handshakes and four waves

Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure

The maximum expiration time of client secret in azure ad application registration is modified to 2 years

idea修改主体颜色

ctf-pikachu-XSS

LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)

Distributed system: what, why, how
![[Logitech] m720](/img/bb/44144a1c3907808398c05b3b36962c.png)
[Logitech] m720

深度优先搜索简要讲解(附带基础题)

Flink学习7:应用程序结构
随机推荐
02 specific implementation of LS command
Why is the probability of pod increasing after IPtable
Small record of thinking
资深开发人员告诉你,怎样编写出优秀的代码?
Redis cluster uses Lua script. Lua script can also be used for different slots
Global exposure and roller shutter exposure of industrial cameras
[book club issue 13] packaging format of video files
User defined path and file name of Baidu editor in laravel admin
Getting started with the go language is simple: go implements the Caesar password
[microservices openfeign] two degradation methods of feign | fallback | fallbackfactory
JDBC advanced
Leetcode brush question: binary tree 06 (symmetric binary tree)
ctf-pikachu-XSS
量子力学习题
(指针)编写函数void fun(int x,int *pp,int *n)
Huawei cloud Kunpeng engineer training (Guangxi University)
Pointer array and array pointer
The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO
How to add custom API objects in kubernetes (1)
I was tortured by my colleague's null pointer for a long time, and finally learned how to deal with null pointer