python - 运行单元测试时没有弃用警告

标签 python python-2.7 python-unittest deprecation-warning

运行单元测试时,我希望看到弃用警告。看来 since Python 2.7 deprecation warnings are silenced .我将从页面引用:

For Python 2.7, a policy decision was made to silence warnings only of interest to developers by default. DeprecationWarning and its descendants are now ignored unless otherwise requested, preventing users from seeing warnings triggered by an application. This change was also made in the branch that became Python 3.2. (Discussed on stdlib-sig and carried out in issue 7319.)

后来看起来好像我应该在运行单元测试时看到弃用警告:

The unittest module also automatically reenables deprecation warnings when running tests.

嗯.. 简而言之,它对我不起作用,所以我一定是做错了什么。我已经使用以下代码进行了测试:

import warnings
import unittest

def spam():
    warnings.warn('test', DeprecationWarning, stacklevel=2)
    return 'spam'

class Eggs(object):
    def __init__(self):
        self.spam = spam()

class Test(unittest.TestCase):
    def test_warn(self):
        eggs = Eggs()
        self.assertEqual('spam', eggs.spam)

然后我运行代码(保存在 spam.py 中):

python -m 'unittest' spam

这给了我以下输出:

.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

没有弃用警告。所以问题是;我在这里做错了什么?

最佳答案

看起来文档有误 - 在 2.7 中,unittest 不会重新启用弃用警告。

>>> import warnings
>>> from pprint import pprint

>>> pprint(warnings.filters)
[('ignore', None, <type 'exceptions.DeprecationWarning'>, None, 0),
 ('ignore', None, <type 'exceptions.PendingDeprecationWarning'>, None, 0),
 ('ignore', None, <type 'exceptions.ImportWarning'>, None, 0),
 ('ignore', None, <type 'exceptions.BytesWarning'>, None, 0)]

>>> import unittest
>>> pprint(warnings.filters)
[('ignore', None, <type 'exceptions.DeprecationWarning'>, None, 0),
 ('ignore', None, <type 'exceptions.PendingDeprecationWarning'>, None, 0),
 ('ignore', None, <type 'exceptions.ImportWarning'>, None, 0),
 ('ignore', None, <type 'exceptions.BytesWarning'>, None, 0)]

... 我在 unittest.py 中没有看到重新启用 DeprecationWarning 的内容。

您当然可以自己启用它们:

warnings.simplefilter('always', DeprecationWarning)

或者在命令行上:

$ python -Wd -m 'unittest' spam
spam.py:10: DeprecationWarning: test
  self.spam = spam()
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

或者将装饰器应用于每个 unittest.TestCase 函数,以便仅在测试时启用 DeprecationWarning:

import warnings
import unittest

def enable_DeprecationWarning(fn):
    def _wrapped(*args, **kwargs):
        with warnings.catch_warnings():
            warnings.simplefilter('always', DeprecationWarning)
            return fn(*args, **kwargs)
    return _wrapped

def spam():
    warnings.warn('test', DeprecationWarning, stacklevel=2)
    return 'spam'

class Eggs(object):
    def __init__(self):
        self.spam = spam()

class Test(unittest.TestCase):
    @enable_DeprecationWarning
    def test_warn(self):
        eggs = Eggs()
        self.assertEqual('spam', eggs.spam)

if __name__ == '__main__':
        unittest.main()

命令行选项可能是单元测试的最佳选择,因为它不需要更改代码。

关于python - 运行单元测试时没有弃用警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25506527/

相关文章:

python - 依赖单元测试

python - 使用 Requests Post 登录此站点无效

python - 如何删除普通表格中的链接?

python - 将列表转换为逗号分隔的字符串,在最后一项之前使用 "and"- Python 2.7

python - 尝试实现 python TestSuite

python - 对 python 类属性进行单元测试

python - 列表理解中函数调用的多次返回

python - 使用 PyInstaller (--onefile) 捆绑数据文件

linux - Python 和 Skype4Py 中 skype.Attach() 中的段错误

python - 适合 Python 平均值的随机分布