在使用nosetests时,我在python测试中过滤警告时遇到了一些麻烦。我希望对warn.filterwarnings的调用只对单个文件是本地的,但是如果我用nosetests call warn.filterwarnings测试的任何文件,所有的测试都会过滤警告。例如(从scikit生物代码库),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。实际上,我也会过滤它,但是我想知道在我的测试套件的其他地方是否有其他的警告没有被捕获。
发布于 2014-06-25 17:19:47
理想情况下,您希望将文本管理器用于暂时压制他们。
https://stackoverflow.com/questions/24414713
复制相似问题