【问题标题】:Can I group test methods and/or test classes in Python unittest我可以在 Python unittest 中对测试方法和/或测试类进行分组吗
【发布时间】:2015-11-25 21:49:29
【问题描述】:

来自 PHPUnit,使用@group annotation 可以很容易地对测试类或函数进行分组。这样我就可以运行或排除一个非常特殊的测试子集,可能跨越多个文件。

我想知道 python unittest 是否有类似的东西。如果是这种情况,我该如何使用它并从 CLI 运行它?

谢谢。

【问题讨论】:

    标签: python unit-testing phpunit python-unittest


    【解决方案1】:

    可以通过将它们全部放在一个类中来运行一组测试函数。假设您的单元测试中有 4 个测试函数,并且您希望有两组 2 个函数。您需要创建一个包含两个类的 tests.py 脚本,每个类都有 2 个函数:

    from unittest import TestCase
    class testClass1(TestCase):
       def testFunction1(self):
          #test something
       def testFunction2(self):
          #test something
    class testClass2(TestCase):
       def testFunction3(self):
          #test something
       def testFunction4(self):
          #test something
    

    然后要从命令行仅在类 1 上运行您的单元测试,您可以运行

    python -m unittest tests.testClass1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多