pytest-根据类或函数名选择用例

您可以使用-k命令行选项来指定一个表达式,该表达式实现了测试名称上的子字符串匹配,而不是-m提供的标记的精确匹配。这使得基于他们的名字选择测试变得很容易:

Using -k expr to select tests based on their name

You can use the -k command line option to specify an expression which implements a substring match on the test names instead of the exact match on markers that -m provides. This makes it easy to select tests based on their names:

$ pytest -v -k http  # running with the above defined example module
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5
cachedir: .pytest_cache
rootdir: $REGENDOC_TMPDIR, inifile:
collecting ... collected 4 items / 3 deselected

test_server.py::test_send_http PASSED                                [100%]

================== 1 passed, 3 deselected in 0.12 seconds ==================

And you can also run all tests except the ones that match the keyword:

$ pytest -k "not send_http" -v
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5
cachedir: .pytest_cache
rootdir: $REGENDOC_TMPDIR, inifile:
collecting ... collected 4 items / 1 deselected

test_server.py::test_something_quick PASSED                          [ 33%]
test_server.py::test_another PASSED                                  [ 66%]
test_server.py::TestClass::test_method PASSED                        [100%]

================== 3 passed, 1 deselected in 0.12 seconds ==================

Or to select “http” and “quick” tests:

$ pytest -k "http or quick" -v
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5
cachedir: .pytest_cache
rootdir: $REGENDOC_TMPDIR, inifile:
collecting ... collected 4 items / 2 deselected

test_server.py::test_send_http PASSED                                [ 50%]
test_server.py::test_something_quick PASSED                          [100%]

================== 2 passed, 2 deselected in 0.12 seconds ==================




### 在 pytest-html 报告中增加测试用的作者字段 为了在 `pytest-html` 生成的 HTML 测试报告中添加自定义信息,比如测试用的作者字段,可以利用 `pytest_html_results_table_header` 和 `pytest_html_results_table_row` 这两个钩子函数。通过这两个钩子函数可以在表格头部以及每一行的结果记录里加入额外列。 #### 定义自定义属性并修改报告头 当编写测试案时,在每个测试函数上附加元数据作为参数传递给装饰器 `@pytest.mark.parametrize()` 者直接使用 `request.node.add_marker()` 方法动态添加标记。对于静态声明的情况如下所示: ```python import pytest @pytest.mark.author("张三") def test_sample(): assert True ``` 接着需要配置环境以便这些信息能够显示在最终生成的HTML文档内。这通常是在项目的根目录下的 `conftest.py` 文件完成操作。以下是具体实现方式: ```python # conftest.py def pytest_html_results_table_header(cells): cells.insert(2, "<th>Author</th>") # 向表头插入新列 'Author' def pytest_html_results_table_row(report, cells): try: author = report.user_properties.get('author', '') cells.insert(2, f"<td>{author}</td>") except AttributeError: pass @pytest.hookimpl(hookwrapper=True) def pytest_runtest_makereport(item, call): outcome = yield report = outcome.get_result() marker = item.get_closest_marker(name="author") if marker is not None: report.user_properties['author'] = marker.args[0] ``` 上述代码片段实现了向默认结果表单中新增一列表示负责人的名字,并且从测试项获取到对应的标注信息填充进去[^1]。 这样做的好处在于不仅限于展示开发者姓名,还可以扩展至其他任何有意义的数据点,只要遵循相同模式即可轻松集成更多维度的信息进入可视化报表之中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值