我試圖做類似下面的東西: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'),或用這些參數調用基礎構造函數。 –