Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

decorators.py 1.0KB

12345678910111213141516171819202122
  1. def dataProvider(fn_data_provider):
  2. """
  3. Data provider decorator, allows another callable to provide the data for the test.
  4. This is a nice feature from PHPUnit which is very useful. Am sticking with the JUnit style
  5. naming as unittest does this already.
  6. Implementation based on http://melp.nl/2011/02/phpunit-style-dataprovider-in-python-unit-test/#more-525
  7. """
  8. def test_decorator(test_method):
  9. def execute_test_method_with_each_data_set(self):
  10. for data in fn_data_provider():
  11. if len(data) == 2 and isinstance(data[0], tuple) and isinstance(data[1], dict):
  12. # Both args and kwargs being provided
  13. args, kwargs = data[:]
  14. else:
  15. args, kwargs = data, {}
  16. try:
  17. test_method(self, *args, **kwargs)
  18. except AssertionError, e:
  19. self.fail("%s (Provided data: %s, %s)" % (e, args, kwargs))
  20. return execute_test_method_with_each_data_set
  21. return test_decorator