简体   繁体   English

Python unittest:setUpClass使用非静态方法

[英]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). 我是Python的初学者,开始设计Python的单元测试,在运行测试类之前,我需要向服务器发布一些消息(因为它会搜索它们)。 Thus i need to call a non-static method postMessages() . 因此,我需要调用一个非静态方法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. 目前没有任何方法可以使foo静态化。 How can i instantiate B (or A, for that matter) in postMessages() so i can use it in setUpClass() ? 如何在postMessages()中实例化B(或A),以便可以在setUpClass()中使用它?

After having a read through the __init__ method for TestCase I see that you need to provide a test method name to it. __init__阅读TestCase的__init__方法后,我看到您需要为其提供测试方法名称。 The default is "runTest" which is why that error was popping up. 默认值为“ runTest”,这就是该错误弹出的原因。

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 . 您可以在此处看到它在交互式Python控制台中运行。 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 : 您需要在类中传递有效方法名称的原因可以在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. 真傻。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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