让鼻子忽略名称中带有 'test' 的函数

Getting nose to ignore a function with 'test' in the name

nose 发现过程会找到名称以 test 开头的所有模块,以及其中所有名称中包含 test 的函数,并尝试 运行 它们作为单元测试。参见 http://nose.readthedocs.org/en/latest/man.html

我在文件 accounts.py 中有一个名为 make_test_account 的函数。我想在名为 test_account 的测试模块中测试该功能。所以在那个文件的开头我做了:

from foo.accounts import make_test_account

但现在我发现 nose 将函数 make_test_account 视为单元测试并尝试 运行 它(失败是因为它没有传递任何必需的参数)。

如何确保 nose 专门忽略该功能?我更愿意以一种方式来做这意味着我可以调用 nose 作为 nosetests,没有任何命令行参数。

告诉 nose 该函数不是测试 - 使用 nottest 装饰器。

# module foo.accounts

from nose.tools import nottest

@nottest
def make_test_account():
    ...

鼻子有一个 nottest 装饰器。但是,如果您不想在要从中导入的模块中应用 @nottest 装饰器,您也可以在导入后简单地修改方法。使单元测试逻辑接近单元测试本身可能会更清晰。

from foo.accounts import make_test_account
# prevent nose test from running this imported method
make_test_account.__test__ = False

您仍然可以使用 nottest 但它具有相同的效果:

from nose.tools import nottest
from foo.accounts import make_test_account
# prevent nose test from running this imported method
make_test_account = nottest(make_test_account)