![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
pytest
waitan2018
这个作者很懒,什么都没留下…
展开
-
【pytest】pytest-cov :统计代码测试覆盖率
pytest-cov :coverage.py是 Python推荐使用的覆盖率统计工具,pytest-cov是pytest的插件,它可以让你在pytest中使用cpverage.py;使用pytest-cov:下载pytest-cov :pip install pytest-cov; 进入项目路径使用 --cov参数:pytest --cov=src;因为...原创 2020-02-19 21:21:29 · 6347 阅读 · 2 评论 -
【pytest】pdb 调试
pdb 调试:pdb是 Python标准库的调试模块。在pytest中,可以直接使用 --pdb参数在测试失败时开启调试;直接使用 --pdb参数:import pytestclass TestDemoA: @pytest.mark.parametrize('data', [1, 2, 3, 4]) def test_A_001(self, d...原创 2020-02-19 20:59:02 · 2691 阅读 · 0 评论 -
【pytest】配置文件-pytest.ini
pytest.ini:pytest.ini 可以修改pytest的默认行为注意:pytest.ini不能使用任何中文符号,包括汉字、空格、引号、冒号等等;更改默认命令行参数:将常用的命令行参数设置为默认,省去重复输入的工作;# pytest.ini[pytest]addopts = -rsxX -l -strict --tb=short注册 mark...原创 2020-02-19 17:16:04 · 12615 阅读 · 0 评论 -
【pytest】安装、编写用例、运行用例
安装pytest:命令行:pip install pytestPycharm:File | Settings | Project:xxx| Project Interpreter,点击 +号,搜索 pyteset,点击左下角install package编写测试用例:测试用例编写规则:1.测试用例文件名(.py文件)必须以“test_” 开头或者以“test”结尾...原创 2020-01-17 17:16:00 · 308 阅读 · 0 评论 -
【pytest】Hook 方法之 pytest_runtest_makereport:获取测试用例执行结果
Hook 方法之 pytest_runtest_makereport:@hookspec(firstresult=True)def pytest_runtest_makereport(item, call): """ return a :py:class:`_pytest.runner.TestReport` object for the given :py:class:`...原创 2020-02-16 21:30:40 · 6638 阅读 · 5 评论 -
【pytest】Hook 方法之 pytest_runtest_protocol:获取将要执行的用例(item)及下一个测试用例(nextitem)
Hook 方法之 pytest_runtest_protocol:pytest_runtest_protocol :官方给的解释是:为给定的测试项目执行runtest_setup / call / teardown协议;@hookspec(firstresult=True)def pytest_runtest_protocol(item, nextitem): """.....原创 2020-02-16 19:25:38 · 3289 阅读 · 0 评论 -
【pytest】Hook 方法之 pytest_collection_modifyitems:修改测试用例执行顺序
Hook 方法之 pytest_collection_modifyitems:pytest_collection_modifyitems是在用例收集完毕之后被调用,可以用来调整测试用例执行顺序;它有三个参数,分别是:session:会话对象; config:配置对象; items:用例对象列表;这三个参数分别有不同的作用,都可以拿来单独使用,修改用例执行顺序主要是使用item...原创 2020-02-15 22:33:25 · 3945 阅读 · 0 评论 -
【pytest】Hook 方法之 pytest_addoption :注册命令行参数
Hook 方法之 pytest_addoption:pytest_addoption可以让用户注册一个自定义的命令行参数,方便用户将数据传递给pytest;这个 Hook方法一般和内置fixturepytestconfig配合使用,pytest_addoption注册命令行参数,pytestconfig通过配置对象读取参数的值;# conftest.py ...原创 2020-02-15 00:12:23 · 10661 阅读 · 2 评论 -
【pytest】内置 fixtures 之 tmpdir:创建临时文件
内置fixtures之 tmpdir:tmpdir的作用是:在本地生成临时文件夹,并返回文件对象;代码演示:先写个测试用例,调用 tmpdir,执行一下看看:class TestDemoA: def test_A_001(self, tmpdir): time.sleep(2) print('\n',tmpdir)if...原创 2020-02-13 22:14:17 · 1533 阅读 · 0 评论 -
【pytest】内置 fixtures 之 cache:使用 cache 写入、读取 pytest 缓存
内置fixtures之cache:cache是一个可以在测试会话之间保持状态的缓存对象;源码展示:@pytest.fixturedef cache(request): """ Return a cache object that can persist state between testing sessions. cache.get(key...原创 2020-02-10 21:16:20 · 1870 阅读 · 0 评论 -
【pytest】内置 fixtures 之 pytestconfig:使用 pytestconfig 获取命令行参数 和 pytest.ini 配置文件的值
内置fixtures:前面说过我们可以通过自定义fixture来实现测试用例的前置和后置工作,其实pytest中也有一些内置的fixture可供我们调用;内置fixture的作用就是为了使pytest更加强大,它可以修改控制pytest的行为,以及为我们提供一些更加便利的功能。pytestconfig:pytestconfig 可以通过命令行参数、选项、配...原创 2020-02-09 16:17:23 · 7238 阅读 · 5 评论 -
【pytest】如何生成 html 和 allure 测试报告
生成html测试报告:安装插件:pip install pytest-html; 使用参数:pytest -m xxx --html=report_path.html;生成allure测试报告:安装插件:pip install allure-pytest; 下载并解压allure2,将 allure2/bin 目录配置到环境变量; --alluredir参数生成all...原创 2020-02-08 19:21:20 · 1060 阅读 · 0 评论 -
【pytest】常见的 pytest 参数及插件
pytest的常见参数:-s :显示标准输出,例如print()的语句; -v :显示详细报告; -q :显示简洁报告; -x :用例失败时立即停止测试; -c file :从 file 加载配置文件; -l (--showlocals) :用例失败信息回溯时显示局部变量及其值; --maxfail=n :失败n后停止运行测试; --lf (--last-failed) :...原创 2020-02-08 19:10:24 · 1365 阅读 · 0 评论 -
【pytest】skip、skipif 跳过测试用例,xfail 处理预计会失败的测试用例
如何跳过测试用例:skip直接跳过测试用例:使用方法:@pytest.mark.skip(reason='这个是跳过的原因!') 装饰要跳过的测试方法或测试类上;装饰在测试方法只跳过该方法对应的测试用例; 装饰在使用了 parametrize 参数化的测试方法上,跳过生成的所有测试用例; 装饰在测试类上,跳过测试类中所有的测试用例; reason参数是跳过用例的原因,可以不写...原创 2020-02-03 23:25:59 · 1732 阅读 · 0 评论 -
【pytest】paramtrize 参数化、数据驱动
paramtrize参数化:pytest中可以使用@pytest.mak.paramtrize(argsnames, argsvalues, ids=None)来实现参数化;主要参数说明:argsnames :参数名,是个字符串,如中间用逗号分隔则表示为多个参数名; argsvalues :参数值,参数组成的列表,列表中有几个元素,就会生成几条测试用例;使用方法:使用 ...原创 2020-01-21 16:47:11 · 2301 阅读 · 1 评论 -
【pytest】fixture 间的互调,fixture 的作用域、执行顺序
fixtures 之间互调:pytest中fixtures之间也可以互相调用:# conftest.pyimport pytest@pytest.fixture()def fix_init_1(): print(" \nfix_init_1...")@pytest.fixture()def fix_init_2(fix_init_1): prin...原创 2020-01-20 15:40:43 · 3617 阅读 · 0 评论 -
【pytest】编写、共享及使用 fixture(测试夹具)
什么是fixture?fixture是pytest的精髓,它是测试脚手架,有两个部分组成:用例前置:用例前置的作用就是准备测试用例的前置条件和测试数据,相当于unittest中的 setup()的功能; 用例后置:用例后置的作用就是在测试用例执行完毕之后进行前置条件和测试数据的清理、复原等工作,总之就是你想在测试用例执行完之后干的事都在这里干就行了,没有想干的事情也可以空着...原创 2020-01-18 21:59:14 · 1818 阅读 · 0 评论 -
【pytest】assert 断言
使用assert断言:assert断言是判断测试执行结果是否符合预期的重要方法!pytest中的assert要比unittest中的assertEqual等断言方法灵活很多,它支持各种条件表达式,只要表达式成立(条件最终为True),便是断言通过,如果表达式失败(条件最终为False),便是断言失败。assert支持的表达式:# test_moduleNam...原创 2020-01-18 20:12:49 · 2477 阅读 · 0 评论 -
【pytest】使用 mark 标记及运行测试用例,注册、管理 mark 标记
使用 @pytest.mark.xxx 标记测试用例:可以标记测试方法、测试类,标记名可以自定义,最好起有意义的名字; 同一测试类/方法可同时拥有多个标记;# test_login_logout.pyimport pytest@pytest.mark.loginclass TestLogin: """登陆功能测试类""" @pytest.mark.smoke...原创 2020-01-18 19:13:56 · 14335 阅读 · 1 评论