在我们的团队中,我们定义大多数测试用例:
一个“框架”类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
并有单元测试运行testItIsHot()
我如何实现呢?
我试图重写if __name__ ==“__main__”:一部分,但是因为我是新的Python,我感觉迷失,继续打破除了方法之外的一切。