使用nose.run()或nose.main()在特定模块中运行测试

nee*_*eel 8 python nose nosetests

它在文档中提到(http://nose.readthedocs.org/en/latest/api/core.html),但似乎没有任何示例,尝试它似乎在cwd中运行所有测试.

Ole*_*siy 8

试试这个:

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)
Run Code Online (Sandbox Code Playgroud)

python test_module.py 会得到你:

test_module.test_me ... ok

----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
INFO:root:all tests ok: True
Run Code Online (Sandbox Code Playgroud)


pau*_*kow 8

这是一个针对鼻子的主要的最小版本:

if __name__ == '__main__':
    import nose
    nose.run(defaultTest=__name__)
Run Code Online (Sandbox Code Playgroud)

和nose2的版本:

if __name__ == '__main__':
    import nose2
    nose2.main()
Run Code Online (Sandbox Code Playgroud)