010*_*101 26 python unit-testing
我有一些基本的设置/拆卸代码,我想在一大堆单元测试中重用.所以我明白了创建一些派生类以避免在每个测试类中重复代码.
这样做,我收到了两个奇怪的错误.一,我无法解决.这是无法解决的问题:
AttributeError: 'TestDesktopRootController' object has no attribute '_testMethodName'
Run Code Online (Sandbox Code Playgroud)
这是我的基类:
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)
Run Code Online (Sandbox Code Playgroud)
这是我的派生类:
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/')
Run Code Online (Sandbox Code Playgroud)
另一个奇怪的错误是:
TypeError: __init__() takes exactly 1 argument (2 given)
Run Code Online (Sandbox Code Playgroud)
对此的"解决方案"是__init__在派生类中向我的函数添加单词"args" .有什么办法可以避免吗?
请记住,我在这一个中有两个错误.
Gre*_*ard 61
这是因为你__init__()错误地覆盖了.几乎可以肯定,你根本不想覆盖__init__(); 你应该做的一切setUp().我已经使用unittest了10年以上,我认为我没有超越过__init__().
但是,如果您确实需要覆盖__init__(),请记住您无法控制构造函数的调用位置 - 框架会为您调用它.所以你必须提供一个可以调用的签名.从源代码(unittest/case.py),该签名是:
def __init__(self, methodName='runTest'):
Run Code Online (Sandbox Code Playgroud)
这样做的安全方法是接受任何参数,然后将它们传递给基类.这是一个有效的实现:
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13119 次 |
| 最近记录: |