【问题标题】:How do I run multiple Classes in a single test suite in Python using unit testing?如何使用单元测试在 Python 的单个测试套件中运行多个类?
【发布时间】:2011-03-19 07:40:44
【问题描述】:

如何使用单元测试在 Python 中的单个测试套件中运行多个类?

【问题讨论】:

标签: python unit-testing


【解决方案1】:

如果您想运行特定测试类列表中的所有测试,而不是模块中所有测试类中的所有测试,您可以使用TestLoaderloadTestsFromTestCase 方法为每个类获取一个TestSuite 的测试,然后从包含所有可以与run 一起使用的套件的列表中创建一个组合TestSuite

import unittest

# Some tests

class TestClassA(unittest.TestCase):
    def testOne(self):
        # test code
        pass

class TestClassB(unittest.TestCase):
    def testOne(self):
        # test code
        pass

class TestClassC(unittest.TestCase):
    def testOne(self):
        # test code
        pass

def run_some_tests():
    # Run only the tests in the specified classes

    test_classes_to_run = [TestClassA, TestClassC]

    loader = unittest.TestLoader()

    suites_list = []
    for test_class in test_classes_to_run:
        suite = loader.loadTestsFromTestCase(test_class)
        suites_list.append(suite)
        
    big_suite = unittest.TestSuite(suites_list)

    runner = unittest.TextTestRunner()
    results = runner.run(big_suite)

    # ...

if __name__ == '__main__':
    run_some_tests()

【讨论】:

  • 以及如何跨多个类使用单个 webdriver 实例
  • @UjjawalKhare 如果您在使用 webdriver 时遇到特定问题,这听起来像是您应该将问题提交给 StackOverflow。一般来说,在使用uniittest 框架时,您在测试方法(本例中为testOnetestTwo 等)的代码中所做的操作没有限制。因此,无论您使用什么方法在普通 Python 程序中的类之间共享任何内容都是可能的(依赖注入、全局变量等)
【解决方案2】:

我有点不确定你在这里问什么,但是如果你想知道如何在同一个套件中测试多个类,通常你只需在同一个 python 文件中创建多个测试类并一起运行它们:

import unittest

class TestSomeClass(unittest.TestCase):
    def testStuff(self):
            # your testcode here
            pass

class TestSomeOtherClass(unittest.TestCase):
    def testOtherStuff(self):
            # testcode of second class here
            pass

if __name__ == '__main__':
    unittest.main()

并运行例如:

python mytestsuite.py

可以在the official documention 中找到更好的示例。

另一方面,如果您想运行多个测试文件,如"How to organize python test in a way that I can run all tests in a single command?" 中所述,那么另一个答案可能会更好。

【讨论】:

    【解决方案3】:

    unittest.TestLoader.loadTestsFromModule() 方法将发现并加载指定模块中的所有类。所以你可以这样做:

    import unittest
    import sys
    
    class T1(unittest.TestCase):
      def test_A(self):
        pass
      def test_B(self):
        pass
    
    class T2(unittest.TestCase):
      def test_A(self):
        pass
      def test_B(self):
        pass
    
    if __name__ == "__main__":
      suite = unittest.TestLoader().loadTestsFromModule( sys.modules[__name__] )
      unittest.TextTestRunner(verbosity=3).run( suite )
    

    【讨论】:

      【解决方案4】:

      通常您会采用以下方式(每个套件仅添加一个类):

      # Add tests.
      alltests = unittest.TestSuite()
      alltests.addTest(unittest.makeSuite(Test1))
      alltests.addTest(unittest.makeSuite(Test2))
      

      如果您希望每个套件有多个类,可以通过以下方式添加这些测试:

      for name in testnames:
          suite.addTest(tc_class(name, cargs=args))
      

      这是在每个单独的套件中运行所有类的相同示例,您可以定义自己的 make_suite 方法:

      # Credits: http://codereview.stackexchange.com/a/88662/15346
      def make_suite(tc_class):
          testloader = unittest.TestLoader()
          testnames = testloader.getTestCaseNames(tc_class)
          suite = unittest.TestSuite()
          for name in testnames:
              suite.addTest(tc_class(name, cargs=args))
          return suite
      
      # Add all tests.
      alltests = unittest.TestSuite()
      for name, obj in inspect.getmembers(sys.modules[__name__]):
          if inspect.isclass(obj) and name.startswith("FooTest"):
              alltests.addTest(make_suite(obj))
      
      result = unittest.TextTestRunner(verbosity=2).run(alltests)
      

      如果上面不适合,您可以将上面的示例转换为可以接受多个类的方法。

      【讨论】:

        【解决方案5】:

        我发现nose 是一个很好的工具。它发现目录结构中的所有单元测试并执行它们。

        【讨论】:

          猜你喜欢
          • 2016-11-07
          • 2014-03-25
          • 2016-12-13
          • 2011-02-10
          • 2010-12-03
          • 1970-01-01
          • 1970-01-01
          • 2016-11-24
          • 1970-01-01
          相关资源
          最近更新 更多