我想爲TestCase派生類的變體創建一個生成器。鼻子:基於TestCase的類的生成器
我想這是什麼:
import unittest
def create_class(param):
class Test(unittest.TestCase):
def setUp(self):
pass
def test_fail(self):
assert False
return Test
def test_basic():
for i in range(5):
yield create_class(i)
我得到的是這樣的:
======================================================================
ERROR: test_1.test_basic
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3.3/site-packages/nose/case.py", line 268, in setUp
try_run(self.test, names)
File "/usr/lib/python3.3/site-packages/nose/util.py", line 478, in try_run
return func()
TypeError: setUp() missing 1 required positional argument: 'self'
屈服實例,而不是類(yield create_class(i)()
)給我留下了這個錯誤:
======================================================================
ERROR: test_1.test_basic
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3.3/site-packages/nose/case.py", line 198, in runTest
self.test(*self.arg)
File "/usr/lib/python3.3/unittest/case.py", line 492, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python3.3/unittest/case.py", line 423, in run
testMethod = getattr(self, self._testMethodName)
AttributeError: 'Test' object has no attribute 'runTest'
有什麼建議嗎?
如果我在'Test'中有多個方法,該怎麼辦?明確地傳遞這些信息似乎是對DRY原則的一大破壞...... – dom0 2013-03-07 21:33:38
@ dom0我認爲你應該仔細閱讀關於[測試生成器]的鼻子文檔(https://nose.readthedocs.org/en/latest/writing_tests.html#test -generators)。你在混合東西。 'TestCase's *不是*被設計用於這種方式,顯然這導致次優代碼。 – Bakuriu 2013-03-07 21:38:03