让鼻子忽略名称中带有“测试"的功能 [英] Getting nose to ignore a function with 'test' in the name

查看:65
本文介绍了让鼻子忽略名称中带有“测试"的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

nose发现过程查找名称以test开头的所有模块,并在其中查找名称中具有test的所有函数,并尝试将其作为单元测试运行.请参见 http://nose.readthedocs.org/en/latest/man.html

The nose discovery process finds all modules whose name starts with test, and within them all functions which have test in the name and tries to run them as unit tests. See http://nose.readthedocs.org/en/latest/man.html

我在文件accounts.py中有一个函数,其名称为make_test_account.我想在名为test_account的测试模块中测试该功能.因此,在该文件的开头,我做了:

I have a function whose name is say, make_test_account, in the file accounts.py. I want to test that function in a test module called test_account. So at the start of that file I do:

from foo.accounts import make_test_account

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

But now I find that nose treats the function make_test_account as a unit test and tries to run it (which fails because it doesn't pass in any parameters, which are required).

如何确保鼻子专门忽略该功能?我希望这样做的方式意味着我可以在没有任何命令行参数的情况下将鼻子作为nosetests调用.

How can I make sure nose ignores that function specifically? I would prefer to do it in a way which means I can invoke nose as nosetests, without any command line arguments.

推荐答案

Nose具有nottest装饰器.但是,如果您不想在要导入的模块中应用@nottest装饰器,也可以在导入后简单地修改方法.保持单元测试逻辑与单元测试本身接近可能更清洁.

Nose has a nottest decorator. However, if you don't want to apply the @nottest decorator in the module you are importing from you can also simply modify the method after the import. It may be cleaner to keep unit test logic close to the unit test itself.

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

您仍然可以使用nottest,但效果相同:

You can still use nottest but it has the same effect:

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)

这篇关于让鼻子忽略名称中带有“测试"的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆