使用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
也是如此。在实践中,我最终也会对其进行过滤,但我想知道是否还有其他警告未被我的测试套件中的其他测试捕获。