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

标签 python nose nosetests

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

最佳答案

试试这个:

测试模块.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

关于python - 使用 nose.run() 或 nose.main() 在特定模块中运行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23001642/

相关文章:

python - 使用 python3 时 Lambda 函数错误

python - 由于应用程序尚未加载,Pytest 模型未导入

python - 评估 python 中元组的字符串表示形式

django-nose:无法使用 --failed 查看自定义用户模型

python - 如果测试通过,我怎样才能包括额外的测试?

python - 用 Nose 捕获 subprocess.Popen() 的输出

python - 让 Python 在 Cocoa App 中工作

python - 将 paver 和 nose 与非典型目录结构一起使用

unit-testing - 在 virtualenv 中使用 gaetestbed

Python + Selenium : Running tests in parallel