You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

decorators.py 1.1KB

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