在我们的团队中,我们会这样定义大多数测试用例:
一个“框架”类ourtcfw.py
:
import unittest
class OurTcFw(unittest.TestCase):
def setUp:
# Something
# Other stuff that we want to use everywhere
还有很多测试用例,比如 testMyCase.py:
import localweather
class MyCase(OurTcFw):
def testItIsSunny(self):
self.assertTrue(localweather.sunny)
def testItIsHot(self):
self.assertTrue(localweather.temperature > 20)
if __name__ == "__main__":
unittest.main()
当我正在编写新的测试代码并想经常运行它并节省时间时,我会在所有其他测试前面加上“__”。但这很麻烦,分散了我正在编写的代码的注意力,而且这产生的提交噪音很烦人。
因此,例如,在对 testItIsHot()
进行更改时,我希望能够这样做:
$ python testMyCase.py testItIsHot
并让 unittest
运行 testItIsHot()
我怎样才能做到这一点?
我尝试重写 if __name__ == "__main__":
部分,但由于我是 Python 新手,所以我感到迷茫,并不断抨击方法以外的所有内容。
最佳答案
这正如你所建议的那样工作 - 你只需要指定类名:
python testMyCase.py MyCase.testItIsHot
关于python - 通过命令行从 unittest.TestCase 运行单个测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15971735/