简体   繁体   中英

Python unittest : setUpClass uses a non-static method

I'm quite a beginner in Python and started designing a unit test in Python and i need to post some messages to the server before i run the test class (cause it's gonna search for them). Thus i need to call a non-static method postMessages() .

the stack-trace of the error i'm getting is this-

    Error
Traceback (most recent call last):
  File ".../TestMsgs.py", line 23, in setUpClass
    instance = cls()
  File ".../python2.7/unittest/case.py", line 191, in __init__
    (self.__class__, methodName))
ValueError: no such test method in <class 'TestMsgs.TestMsgs'>: runTest

i have something like this in the code:

class A(object):

    def postMessages(self):
        print "i post messages in the server!"

class B(A):

    @classmethod
    def setUpClass(cls):
        cls.foo()  # should post messages for the tests in the class to work on

There's no option, right now, to make foo static. How can i instantiate B (or A, for that matter) in postMessages() so i can use it in setUpClass() ?

After having a read through the __init__ method for TestCase I see that you need to provide a test method name to it. The default is "runTest" which is why that error was popping up.

import unittest 

class A(unittest.TestCase):

    def postMessages(self):
        print "i post messages in the server!"

class B(A):

    @classmethod
    def setUpClass(cls):
        cls.foo(cls(methodName='test_method')) # should post messages for the tests in the class to work on

    def foo(self):
        self.postMessages()

    def test_method(self):
        pass


B.setUpClass()

You can see it running in an interactive Python console here . It will print out "i post messages in the server!"

The reason you need to pass in a valid method name in the class can be clearly seen in the source code for unittest :

class TestCase: 
    """A class whose instances are single test cases.""" 

    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. 
        """ 
        try: 
           self._testMethodName = methodName 
           testMethod = getattr(self, methodName) 
           self._testMethodDoc = testMethod.__doc__ 
           except AttributeError: 
               raise ValueError, "no such test method in %s: %s" % \ 
                   (self.__class__, methodName) 

If you want to pass in parameters to the method that you have just passed in then you would need to do something like

class A(unittest.TestCase):

    def foo(self, arg1):
        pass

a = A(methodName='foo')
a.foo('an_argument')

But this whole question just feels really wrong. You should refactor rather than have a static method calling an instance method. It's just silly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM