Use nose rune () or nose .main () to run tests in a specific module

It is mentioned in the documentation ( http://nose.readthedocs.org/en/latest/api/core.html ), but there seems to be no examples, and it looks like it is trying to run all the tests in cwd.

+8
python nose nosetests
source share
2 answers

Try the following:

test_module.py:

import logging import sys import nose logging.basicConfig(level=logging.INFO) #here are some tests in this module def test_me(): pass if __name__ == '__main__': #This code will run the test in this file.' module_name = sys.modules[__name__].__file__ logging.debug("running nose for package: %s", module_name) result = nose.run(argv=[sys.argv[0], module_name, '-v']) logging.info("all tests ok: %s", result) 

python test_module.py will get you:

 test_module.test_me ... ok ---------------------------------------------------------------------- Ran 1 test in 0.001s OK INFO:root:all tests ok: True 
+8
source share

Here's the minimum version of the core for the nose:

 if __name__ == '__main__': import nose nose.run(defaultTest=__name__) 

And nose version 2:

 if __name__ == '__main__': import nose2 nose2.main() 
+6
source share

All Articles