可以将warn.filterwarnings设置为在使用nosetests进行测试时在本地执行操作

时间:2014-06-25 17:10:14

标签: python nose skbio

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

1 个答案:

答案 0 :(得分:1)

理想情况下,您希望将conext管理器用于temporarily suppress them