随笔分类 -  pytest

摘要:初始化钩子 调用插件和conftest.py文件的初始化挂钩,添加命令行选项 import pytest def pytest_addoption(parser:pytest.Parser): parser.addoption("--xxxx",default="xxxx",name="weburl 阅读全文
posted @ 2022-11-18 16:34 zhq9 阅读(26) 评论(0) 推荐(0)
摘要:介绍 调用插件和conftest.py文件的初始化挂钩。 添加命令行选项 import pytest def pytest_addoption(parser:pytest.Parser): parser.addoption("--xxxx",default="xxxx",help="url") @p 阅读全文
posted @ 2022-11-16 17:47 zhq9 阅读(24) 评论(0) 推荐(0)
摘要:介绍 config.cache是一个实例pytest.Cache:该config.cache对象允许其他插件和装置在测试运行中存储和检索值。要从 fixtures 请求访问它pytestconfig到您的 fixture 并使用pytestconfig.cache. 函数 mkdir返回给定的nam 阅读全文
posted @ 2022-11-15 10:39 zhq9 阅读(159) 评论(0) 推荐(0)
摘要:介绍 pytester是pytest框架的内置fixture,想使用pytester要在conftest.py或者是用例文件的最上面添加pytest_plugins = "pytester" 默认临时目录可以通过tmp_path fixture查看,也可以使用--basetemp=mydir定义临时 阅读全文
posted @ 2022-11-15 10:09 zhq9 阅读(60) 评论(0) 推荐(0)
摘要:tmp_path 介绍 tmp_path夹具返回一个测试调用的唯一临时文件目录,tmp_path是一个pathlib.Path对象。临时目录可以使用--basetemp=mydir指定 示例 @pytest.fixture() def get_weburl(tmp_path): print("zhq 阅读全文
posted @ 2022-11-14 14:59 zhq9 阅读(64) 评论(0) 推荐(0)
摘要:介绍 在测试当中,fixture为测试用例提供服务,比如配置数据库、获取配置信息、环境准备清理等,对于一些复杂的测试用例编写带来了很大的帮助 fixture一般是定义到conftest.py文件当中,他的作用域是使用fixture的用例从里往外找,比如项目根下一个conftest文件,用例同级目录下 阅读全文
posted @ 2022-11-13 07:24 zhq9 阅读(90) 评论(0) 推荐(0)
摘要:介绍 自定义标记首先要在pytest.ini 文件当中注册也可以通过fixture注册,不注册的话不影响使用但是执行的时候就会有警告 注册标记 # pytest.ini [pytest] markers = p0: 这是一个p0级别的标签 p1: 这是一个p1级别的标签 使用标签 import py 阅读全文
posted @ 2022-11-13 05:50 zhq9 阅读(150) 评论(0) 推荐(0)
摘要:介绍 xfail 是用来标记那些预期失败的用例,它是一个内部标记 将用例标记失败 不同的标记方法使用示例 @pytest.mark.xfail def test_01(): pass @pytest.mark.xfail(reason="功能还未提测") def test_01(): pass de 阅读全文
posted @ 2022-11-12 11:57 zhq9 阅读(37) 评论(0) 推荐(0)
摘要:安装 在命令行中运行以下命令: pip install -U pytest 检查您是否安装了正确的版本: pytest --version 常用的执行命令 -q简短输出,在执行用例的时候加上-q会简洁输出 pytest -q xxx.py -k是根据表达式匹配用例并执行用例,表达式不区分大小写,可以 阅读全文
posted @ 2022-11-10 09:32 zhq9 阅读(82) 评论(0) 推荐(0)
摘要:安装 mac 安装allure brew install alluer 安装插件 allure-pytest pip3 install allure-pytest 生成报告 第一种方式 pytest test_01.py --alluredir=./report --clean-alluredir 阅读全文
posted @ 2022-10-06 16:35 zhq9 阅读(32) 评论(0) 推荐(0)
摘要:# conftest.py driver = None @pytest.fixture() def driver(): global driver driver = webdriver.Chrome() driver.get("http://www.baidu.com") yield driver 阅读全文
posted @ 2022-10-06 07:47 zhq9 阅读(77) 评论(0) 推荐(0)
摘要:当用例失败的时候重新执行插件 要求 要求python大于3.6小于3.10 安装 pip install pytest-rerunfailures 重新运行所有失败用例 要重新运行所有测试失败,请使用--reruns命令行选项以及您希望测试运行的最大次数: pytest --reruns 5 # 失 阅读全文
posted @ 2022-07-08 18:20 zhq9 阅读(41) 评论(0) 推荐(0)
摘要:安装 pip install pytest-ordering 使用 import pytest @pytest.mark.run(order=2) def test_foo(): assert True @pytest.mark.run(order=1) def test_bar(): assert 阅读全文
posted @ 2022-07-01 15:47 zhq9 阅读(15) 评论(0) 推荐(0)
摘要:初始化钩子 ###pytest_addoption 调用插件和conftest.py文件的初始化挂钩,添加命令行选项 import pytest def pytest_addoption(parser:pytest.Parser): parser.addoption("--xxxx",default 阅读全文
posted @ 2022-07-01 09:56 zhq9 阅读(64) 评论(0) 推荐(0)
摘要:pytest.ini官网配置选项 注册标记标签 请注意,:标记名称之后的所有内容都是可选描述。 [pytest] markers = slow: marks tests as slow (deselect with '-m "not slow"') serial addopts 强制校验标签 没有注 阅读全文
posted @ 2022-06-29 18:03 zhq9 阅读(181) 评论(0) 推荐(0)
摘要:介绍 pytest.mark.skipif可以根据条件判断是否执行该用例, 第一个参数传入条件 可以传入True/False如果条件应该被跳过或条件字符串 第二个参数是说明/原因 reason="2大于1" 有条件的跳过用例 import pytest @pytest.mark.skipif(1<2 阅读全文
posted @ 2022-06-29 16:21 zhq9 阅读(269) 评论(0) 推荐(0)
摘要:介绍 您可以标记无法在某些平台上运行或您预计会失败的测试功能,以便 pytest 可以相应地处理它们并提供测试会话的摘要,同时保持测试套件绿色。 跳过意味着您希望测试仅在满足某些条件时才能通过,否则 pytest 应该完全跳过运行测试。常见的例子是跳过非 Windows 平台上的纯 Windows 阅读全文
posted @ 2022-06-29 15:22 zhq9 阅读(101) 评论(0) 推荐(0)
摘要:介绍 parametrize装饰器可以对函数进行参数化,像用例相同场景不同数据的时候就可以使用它 函数参数化 import pytest @pytest.mark.parametrize("nub1,nub2,nub3",[[1,2,3],[2,2,4],[3,2,5]]) def test_01( 阅读全文
posted @ 2022-06-21 09:54 zhq9 阅读(46) 评论(0) 推荐(0)
摘要:读取并返回到目前为止捕获的输出,重置内部缓冲区。 返回 将捕获的内容作为具有out和err 字符串属性的命名元组。 def test_output(capsys): print("hello") captured = capsys.readouterr() assert captured.out = 阅读全文
posted @ 2022-06-17 14:03 zhq9 阅读(39) 评论(0) 推荐(0)
摘要:cache fixture 也可以根据 request fixture关联出来 例如:request.config.cache.makedir 获取给定的name缓存路径,没有该目录就会创建, @pytest.fixture def mydata(cache): val = cache.makedi 阅读全文
posted @ 2022-06-16 18:34 zhq9 阅读(36) 评论(0) 推荐(0)