我试图做类似下面的东西:DI与应用程序中从别处调用的单元测试?
import unittest
class foo:
one = 1
two = 1
class bar:
one = 2
two = 2
class my_test(unittest.TestCase):
def __init__(self, di_source):
self.di = di_source
print 'initing my_test'
def setUp(self):
print 'setting up!'
def tearDown(self):
print 'tearing down :('
def test_case_one(self):
self.assertEqual(self.di.one,1)
def test_case_two(self):
self.assertEqual(self.di.two, 2)
di_one = foo()
di_two = bar()
# called from elsewhere in my application
test_one = my_test(di_one).run()
test_one = my_test(di_two).run()
我的目标是:
- 能够调用
run()
上一个测试套件 - 提供一个DI容器在运行时测试套件
- 利用
setUp
和tearDown
功能提供的单元测试框架
但是,似乎当我试图做到这一点的单元测试框架不喜欢我的构造函数:
AttributeError: 'my_test' object has no attribute '_testMethodName'
是否有更好的方法来组织这个例子来避免这个问题?
你的例子强调了你如何用错误的参数调用TestCase。您可以使用方法名称来调用它,而不是自定义对象。 使用my_test('test_case_one'),或用这些参数调用基础构造函数。 –