一个用于查找 Python 测试不稳定原因的工具
A tool for finding the causes of unstable Python tests

原始链接: https://github.com/mgaitan/pytest-leak-finder

当一个测试在单独运行时可以通过,但在整个测试套件中却会失败时,通常是由于前置测试产生的状态污染所致。在众多测试中找出罪魁祸首非常耗时,但 `pytest-leak-finder` 插件利用类似于 `git bisect` 的二分查找算法实现了这一过程的自动化。 该插件会确定一个失败的“目标”测试,并将其与前置测试的不同子集交替运行。通过观察目标测试是通过还是失败,插件可以缩小搜索范围。如果目标测试失败,说明“泄露”存在于当前子集中;如果通过,插件则会将重点转向另一半。这种系统的分治法能够快速定位导致副作用的具体测试,从而让开发人员免于手动调试。

对不起。
相关文章

原文

PyPI version Python versions CI

You have a test that passes when executed alone but fails when running its suite. What's happening? My two cents that some previous test keeps the things dirty. But wich one/s, maybe the previous are a lot, right?

This plugin helps to find a culprit by doing a binary search (alla git bisect) on the collected tests before the target.

The first time it will collect the first half of those tests plus the failing one (the target). If the target fails, we are in a good path, so, a new bisect is applied. When the target doesn't fail, it changes the "half" to bisect the next time.

Consider the following example:

$ pytest -v demo/test_demo.py 
collected 6 items                                                                                                                                            
tests/test_demo.py::test1 PASSED                                                                             
tests/test_demo.py::test2 PASSED                                                                              
tests/test_demo.py::test3 PASSED                                                                              
tests/test_demo.py::test4 PASSED                                                                             
tests/test_demo.py::test5 FAILED                                                                              
tests/test_demo.py::test6 PASSED 

$ pytest -v --lf demo/test_demo.py 
collected 6 items / 5 deselected / 1 selected                                                                                                                
tests/test_demo.py::test5 PASSED 

You can use pytest-leak-finder to find the problematic test.

On the first run will set the failed test as the "target" and will stop the session.

$ pytest -v --leak-finder demo/test_demo.py 
collected 6 items

tests/test_demo.py::test1 PASSED                                                                              
tests/test_demo.py::test2 PASSED                                                                              
tests/test_demo.py::test3 PASSED                                                                              
tests/test_demo.py::test4 PASSED                                                                              
tests/test_demo.py::test5 FAILED

===================================================== Leak finder =====================================================
Target set to: pytest-leak-finder/tests/test_demo.py::test5

Next step: a
Current target is: pytest-leak-finder/tests/test_demo.py::test5
============================================= 1 failed, 4 passed in 0.13s =============================================

The second execution will run the first half of the tests passed before the target (step "a", composed by test1 and test2).

If the target still fail, that path would followed deeper by dividing again. But in this example it passes, so we'll discard it, and asumme the other half was the one that include the leak. That's the reason why the "next step" will be "ba".

$ pytest -v --leak-finder demo/test_demo.py
collected 6 items / 3 deselected / 3 selected                                                                                                                
demo/test_demo.py::test1 PASSED                                                                                 [ 33%]
demo/test_demo.py::test2 PASSED                                                                                 [ 66%]
demo/test_demo.py::test5 PASSED                                                                                 [100%]


===================================================== Leak finder =====================================================
We reach the target and nothing failed. Let's change the last half.

Next step: ba
Current target is: pytest-leak-finder/demo/test_demo.py::test5
=========================================== 3 passed, 3 deselected in 0.03s ===========================================

So, the new step will be "B-A", i.e. test3

$ pytest -v --leak-finder demo/test_demo.py
collected 6 items / 3 deselected / 3 selected                                                                                                                
tests/test_demo.py::test3 PASSED                                                                              
tests/test_demo.py::test5 FAILED

===================================================== Leak finder =====================================================
We found a leak!

Leak found in: pytest-leak-finder/demo/test_demo.py::test3
Last step was: ba

And there it is, test3 was the problematic test we were looking for!

联系我们 contact @ memedata.com