-1

Following this answer of this thread, I'm trying to override super class init function of testing project with python unittest that has three classes as below:

base.py

import unittest

    class BaseTest(unittest.TestCase):
        def setUp(self):
            print '\n\t Prepare configuration'
        #: executed after each test
        def tearDown(self):
            print '\n\t Closing session'

helper.py

from base import *

class HELPER(BaseTest):
    """ Predefine Parameter """
    URL         = ''
    ID          = ''
    Module      = ''
    def __init__(self, module=''):

        self.Module = module

    def open_url(self):
        print "INIT FROM SUPER CLASS IS "
        print self.Module
        status_code = 200
        self.assertEqual(200,status_code)

test_province.py

from helper import *

class TestProvince(HELPER):
    def __init__(self, module = ''):
        super(TestProvince, self).__init__()
        self.Module = 'Province'

    def test_open_url(self):
        self.open_url()



if __name__ == "__main__":
    unittest.main()

The detail is that in test_province.py that I tried to override super class HELPER's init function but it does not work with an error execption

AttributeError: 'TestProvince' object has no attribute '_testMethodName'

What is wrong or missing with this? How can I override super class init function correctly? Please kindly help Thanks

Houy Narun
  • 1,557
  • 5
  • 37
  • 86

2 Answers2

1

The docstring for unittest.TestCase includes this text:

If it is necessary to override the __init__ method, the base class __init__ method must always be called. It is important that subclasses should not change the signature of their __init__ method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.

So the problem is not with how TestProvince is inheriting from HELPER, but with how HELPER is overriding the __init__ method it inherited from the library code. If you add a call to super(HELPER, self).__init__, it will probably work properly.

You probably also need to change your __init__ to match the signature the base class uses: __init__(self, methodName='runTest')

A final note: Python 2 is going to be losing its official support from the Python developers at the end of the year, so you may be better served learning Python 3 now, rather than learning Python 2 and them needing to update your knowledge for the newer version. It's relevant in this context, because Python 3 lets you use super with no arguments, which is pretty nice (no need to repeat yourself with the current class name).

Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • thanks very much for your helpful answer, I managed to solve problem from this answer https://stackoverflow.com/questions/27158573/how-to-delete-a-record-by-id-in-flask-sqlalchemy . Anyway I've learned something new :) – Houy Narun Jul 10 '19 at 03:39
1

First of all, I would like to thank you @Blckknght very much for your useful detail answer.

I managed to find solution from this old thread (7 years ago), so I changed all three classes as the following:

base.py

import unittest

class BaseTest(unittest.TestCase):
    # ** Adding init Function from unittest class **
    def __init__(self, *args, **kwargs):
        unittest.TestCase.__init__(self, *args, **kwargs)

    def setUp(self):
        print '\n\t Prepare configuration'
    #: executed after each test
    def tearDown(self):
        print '\n\t Closing session'

helper.py

from base import *

class HELPER(BaseTest):
    """ Predefine Parameter """
    URL         = ''
    ID          = ''
    Module      = ''
    # *** Adding init function from class BaseTest ***
    def __init__(self, *args, **kwargs):
        BaseTest.__init__(self, *args, **kwargs)
        self.controller = object()


    def open_url(self):
        print "INIT FROM SUPER CLASS IS "
        print self.Module
        status_code = 200
        self.assertEqual(200,status_code)

test_province.py

from helper import *

class TestProvince(HELPER):
    # *** Calling init function from class HELPER
    def __init__(self, *args, **kwargs):
        HELPER.__init__(self,*args, **kwargs)
        self.Module = 'Province'

    def test_open_url(self):
        self.open_url()

if __name__ == "__main__":
    unittest.main()

After this, I could access any variable inside super class HELPER from sub-class TestProvince.

The cause root of problem could be I might trying to inherit init() function of testCase of class of unittest instead of class HELPER itself. Thanks

Houy Narun
  • 1,557
  • 5
  • 37
  • 86