Creating a test suite from multiple test cases

7.1k views Asked by At

I've been get myself acquainted with the unittest library in Python, and I've written up several unitest.TestCases which look similar to this:

class TestOne(unittest.TestCase):
    def setUp(self):
        pass

    def first_test(self):
        self.assertEqual('a', 'b')

    def second_test(self):
        self.assertEqual('a', 'b')

    def third_test(self):
        self.assertEqual('a', 'b')

    def tearDown(self):
        pass


class TestTwo(unittest.TestCase):
    def setUp(self):
        pass

    def first_test(self):
        self.assertEqual('a', 'b')

    def second_test(self):
        self.assertEqual('a', 'b')

    def third_test(self):
        self.assertEqual('a', 'b')

    def tearDown(self):
        pass


class TestThree(unittest.TestCase):
    def setUp(self):
        pass

    def first_test(self):
        self.assertEqual('a', 'b')

    def second_test(self):
        self.assertEqual('a', 'b')

    def third_test(self):
        self.assertEqual('a', 'b')

    def tearDown(self):
        pass

Now that isn't what my code looks like exactly, but that is the basic structure that is followed.

I know that if I want to execute a single TestCase, I can do this:

suite = unittest.TestLoader().loadTestsFromTestCase(TestOne)
unittest.TextTestRunner(verbosity=2).run(suite)

However, I've been struggling with getting multiple TestCases to run at the same time.

I've tried doing this:

suite = unittest.TestSuite()
suite.addTest(TestOne())
unittest.TextTestRunner(verbosity=2).run(suite)

But that throws the error:

ValueError: no such test method in <class 'unit_tests.TestOne'>: runTest

I know I would use a runTest method if I had only a single test per case, but I have several, all of which need to be their own individual tests inside of a TestCase.

I've tried poring through the unittest documentation, but I have been unable to figure out what to do to run tests from multiple test cases from within a single test suite.

I've looked at nosetests, and it does what I want to do without me having to make any modifications to my code, but I would like to know if there is a way to run tests from multiple cases in a suite without depending on an external library.

1

There are 1 answers

0
JoshRomRock On BEST ANSWER

Did you try to create a TestSuite from a list of suites created with TestLoader? This is an example adapted from Python documentation:

suite1 = unittest.TestLoader().loadTestsFromTestCase(TestOne)
suite2 = unittest.TestLoader().loadTestsFromTestCase(TestTwo)
alltests = unittest.TestSuite([suite1, suite2])