39

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

user5305519
  • 3,008
  • 4
  • 26
  • 44
passionTime
  • 989
  • 6
  • 14
  • 27
  • Did you mean this? http://stackoverflow.com/questions/1732438/run-all-unit-test-in-python-directory It runs multiple python test classes in a directory – amdstorm Jul 16 '12 at 12:44

5 Answers5

51

If you want to run all of the tests from a specific list of test classes, rather than all of the tests from all of the test classes in a module, you can use a TestLoader's loadTestsFromTestCase method to get a TestSuite of tests for each class, and then create a single combined TestSuite from a list containing all of those suites that you can use with run:

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()
rakslice
  • 8,742
  • 4
  • 53
  • 57
  • And How do I use single webdriver instance across multiple classes – Ujjawal Khare Jun 25 '19 at 10:16
  • @UjjawalKhare That sounds like something you should submit to StackOverflow as a question if there are specific issues you are encounting with webdriver. In general there is no limitation on what you do in the code in test methods (`testOne`, `testTwo`, etc. in this example) when using the `uniittest` framework. so whatever method you would use to share anything between classes in an ordinary Python program would be a possibility (dependency injection, global variables, etc.) – rakslice Jun 25 '19 at 19:51
24

I'm a bit unsure at what you're asking here, but if you want to know how to test multiple classes in the same suite, usually you just create multiple testclasses in the same python file and run them together:

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()

And run with for example:

python mytestsuite.py

Better examples can be found in the official documention.

If on the other hand you want to run multiple test files, as detailed in "How to organize python test in a way that I can run all tests in a single command?", then the other answer is probably better.

Community
  • 1
  • 1
tddtrying
  • 870
  • 6
  • 12
7

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 )
pds
  • 2,242
  • 1
  • 22
  • 20
1

Normally you would do in the following way (which adds only one class per suite):

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

If you'd like to have multiple classes per suite, you can use add these tests in the following way:

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

Here is same example to run all classes per separate suite you can define your own make_suite method:

# 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)

If above doesn't suite, you can convert above example into method which could accept multiple classes.

kenorb
  • 155,785
  • 88
  • 678
  • 743
0

I've found nose to be a good tool for this. It discovers all unit tests in a directory structure and executes them.