繁体   English   中英

如何从python脚本运行python文件中的所有测试?

[英]How to run all tests in a python file from a python script?

如何从脚本中的python文件运行所有测试(unittest.TestCase)?

我尝试使用鼻子,但似乎总是运行测试发现。 我想要的只是导入一个模块,给一些函数一个路径并获得测试结果,任何建议?

在测试脚本的底部放

if __name__ == '__main__':
    unittest.main()

然后正常调用您的文件

$ python my_test_script.py

或者,如果你想使用nosepytest ,你可以给它一个你想要运行的脚本的名称,它仍然会发现,但只能在那个文件上。

$ nosetests my_test_script.py

$ py.test my_test_script.py

如果你在python里面运行鼻子,你可以使用nose.run()

my_script.py

import nose
nose.run()

默认情况下,它将使用您传递给脚本的参数,因此如果您只想运行单个test_script

$ python my_script.py /path/to/test_script.py

或者,您可以直接在脚本中传递参数

nose.run(argv=[__file__, '/path/to/test_script'])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM