通过命令行从 unittest.TestCase 运行单个测试 [英] Running a single test from unittest.TestCase via the command line
问题描述
在我们的团队中,我们像这样定义大多数测试用例:
In our team, we define most test cases like this:
一个框架"class ourtcfw.py
:
One "framework" class ourtcfw.py
:
import unittest
class OurTcFw(unittest.TestCase):
def setUp:
# Something
# Other stuff that we want to use everywhere
还有很多测试用例,比如 testMyCase.py:
And a lot of test cases like 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()
当我正在编写新的测试代码并希望经常运行它以节省时间时,我确实将__"放在在所有其他测试之前.但它很麻烦,会分散我对正在编写的代码的注意力,并且由此产生的提交噪音很烦人.
When I'm writing new test code and want to run it often, and save time, I do put "__" in front of all other tests. But it's cumbersome, distracts me from the code I'm writing, and the commit noise this creates is plain annoying.
因此,例如,在对 testItIsHot()
进行更改时,我希望能够这样做:
So, for example, when making changes to testItIsHot()
, I want to be able to do this:
$ python testMyCase.py testItIsHot
并让 unittest
只运行 testItIsHot()
我怎样才能做到这一点?
How can I achieve that?
我尝试重写 if __name__ == "__main__":
部分,但由于我是 Python 新手,我感到迷茫,并继续抨击除方法之外的所有其他内容.
I tried to rewrite the if __name__ == "__main__":
part, but since I'm new to Python, I'm feeling lost and keep bashing into everything else than the methods.
推荐答案
这按您的建议工作 - 您只需要指定类名:
This works as you suggest - you just have to specify the class name as well:
python testMyCase.py MyCase.testItIsHot
这篇关于通过命令行从 unittest.TestCase 运行单个测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!