我有一个实用工具类,它存储对某些单元测试用例有用的方法。我希望这些助手方法能够进行断言/失败/等等,但我似乎不能使用这些方法,因为它们期望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)
发布于 2009-09-26 01:08:51
听起来你想要这个,至少从错误中.
unittest_foo.py:
import unittest
from common_methods import *
class TestPayments(unittest.TestCase):
def test_0(self):
common_method1(self, "foo")common_methods.py:
def common_method1( self, stuff ):
blah=stuff
self.failUnless(len(blah) > 5)发布于 2009-09-26 02:48:11
这通常是通过多重继承实现的:
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")https://stackoverflow.com/questions/1480144
复制相似问题