在我们的团队中,我们定义大多数测试用例是这样的:
一个“框架”类 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 的新手,我感到迷茫并继续抨击除方法之外的所有内容。
原文由 Alois Mahdal 发布,翻译遵循 CC BY-SA 4.0 许可协议
这按照你的建议工作——你只需要指定类名: