在使用 nosetests 时,我在 python 测试中过滤警告时遇到了一些麻烦。我希望我对 warn.filterwarnings 的调用仅对单个文件是本地的,但是如果我使用 nosetests 测试的任何文件调用 warn。 filterwarnings,在所有测试中过滤警告。例如(来自 scikit-bio code base ),skbio/math/diversity/beta/tests/test_base.py 没有调用 warn.filterwarnings,所以正如预期的那样如果在测试期间生成警告,则会将其打印到屏幕上:
$ nosetests skbio/math/diversity/beta/tests/test_base.py
../Users/caporaso/Dropbox/code/skbio/skbio/math/diversity/beta/base.py:89: UserWarning: pw_distances_from_table is deprecated. In the future (tentatively scikit-bio 0.2.0), pw_distance will take a biom.table.Table object and this function will be removed. You will need to update your code to call pw_distances at that time.
warn("pw_distances_from_table is deprecated. In the future (tentatively "
...
----------------------------------------------------------------------
Ran 5 tests in 0.017s
OK
但是,在skbio/core/alignment/tests/test_pairwise.py中,有对warn.filterwarnings的调用。如果我在 skbio/math/diversity/beta/tests/test_base.py 之前运行这些测试,则不会打印上述警告:
$ nosetests skbio/core/alignment/tests/test_pairwise.py skbio/math/diversity/beta/tests/test_base.py
...................
----------------------------------------------------------------------
Ran 19 tests in 0.056s
OK
我想打印来自 skbio/math/diversity/beta/tests/test_base.py 的警告,即使其他 warn.filterwarnings 被调用其他测试文件。实际上,我最终也会对其进行过滤,但我想知道是否还有其他警告未被我的测试套件中其他地方的测试捕获。
请您参考如下方法:
理想情况下,您希望使用 conext 管理器来 temporarily suppress them .
