user1116309
user1116309

Reputation: 101

how to reuse tests written using unittest.testcase

I've written some tests using unittest as below and I want to reuse them in another class where I'm stuck and need help.. Code snippets are as below.

    MyTestClass.py
    Class MyTestClass(unittest.TestCase):   
        @classmethod
        def test_TC01_set(self):
            self.devAddr = "127.0.0.0"
            self.teststoSkip = 'TC02'

        def skip(type):
            if type in self.teststoSkip:
                self.skipTest('skipped!!') #unittest.Testcase method

        def test_TC02(self):
            self.skip('TC02')
            print 'test_TC02 will do other tasks'

        def test_TC03(self):
            self.skip('TC03')
            print 'test_TC03 will do other tasks'

This will work fine. Now I want to reuse the same testcases in another class. say,

    RegressionMyTest.py
    from MyTestClass import MyTestClass
    Class RegressionMyTest(MyTestClass):
        @classmethod
        def setupmytest(self):
            self.test_TC01_set(self)#this will work fine since it is accessing classmethod
            self.tes_TC02(self)#cant access like this since it is not a class method
            self.tes_TC03(self)#cant access like this since it is not a class method

How can I reuse the tests in MyTestClass in RegressionMyTest so that both MyTestClass and RegressionMyTest should work if they are run individually using nosetests/unittest.

Upvotes: 7

Views: 3961

Answers (2)

dm03514
dm03514

Reputation: 55962

Usually tests are supposed to assert code is functioning in a certain way, so I'm not sure if it would make sense to actually share tests between testsuites, (I don't think it would be very explicit)

Python tests cases are just python classes, that are introspected by the test runner for methods beginning in test_. Because of this you can you use inheritance in the same way you would with normal classes.

If you need shared functionality, you could create a base class with shared initialization methods/ helper methods. Or create testing mixins with utility functions that are needed across tests.

    class BaseTestCase(unittest.TestCase):
      def setUp(self):
         # ran by all subclasses
    
      def helper(self):
        # help
    
    class TestCaseOne(BaseTestCase):
    
       def setUp(self):
          # additional setup
          super(TestCaseOne, self).setUp()

      def test_something(self):
          self.helper() # <- from base

Perhaps you don't want to define a setup method on the base class and just define on child classes using some helper methods defined in the base class? Lots of options!

Upvotes: 10

Clock ZHONG
Clock ZHONG

Reputation: 980

I don't think your title describe your question correctly. Your code mistake is: Calling a parent class "object method" in the child "class method"(@classmethod), because an "object method" must have one class instance(object), so in the child "class method", the system could find any object instance for its parent class.

You just need review the concepts of "class methods" and "object methods"(or instance methods) in programming language.

Upvotes: 0

Related Questions