我有一个实用程序类,它存储对某些单元测试用例有用的方法。我希望这些辅助方法能够执行asserts / failed / etc.,但似乎我不能使用这些方法,因为他们期望TestCase成为他们的第一个参数......
我希望能够将常用方法存储在测试用例代码之外并继续在其中使用断言,这有可能吗?它们最终用于测试用例代码。
我有类似的东西:
unittest_foo.py:
import unittest
from common_methods import *
class TestPayments(unittest.TestCase):
def test_0(self):
common_method1("foo")
common_methods.py:
from unittest import TestCase
def common_method1():
blah=stuff
TestCase.failUnless(len(blah) > 5)
...
...
套件运行时:
TypeError: unbound method failUnless() must be called with TestCase instance as first argument (got bool instance instead)
答案 0 :(得分:4)
这通常通过多重继承来实现:
common_methods.py:
class CommonMethods:
def common_method1(self, stuff):
blah=stuff
self.failUnless(len(blah) > 5)
...
...
<强> unittest_foo.py:强>
import unittest
from common_methods import CommonMethods
class TestPayments(unittest.TestCase, CommonMethods):
def test_0(self):
self.common_method1("foo")
答案 1 :(得分:1)
听起来你想要这个,至少从错误......
import unittest
from common_methods import *
class TestPayments(unittest.TestCase):
def test_0(self):
common_method1(self, "foo")
def common_method1( self, stuff ):
blah=stuff
self.failUnless(len(blah) > 5)