2

Nose will automatically run any function it finds in the project starting with test_*. So, for example, if there is a function called:

"""
test_server_setup.py
sets up a pristine database to use for testing.
DO NOT RUN ON PROD
"""

def test_server_init():
    drop_all_tables()

...then nose will run it when you run the command nosetests from the root of the project. Is my only option to rename this function, or is there another way I can change the file so that nose ignores it?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
gregsabo
  • 430
  • 2
  • 9

1 Answers1

5

nottest decorator is exactly what you need:

nose.tools.nottest(func)

Decorator to mark a function or method as not a test

from nose.tools import nottest

@nottest
def test_server_init():
    drop_all_tables()

To exclude a file or a directory from being picked up by the nose test discovery, see:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 1
    This does not answer the post's title "prevent a python file from being run with nose"... rather you show how to prevent a function. – nmz787 Mar 01 '16 at 19:35
  • @nmz787 good point, updated both the question title and the answer. Thanks. – alecxe Mar 01 '16 at 19:46
  • `nottest` works MUCH better than the now-broken `skip` decorator! (in that I can still explicitly run a class/method that is specified as `nottest`, but that class/method will not be run automatically by nose) Thanks! – nmz787 Mar 01 '16 at 19:49