使用nose.run()或nose.main()在特定模块中运行测试
文档中提到过这个内容(http://nose.readthedocs.org/en/latest/api/core.html),但好像没有任何示例。而且尝试之后发现,它似乎会在当前工作目录下运行所有的测试。
3 个回答
0
nose有一个叫做 runmodule
的功能。也就是说,下面的代码可以正常运行。
if __name__ == '__main__':
import nose
nose.runmodule()
8
这是一个简单的nose的主程序示例:
if __name__ == '__main__':
import nose
nose.run(defaultTest=__name__)
这是一个nose2的主程序示例:
if __name__ == '__main__':
import nose2
nose2.main()
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)
运行 python test_module.py
你会得到:
test_module.test_me ... ok
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
INFO:root:all tests ok: True