2

I am writing a set of test cases say Test1, Test2 in a test module.

Is there a way to skip Test1 or selectively execute only Test2 in that module using the command nose.main()?

My module contains,

test_module.py,

class Test1:
    setUp(self):
       print('setup')
    tearDown(self):
       print('teardown')
    test(self):
       print('test1')

class Test2:
    setUp(self):
       print('setup')
    tearDown(self):
       print('teardown')
    test(self):
       print('test2')

I run it from a different python file using,

if __name__ == '__main__':
    nose.main('test_module')
  • possible duplicate of [Conditional skip TestCase decorator in nosetests](http://stackoverflow.com/questions/21936292/conditional-skip-testcase-decorator-in-nosetests) – salparadise Apr 01 '15 at 18:28
  • But I don't want to use decorators here as I don't want to touch the test module code once written. And I wanted to skip them with an option from main module. Am I missing some thing here? – Joshi Sravan Kumar Apr 01 '15 at 18:34

1 Answers1

2

The notion of skipping test and not running a test are different in the context of nose: skipped tests will be reported as skipped at the end of the test result. If you want to skip the test you would have to monkey patch your test module with decorators or do some other dark magic.

But if you want to just not run a test, you can do it the same way you would do it from the command line: using --exclude option. It takes a regular expression of the test you do not want to run. Something like this:

import sys
import nose

def test_number_one():
    pass

def test_number_two():
    pass

if __name__ == '__main__':
    module_name = sys.modules[__name__].__file__

    nose.main(argv=[sys.argv[0],
                    module_name,
                    '--exclude=two',
                    '-v'
                    ])

Running the test will give you:

$ python stackoverflow.py
stackoverflow.test_number_one ... ok

----------------------------------------------------------------------
Ran 1 test in 0.002s

OK
Oleksiy
  • 6,337
  • 5
  • 41
  • 58