unittest.TestCase gets `TypeError: Error when calling the metaclas bases __init__() takes exactly 2 arguments (4 given)`

StackOverflow https://stackoverflow.com//questions/25066836

  •  23-12-2019
  •  | 
  •  

Question

I am trying to add attributes to a unittest TestCase, but I keep getting the following error. TypeError: Error when calling the metaclas bases __init__() takes exactly 2 arguments (4 given) I am running the tests with nose.

But I am passing only one argument. self makes it two. So I have no idea where the 3rd and 4th arguments are coming from. Is there something in unittest I should be aware of? or nose?

Base_Test.py

import unittest
from logUtils import logger

class Base_Test(unittest.TestCase):
    def __init__(self, p4rev):
        self.p4rev = p4rev

    def setUp(self):
        logger.critical(p4rev)
        triggerSomething(p4rev)

My_test.py

from Base_Test import Base_Test

rev = '12345'

class check_error_test(Base_Test(rev)):
    dosomething()
Was it helpful?

Solution

When inheriting from a sub class from a supper class the python syntax comes as,

class SubClassName(SupperClassName): #Sub class body

But in your coding,

class check_error_test(Base_Test(rev)):
    dosomething()

you are parsing an instance of Base_Test class, but not the class. Base_Test(rev) will create an instance of Base_Test class.

You can modify your code as bellow.

class check_error_test(Base_Test):
    def __init__(p4rev):
        super(check_error_test).__init__(p4rev)

Base_Test is also a class that inherits from unittest.TestCase, so you need to add to Base_Test.py:

class Base_Test(unittest.TestCase):
    def __init__(self, methodName='runTest', p4rev=None):
        super(Base_Test, self).__init__(methodName)
        self.p4rev = p4rev

    def runTest(self):
        pass

the runTest() was added there in the BaseTest class just to avoid the ValueError raise by the unittest.TestCase class. unittest.TestCase raises this error if there is no method to be found by the name passed to methodName parameter.

Bellow is the code segment which raises the ValueError which is found in the unittest.TestCase.init()

def __init__(self, methodName='runTest'):
        """Create an instance of the class that will use the named test
           method when executed. Raises a ValueError if the instance does
           not have a method with the specified name.
        """
        self._testMethodName = methodName
        self._resultForDoCleanups = None
        try:
            testMethod = getattr(self, methodName)
        except AttributeError:
            raise ValueError("no such test method in %s: %s" %
                  (self.__class__, methodName))

OTHER TIPS

Try this out:

from Base_Test import Base_Test

rev = '12345'

class check_error_test(Base_Test):
    def __init__(self):
        super(check_error_test, self).__init__(rev)

    dosomething()

It wasn't working because you were instantiating Base_Test while setting up the inheritance for check_error_test. There's a good section in the Python documentation on super() as well, if you'd like to know more about how that works.

In addition, I would also recommend capitalizing the class name to CheckErrorTest and changing Base_Test to BaseTest, as per the pep8 guidelines.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top