本文介绍了通过命令行选项从 Python 程序运行 unittest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的设置 -
project/
__init__.py
prog.py
test/
__init__.py
test_prog.py
我希望能够通过调用 prog.py 中的命令行选项来运行我的单元测试.这样,当我部署我的项目时,我可以随时部署运行单元测试的能力.
I would like to be able to run my unit tests by calling a command-line option in prog.py. This way, when I deploy my project, I can deploy the ability to run the unit tests at any time.
python prog.py --unittest
在 prog.py 或我的项目的其余部分中需要什么才能使其正常工作?
What do I need in prog.py, or the rest of my project for this to work?
推荐答案
也许 这个 就是你要找的.在 test_prog.py
中实现一个 load_tests
函数,并在 prog.py
中使用以下代码加载和运行测试:
Perhaps this is what you're looking for. Implement a load_tests
function in test_prog.py
and use the following code in prog.py
to load and run the tests:
import unittest
import test.test_prog
suite = unittest.TestLoader().loadTestsFromModule(test.test_prog)
unittest.TextTestRunner().run(suite)
这篇关于通过命令行选项从 Python 程序运行 unittest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!