【发布时间】:2011-09-27 00:04:54
【问题描述】:
我有一些基本的设置/拆卸代码,我想在一大堆单元测试中重用它们。所以我有了创建一些派生类以避免在每个测试类中重复代码的好主意。
这样做时,我收到了两个奇怪的错误。一,我解决不了。这是一个无法解决的问题:
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)
对此的“解决方案”是将单词“args”添加到派生类中的__init__ 函数中。有什么办法可以避免吗?
记住,我有两个错误。
【问题讨论】:
-
你为什么要覆盖
__init__()?一般来说,我们会覆盖setUp。
标签: python unit-testing