Use nose.run() or nose.main() to run tests in a specific module
Asked Answered
R

3

8

It's mentioned in the documentation (http://nose.readthedocs.org/en/latest/api/core.html) but there don't seem to be any examples, and trying it seems to run all tests in the cwd.

Richrichara answered 11/4, 2014 at 0:49 Comment(0)
G
8

Try this:

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
Goldfarb answered 11/4, 2014 at 17:29 Comment(0)
D
8

Here's a minimal version of a main for nose:

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

And a version for nose2:

if __name__ == '__main__':
    import nose2
    nose2.main()
Depurate answered 11/2, 2015 at 1:41 Comment(0)
J
0

nose has runmodule function. So following works.

if __name__ == '__main__':
    import nose
    nose.runmodule()
Jemison answered 20/7, 2018 at 9:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.