我有一個非常大的測試套件,我裝飾了一些test_ *函數。現在我不能通過./test.py MySqlTestCase.test_foo_double
給他們打電話,python3.2抱怨:ValueError: no such test method in <class '__main__.MySqlTestCase'>: result
。我的裝飾代碼如下所示:python unittest:不能調用裝飾測試
def procedure_test(procedure_name, arguments_count, returns):
'''Decorator for procedure tests, that simplifies testing whether procedure
with given name is available, whether it has given number of arguments
and returns given value.'''
def decorator(test):
def result(self):
procedure = self.db.procedures[self.case(procedure_name)]
self.assertEqual(len(procedure.arguments), arguments_count)
self.assertEqual(procedure.returns,
None if returns is None else self.case(returns))
test(self, procedure)
return result
return decorator
和測試方法:
@procedure_test('foo_double', 0, 'integer')
def test_foo_double(self, procedure):
self.assertEqual(procedure.database, self.db)
self.assertEqual(procedure.sql, 'RETURN 2 * value')
self.assertArguments(procedure, [('value', 'int4')])
不錯,很高興知道,謝謝。 – gruszczy 2011-11-17 11:53:35
如果我想裝飾很多測試,該怎麼辦? – pt12lol 2016-09-02 11:16:50