当前位置:网站首页>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='有条件的跳过')
边栏推荐
- PostgreSQL users cannot create table configurations by themselves
- Leetcode skimming: binary tree 09 (minimum depth of binary tree)
- (指針)自己寫一個比較字符串大小的函數,功能與strcmp類似。
- Small record of thinking
- [csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability
- The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO
- Redis cluster uses Lua script. Lua script can also be used for different slots
- STM32外接DHT11显示温湿度
- leetcode刷题:二叉树05(翻转二叉树)
- 01 qemu 启动编译好的镜像 VFS: Unable to mount root fs on unknown-block(0,0)
猜你喜欢

Understand the principle of bytecode enhancement technology through the jvm-sandbox source code

Confession code collection, who says program apes don't understand romance

Illustrated network: what is the hot backup router protocol HSRP?

RHCSA 04 - 进程管理

量子力学习题

leetcode刷题:二叉树07(二叉树的最大深度)

idea修改主体颜色

Leetcode skimming: binary tree 04 (sequence traversal of binary tree)

02 ls 命令的具体实现

Idea configuration 360zip open by default -- external tools
随机推荐
Interpretation of leveldb source code skiplist
资深开发人员告诉你,怎样编写出优秀的代码?
批处理初识
Graduation project: design seckill e-commerce system
Activiti7 task service - process variables (setvariable and setvariablelocal)
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO
量子力学习题
深度优先搜索简要讲解(附带基础题)
User defined path and file name of Baidu editor in laravel admin
线程常用的方法
Leetcode skimming: binary tree 04 (sequence traversal of binary tree)
What does software testing do? Find defects and improve the quality of software
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
【CSRF-01】跨站请求伪造漏洞基础原理及攻防
RHCSA 03 - 文件的基础权限
指针数组和数组指针
(pointeur) Écrivez - vous une fonction qui compare la taille de la chaîne et fonctionne comme strcmp.
JDBC advanced
Getting started with the go language is simple: go implements the Caesar password