14

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

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

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).

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.

jwg
  • 5,547
  • 3
  • 43
  • 57

2 Answers2

13

Tell nose that the function is not a test - use the nottest decorator.

# module foo.accounts

from nose.tools import nottest

@nottest
def make_test_account():
    ...
Dušan Maďar
  • 9,269
  • 5
  • 49
  • 64
12

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

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)
jsnow
  • 1,399
  • 1
  • 9
  • 7
  • 1
    This looks to be a lot neater approach than dm295's accepted answer, since it does not impose test specific (and more importantly - **test framework specific**) code to be added in production code. Thanks for this answer! – dsoosh Jun 30 '17 at 11:46
  • 1
    This seems better because it will work across test runners without introducing a nose dependency. – weberc2 Nov 07 '17 at 20:58