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

Python 使用nose.run()或nose.main()在特定模块中运行测试,python,nose,nosetests,Python,Nose,Nosetests,文档()中提到了它,但似乎没有任何示例,尝试它似乎可以运行cwd中的所有测试。尝试以下方法: 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 wil

文档()中提到了它,但似乎没有任何示例,尝试它似乎可以运行cwd中的所有测试。

尝试以下方法:

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\u module.py
将为您提供:

test_module.test_me ... ok

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

OK
INFO:root:all tests ok: True

以下是一个用于鼻子的主系统的最小版本:

if __name__ == '__main__':
    import nose
    nose.run(defaultTest=__name__)
以及nose2的一个版本:

if __name__ == '__main__':
    import nose2
    nose2.main()
鼻子有功能。所以下面的工作

if __name__ == '__main__':
    import nose
    nose.runmodule()