Search code examples
pythonunit-testingtest-suite

Add simple test method to TestSuite


How can I add simple test method from unittest.TestCase to TestSuite. As I see it is only possible to add whole class only to suite, for example I want something like this:

import unittest


class MyBaseTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.abs = "test"


class MyTestClass(MyBaseTestCase):
    def test_abs(self):
        if self.abs:
            pass


class MyTestSuite(unittest.TestSuite):
    def __init__(self):
        super().__init__()
        self.addTest(MyTestClass.test_abs)

Here I get an error: AttributeError: 'TeamcityTestResult' object has no attribute 'abs'. It seems like it runs as a test, but setUpClass does not calls.


Solution

  • How did you run the test suite? I used your code and ran it using 'python3 -m unittest test.py':

    import unittest
    
    
    class MyBaseTestCase(unittest.TestCase):
        @classmethod
        def setUpClass(cls):
            cls.abs = "test"
    
    
    class MyTestClass(MyBaseTestCase):
        def test_abs(self):
            if self.abs:
                pass
    
    
    class MyTestSuite(unittest.TestSuite):
        def __init__(self):
            super().__init__()
            self.addTest(MyTestClass.test_abs)
    

    And it works.