跳转到主要内容

使用正则表达式选择pytest测试

项目描述

pytest-regex

使用Python标准库正则表达式指定要运行的测试

在本地安装后(例如,使用 python -m pip install .),您可以将其与更传统的测试选择技术进行比较,如下所示。

考虑与SciPy开发合作。让我们先选择所有节点ID中包含test_3d的所有测试,使用传统的/内置的-k标志

python dev.py test – -v -k “test_3d”

这将运行您期望的具有字符串匹配的一组测试

scipy/io/tests/test_idl.py::TestArrayDimensions::test_3d 通过 scipy/io/tests/test_idl.py::TestPointerArray::test_3d 通过 scipy/optimize/_trustregion_constr/tests/test_qp_subproblem.py::TestBoxBoundariesIntersections::test_3d_box_constraints 通过 scipy/optimize/_trustregion_constr/tests/test_qp_subproblem.py::TestBoxBoundariesIntersections::test_3d_box_constraints_entire_line 通过 scipy/optimize/_trustregion_constr/tests/test_qp_subproblem.py::TestModifiedDogleg::test_3d_example 通过 scipy/stats/tests/test_stats.py::TestGeometricStandardDeviation::test_3d_array 通过 scipy/stats/tests/test_stats.py::TestGeometricStandardDeviation::test_3d_array_axis_type_tuple 通过 scipy/stats/tests/test_stats.py::TestGeometricStandardDeviation::test_3d_array_axis_0 通过 scipy/stats/tests/test_stats.py::TestGeometricStandardDeviation::test_3d_array_axis_1 通过 scipy/stats/tests/test_stats.py::TestGeometricStandardDeviation::test_3d_array_axis_2 通过 scipy/stats/tests/test_stats.py::TestFOneWay::test_3d_inputs 通过

如果您只想运行与test_3dtest_3d_example完全匹配的测试怎么办?

尝试使用pytest-regex

python dev.py 测试 -v -regex “(.*test_3d$|.*test_3d_example$)”

scipy/io/tests/test_idl.py::TestArrayDimensions::test_3d 通过 scipy/io/tests/test_idl.py::TestPointerArray::test_3d 通过 scipy/optimize/_trustregion_constr/tests/test_qp_subproblem.py::TestModifiedDogleg::test_3d_example 通过

这完成了我们想要的功能,并且可能比 -k 选项更简洁。事实上,我不太确定如何用当前形式的 -k 来实现这一点。

工作原理

pytest-regex 简单地将 Python 正则表达式传递到节点 ID 列表中,其中节点 ID 的结构如下

path/to/test_module.py::TestClass::test_name[参数值]

如果正则表达式与节点 ID 匹配,则保留并执行测试。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源代码分发

pytest-regex-0.2.0.tar.gz (4.1 kB 查看哈希)

上传时间 源代码

构建分发

pytest_regex-0.2.0-py3-none-any.whl (3.8 kB 查看哈希)

上传时间 Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页