跳转到主要内容

一个用于检测测试污染的工具

项目描述

build status pre-commit.ci status

detect-test-pollution

一个用于检测测试污染的工具

安装

pip install detect-test-pollution

什么是测试污染?

video about test pollution

测试污染是指测试套件中某个测试因其他测试的副作用而失败。

这通常表现为“测试碎片”,即测试神秘失败,但单独运行时通过。

以下是一个简单的示例:

k = 1

def test_k():
    assert k == 1

def test_k2():
    global k

    k = 2
    assert k == 2

现在这个例子有点愚蠢,你可能不会写出这么糟糕的代码,但这有助于我们在这里展示问题。

正常运行时 -- 这些测试通过

$ pytest -q t.py
..                                                                       [100%]
2 passed in 0.00s

但是,如果测试以其他顺序运行(例如由于pytest-randomlypytest-xdist),污染就会显现出来

$ pytest -q t.py::test_k2 t.py::test_k
.F                                                                       [100%]
=================================== FAILURES ===================================
____________________________________ test_k ____________________________________

    def test_k():
>       assert k == 1
E       assert 2 == 1

t.py:4: AssertionError
=========================== short test summary info ============================
FAILED t.py::test_k - assert 2 == 1
1 failed, 1 passed in 0.03s

通常这种情况发生在有数百或数千个测试的代码库中,难以追踪导致全局副作用的测试。

这就是这个工具派上用场的地方!它可以帮助你找到运行顺序时产生错误的测试对。

用法

video about using detect-test-pollution

一旦你确定了一个失败的测试,你就能将其输入到 detect-test-pollution 中以找到原因测试。

基本模式是运行

detect-test-pollution \
    --failing-test test.py::test_id_here \
    --tests ./tests

其中 test.py::test_id_here 是失败测试的标识符,而 ./tests 是你的测试套件所在的目录。

如果你已经将测试ID缩小到那个范围以下,可以使用 --testids-file 而不是 --tests 来加快发现速度

detect-test-pollution \
    --failing-test test.py::test_id_here \
    --testids-file ./testids

通常可以通过pytest --collect-only -q获取测试ID列表(但需要删除末尾的一些无关行,例如时间和警告信息)。

然后detect-test-pollution将通过二分法在测试列表中找到失败的测试。以下是一个来自pytest中的错误的二分法示例。

$ detect-test-pollution --tests ./testing --failing-test testing/io/test_terminalwriter.py::test_should_do_markup_FORCE_COLOR
discovering all tests...
-> discovered 3140 tests!
ensuring test passes by itself...
-> OK!
ensuring test fails with test group...
-> OK!
running step 1:
- 3139 tests remaining (about 12 steps)
running step 2:
- 1570 tests remaining (about 11 steps)
running step 3:
- 785 tests remaining (about 10 steps)
running step 4:
- 393 tests remaining (about 9 steps)
running step 5:
- 197 tests remaining (about 8 steps)
running step 6:
- 99 tests remaining (about 7 steps)
running step 7:
- 50 tests remaining (about 6 steps)
running step 8:
- 25 tests remaining (about 5 steps)
running step 9:
- 12 tests remaining (about 4 steps)
running step 10:
- 6 tests remaining (about 3 steps)
running step 11:
- 3 tests remaining (about 2 steps)
double checking we found it...
-> the polluting test is: testing/test_terminal.py::TestTerminal::test_report_teststatus_explicit_markup

模糊测试

detect-test-pollution还可以用于“模糊”出失败的测试。

它通过随机排列测试ID并运行测试套件直到失败来完成。

以下是在一个愚蠢的测试套件上的执行示例。

$ detect-test-pollution --fuzz --tests t.py
discovering all tests...
-> discovered 1002 tests!
run 1...
-> OK!
run 2...
-> found failing test!
try `detect-test-pollution --failing-test t.py::test_k --tests t.py`!

之后,您可以使用detect-test-pollution的正常模式来查找失败的配对。

支持的测试运行器

目前仅支持pytest -- 尽管理论上该工具可以被修改以支持其他Python测试运行器,甚至其他语言。

项目详情


下载文件

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

源代码分发

detect_test_pollution-1.2.0.tar.gz (6.8 kB 查看哈希值)

上传时间: 源代码

构建分发

detect_test_pollution-1.2.0-py2.py3-none-any.whl (7.3 kB 查看哈希值)

上传时间: Python 2 Python 3

由以下机构支持