python - 通过命令行从 unittest.TestCase 运行单个测试

标签 python unit-testing python-unittest

在我们的团队中,我们会这样定义大多数测试用例:

一个“框架”类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/

相关文章:

python - 使用 Plotnine 在 Python 中绘制 QQ 绘图

python - 分类 : skewed data within a class

python - 区分 `colorbar` 中的截取值

java - 使用@DataJpaTest 时未配置 JdbcTemplate

python - 是否可以使用pyunit动态加载测试用例

python - return with nothing 在 python 中是什么意思?

javascript - 将测试中的日期与 Jasmine 进行比较时出错

python - 如何从 setup.py (setuptools) 中运行我的测试?

python - 在单元测试中比较字典时如何忽略某些值?

python - 在哪里放置 python 单元测试