[英]Deriving a class from TestCase throws two errors
我有一些基本的设置/拆卸代码,我想在一大堆单元测试中重用。 所以我明白了创建一些派生类以避免在每个测试类中重复代码。
这样做,我收到了两个奇怪的错误。 一,我无法解决。 这是无法解决的问题:
AttributeError: 'TestDesktopRootController' object has no attribute '_testMethodName'
这是我的基类:
import unittest
import twill
import cherrypy
from cherrypy._cpwsgi import CPWSGIApp
class BaseControllerTest(unittest.TestCase):
def __init__(self):
self.controller = None
def setUp(self):
app = cherrypy.Application(self.controller)
wsgi = CPWSGIApp(app)
twill.add_wsgi_intercept('localhost', 8080, lambda : wsgi)
def tearDown(self):
twill.remove_wsgi_intercept('localhost', 8080)
这是我的派生类:
import twill
from base_controller_test import BaseControllerTest
class TestMyController(BaseControllerTest):
def __init__(self, args):
self.controller = MyController()
BaseControllerTest.__init__(self)
def test_root(self):
script = "find 'Contacts'"
twill.execute_string(script, initial_url='http://localhost:8080/')
另一个奇怪的错误是:
TypeError: __init__() takes exactly 1 argument (2 given)
对此的“解决方案”是在派生类中向我的__init__
函数添加单词“args”。 有什么办法可以避免吗?
请记住,我在这一个中有两个错误。
这是因为你错误地覆盖了__init__()
。 几乎可以肯定,你根本不想覆盖__init__()
; 你应该在setUp()
做所有事情。 我一直在使用unittest
超过10年而且我认为我没有覆盖__init__()
。
但是,如果您确实需要覆盖__init__()
,请记住您不能控制构造函数的调用位置 - 框架会为您调用它。 所以你必须提供一个可以调用的签名。 从源代码( unittest/case.py
),该签名是:
def __init__(self, methodName='runTest'):
这样做的安全方法是接受任何参数,然后将它们传递给基类。 这是一个有效的实现:
class BaseTest(unittest.TestCase):
def __init__(self, *args, **kwargs):
unittest.TestCase.__init__(self, *args, **kwargs)
def setUp(self):
print "Base.setUp()"
def tearDown(self):
print "Base.tearDown()"
class TestSomething(BaseTest):
def __init__(self, *args, **kwargs):
BaseTest.__init__(self, *args, **kwargs)
self.controller = object()
def test_silly(self):
self.assertTrue(1+1 == 2)
在BaseController
的__init__
你需要像在TestMyController
一样调用unittest.TestCase
的__init__
。
从框架构造TestCase的调用可能是传递参数。 处理此类以获取类的最佳方法是:
class my_subclass(parentclass):
def __init__(self, *args, **kw):
parentclass.__init__(self, *args, **kw)
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.