How do I run multiple Classes in a single test suite in Python using unit testing?

后端 未结 5 941
无人共我
无人共我 2021-02-01 03:09

How do I run multiple Classes in a single test suite in Python using unit testing?

5条回答
  •  离开以前
    2021-02-01 03:39

    The unittest.TestLoader.loadTestsFromModule() method will discover and load all classes in the specified module. So you can just do this:

    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 )
    

提交回复
热议问题