导图社区 Pytest测试框架
pytest基础知识学习思维导图,笔记,总结。本人技术能力有限,其中可能有错误的地方,欢迎大家修改指出来。
编辑于2021-04-18 22:55:37Pytest
测试用例规则
模块:必须以 test_ 开头 或 _test结尾
类:必须以 test_ 开头 或 _test结尾
没有init构造方法
方法:必须以 test_ 开头 或 _test结尾
测试用例运行
主函数运行
运行所有用例::pytest.mian()
运行指定模块用例
pytest.main(['指定模块'])
运行指定目录用例
pytest.mian(['指定目录'])
nodeid指定运行用例,nodeid由目录模块,分隔符,类,函数方法组成
指定运行某个函数,pytest.mian([目录模块::test_func])
指定运行某个方法,pytest.mian([目录模块::test_class::test_func])
运行时指定参数
pytest.mian([参数]),例如pytest.main(['vs'])
命令行运行
运行所有用例::pytest
运行指定模块用例
pytest 指定模块
pytest 模块
运行指定目录用例
pytest 指定目录
pytest 目录
nodeid指定运行用例,nodeid由目录模块,分隔符,类,函数方法组成
指定运行某个函数,pytest 目录模块::test_func
指定运行某个方法,pytest 目录模块::test_class::test_func
运行时指定参数
pytest 参数,例如pytest -vs
常用参数详解
-s
控制台输出结果,当你的代码里面有 print 输出语句,如果想在运行结果中打印 print 输出的代码,在运行的时候可以添加 -s 参数,一般在调试的时候使用
-v
打印详细运行日志信息,一般在调试的时候加上这个参数,终端会打印出每条用例的详细日志信息,方便定位问题
-k
根据用例部分关键字,运行某个或者某些用例
pytest -k '类名' pytest -k '方法名' pytest -k "类名 and not 方法名" //运行类里所有的方法,不包含某个方法
-x
遇到用例失败立即停止运行,一旦发现有失败的用例即中止运行
-m
运行有 @pytest.mark.[标记名] 这个标记的测试用例
pytest -m [标记名]
-q
输出更简化的信息,与-v相反
-l
展示运行过程中的全局变量和局部变量
-n
多线程或分布式执行
pytest 用例 -n 2 #启用2个线程执行
-h | --help
查看所有的命令参数及介绍,包含了所有插件的
--maxfail
用例失败个数达到阀值停止运行
pytest --maxfail=[num]
--version
显示 pytest 的版本
--collect-only
此参数展示当前 pytest 能找到的用例,也就是说直接根据当前参数配置(结合其他参数),能找到的用例列表,用于检验运行的用例是否符合你的预期,这里只是列出会运行的用例,但是不会实际运行
--reruns
失败重跑
pytest 用例 -reruns 2 #重试2次
用例执行顺序
默认从上到下顺序执行,模块顺序、方法顺序
场景:部分用例之间有顺序依赖
使用:测试用例中加装饰器@pytest.mark.run()指定用例执行顺序
@pytest.mark.run(order=1) @pytest.mark.run(order=2) #按照设置的数字顺序执行用例
用例分组
场景:冒烟测试执行(用例分布在各模块) 业务模块执行 业务场景执行 分接口或UI执行
使用:测试用例中加装饰器@pytest.mark.[标记名] 标记名可以看成分组名称
执行:pytest -m [标记名] 执行分组用例
用例跳过
场景:只执行正向用例,忽略异常测试用例
无条件跳过
使用:测试用例中加装饰器@pytest.mark.skip(reason='') 可以使用reason参数加个原因名称
有条件跳过
使用:测试用例中加装饰器@pytest.mark.skipif(condition='',reason='') conditionti参数传入布尔表达式 reason参数传入原因名称
失败重跑
设置执行参数 --reruns 并指定失败重跑次数
pytest -v - -reruns 5 --reruns-delay 1 #每次等1秒 重试5次
参数化
测试用例中加装饰器@pytest.mark.parametrize()
参数argnames(参数名称)和argvalues(参数值,支持列表,元组,字典列表,字典元组,有多少个值就会执行多少次)
设置参数indirect=True时,argnames传入的是fixture方法名,并将argvalues参数传入fixture方法
通过fixture传递参数
fixture
作用:做测试前后的初始化设置,如测试数据准备,连接数据库,打开浏览器等 类似unittest中的setup和teardown 可以实现测试用例和测试用例之间是传递参数和数据
使用:定义函数方法,加装饰器@pytest.fixture()装饰
@pytest.fixture()有五个参数
scope
params
autouse
ids
name
调用fixture有三种方式
fixture名字作为用例的参数
@pytest.fixture() def fixture_func(self): return '调用了fixture_func' def test_func(self, fixture_func): s = fixture_func print(s)
这种方式可以传递参数和数据
使用@pytest.mark.usefixtures('fixture')装饰器
@pytest.fixture() def fixture_func(self): print('调用了fixture_func') @pytest.mark.usefixtures('fixture_func') def test_func(self): pass
这种方式不能似乎不能传递参数和数据
fixture是可以有返回值的,如果没return默认返回None
使用autouse参数
@pytest.fixture(autouse=True) def fixture_func(self): print('调用了fixture_func') def test_func(self): pass
似乎同样不能传递参数和数据
指定fixture的参数autouse=True这样每个测试用例会自动调用fixture,跟作用范围有关,默认是函数级别
作用范围 有四个级别 用参数scope设置
session 会话级别
module 模块级别
class 类级别
function 方法级别
conftest.py文件使用
可以跨.py文件调用 conftest.py与运行的用例要在同一个pakage下,并且有__init__.py文件
不需要import导入 conftest.py,pytest用例会自动识别该文件,放到项目的根目录下就可以全局目录调用了,如果放到某个package下,那就在改package内有效,可有多个conftest.py
conftest.py配置脚本名称是固定的,不能改名称
所有同目录测试文件运行前都会执行conftest.py文件
conftest文件实际应用需要结合fixture来使用,conftest中fixture的特性与上面介绍的同样适用
场景:接口测试所有用例需要用到登录返回的token 获取用例共用的数据和环境配置信息
子主题
fixture用yield实现teardown功能
@pytest.fixture() def fixtureFunc(): '''实现浏览器的打开和关闭''' driver = webdriver.Firefox() yield driver driver.quit() def test_search(fixtureFunc): '''访问百度首页,搜索pytest字符串是否在页面源码中''' driver = fixtureFunc driver.get('http://www.baidu.com') driver.find_element_by_id('kw').send_keys('pytest') driver.find_element_by_id('su').click() time.sleep(3) source = driver.page_source assert 'pytest' in source
多线程 分布式
多线程并行运行 加-n参数 pytest -n 2 表示启用2个线程执行
前提: 用例之间都是独立的,没有先后顺序,随机都能执行,可重复运行不 影响其他用例
pytest-xdist
其他进阶用法
插件
钩子
pytest相关的知识点、场景后续使用到了再补充...
测试报告
pytest-html
安装:pip install pytest-html
生成报告:pytest --html=report.html # 在当前目录生成名为report.html的测试报告
allure
安装:pip install allure-pytest
生成报告 --alluredir=存放路径
生成的xml格式测试报告 下载allure工具转换成html
官网下载allure工具 配置环境变量
生成静态测试报告 allure generate -o 路径 执行该命令后将自动打开浏览器浏览测试报告
常用命令
查看帮助文档:allure help
打开测试报告:allure open
生成静态测试报告:allure generate -o参数指定生成路径 -c参数清除之前的数据(如果放了Environment配置,也会被清除)
生成临时报告并打开:allure serve
Environment配置
通过创建 environment.properties 或者 environment.xml 文件来配置环境信息,并把文件存放到 allure-results
环境变量配置可以添加报告相关的配置参数,如运行的系统环境,版本号,测试环境,测试人员等基本信息
environment.properties实例
baseUrl=http://127.0.0.1:8080 projectName=test author=shuiyin email=1870606217@qq.com
Categories
测试报告默认统计两种类型的测试用例结果,失败的用例和故障测试用例,我们可以自定义添加用例的统计类型,同样需要在allure-results目录下新建categories.json文件
[ { "name": "Ignored tests", "matchedStatuses": ["skipped"] }, { "name": "Infrastructure problems", "matchedStatuses": ["broken", "failed"], "messageRegex": ".*bye-bye.*" }, { "name": "Outdated tests", "matchedStatuses": ["broken"], "traceRegex": ".*FileNotFoundException.*" }, { "name": "Product defects", "matchedStatuses": ["failed"] }, { "name": "Test defects", "matchedStatuses": ["broken"] } ]
allure定制
@allure.step
使用@allure.step修饰某些测试用例中需要的函数,使测试用例在allure报告中能够更加详细的显示测试过程
可以添加分步步骤,在代码块中添加allure.step()
with allure.step(''): pass # 代码块
@allure.attach
使用@allure.attach可以给报告中添加文件,图片,log,html代码等
@allure.description
在报告中展示测试用例的描述信息
@allure.description_html(html代码)添加描述信息
详解 allure.attach(body, name=None, attachment_type=None, extension=None)
body 数据 name 附件名称 attachment_type 附件类型 extension 后缀名
@allure.title
使用allure.title(title)可以重命名测试用例在allure报告中的名称
@allure.link @allure.testcase @allure.issue
这三种特性都可以给测试用例添加一个链接
@allure.testcase 一般用于添加测试用例地址
@allure.issue 一般用于添加bug地址
@allure.feature
@allure.story
@allure.severity
标识测试用例或者测试类的级别,分为blocker,critical,normal,minor,trivial5个级别
其他
label
suite
parent_suite
sub_suite
tag
id
epic
dynamic
severity_label
attachment_type
allure提供部分装饰器除了可以添加到类、方法上,还可以直接当方法调用
如果有需要,可以到allure安装目录修改页面样式,例如修改allure的logo
配置文件
pytest核心配置文件:pytest.ini 默认放置在项目根目录
作用:通过自定义配置改变pytest默认行为 命令行或者函数运行时会读取配置文件
常用配置
addopts
添加pytest运行时默认参数
testpaths
指定扫描的测试用例文件
norecursedirs
忽略扫描的文件
python_files
自定义用例模块的命名规则
python_classes
自定义用例类的命名规则
python_functions
自定义测试方法的命名规则
markers
定义标记名
实例
[pytest] addopts = -vs testpaths = ./case norecursedirs = web python_files = api*.py web*.py app*.py win*.py python_classes = Test* python_functions = test_* markers = smoke module business api web
简介
成熟的单元测试框架,比unittest更加灵活,更简单
可以实现用例跳过,失败重试
可以集成第三方测试报告,如Allure
可以跟Jenkins持续集成
有许多非常强大的插件,可以实现常用操作
pytest-html
生成html格式测试报告
pytest-xdist
用例分布式执行,多线程
pytest-ordering
改变测试用例执行顺序
pytest-rerunfailures
失败用例重跑
allure-pytest
集成allure测试报告
子主题
...