鼻子工具和pylint

use*_*654 5 python testing pylint nose

什么是使用nose.tools并保持pylint快乐的正确方法?

以下代码:

'''
This is a test
'''

import nose.tools
import nose.tools.trivial

nose.tools.assert_equal(1, 1)
nose.tools.assert_equals(1, 1)

nose.tools.trivial.assert_equal(1, 1)
nose.tools.trivial.assert_equals(1, 1)
Run Code Online (Sandbox Code Playgroud)

导致以下pylint错误:

$ pylint -i y -r n /tmp/aseq.py
************* Module aseq
E1101:  8,0: Module 'nose.tools' has no 'assert_equal' member
E1101:  9,0: Module 'nose.tools' has no 'assert_equals' member
E1101: 11,0: Module 'nose.tools.trivial' has no 'assert_equal' member
E1101: 12,0: Module 'nose.tools.trivial' has no 'assert_equals' member
Run Code Online (Sandbox Code Playgroud)

当然,有人可以禁用E1101,有更清洁的方式吗?

ale*_*cxe 2

nose.tools.trivialunittest.TestCase只需动态检查类,并使所有“公共”方法从assertavailable fromnose.tools或开始nose.tools.trivial

鼻子/工具/__init__.py:

from nose.tools.nontrivial import *
from nose.tools.nontrivial import __all__ as nontrivial_all
from nose.tools.trivial import *
from nose.tools.trivial import __all__ as trivial_all

__all__ = trivial_all + nontrivial_all
Run Code Online (Sandbox Code Playgroud)

鼻子/工具/trivial.py:

...

class Dummy(unittest.TestCase):
    def nop():
        pass
_t = Dummy('nop')

for at in [ at for at in dir(_t)
            if at.startswith('assert') and not '_' in at ]:
    pepd = pep8(at)
    vars()[pepd] = getattr(_t, at)
    __all__.append(pepd)

...
Run Code Online (Sandbox Code Playgroud)

Pylint 无法处理这种“hacky”行为。

考虑使用andnose.tools.eq_代替(这些方法实际上是相同的)。希望有帮助。assert_equalassert_equals