Permalink
Browse files

add data_provider annotation

  • Loading branch information...
1 parent 42acae4 commit 620050cd86c5fa2d9236ef5c94e5d6f5680d5b75 @Eyjafjallajokull committed Feb 9, 2013
Showing with 37 additions and 5 deletions.
  1. +14 −5 ddt.py
  2. +23 −0 example.py
View
19 ddt.py
@@ -2,8 +2,7 @@
__version__ = '0.2.0'
-MAGIC = '%values' # this value cannot conflict with any real python attribute
-
+VALUES_ATTRIBUTE_NAME = '%values' # this value cannot conflict with any real python attribute
def data(*values):
"""
@@ -12,10 +11,20 @@ def data(*values):
Should be added to methods of instances of ``unittest.TestCase``.
"""
def wrapper(func):
- setattr(func, MAGIC, values)
+ setattr(func, VALUES_ATTRIBUTE_NAME, values)
return func
return wrapper
+def data_provider(provider, *args):
+ """
+ Method decorator to add to your test methods.
+
+ Should be added to methods of instances of ``unittest.TestCase``.
+ """
+ def wrapper(func):
+ setattr(func, VALUES_ATTRIBUTE_NAME, provider(*args))
+ return func
+ return wrapper
def ddt(cls):
"""
@@ -42,9 +51,9 @@ def wrapper(self):
return wrapper
for name, f in cls.__dict__.items():
- if hasattr(f, MAGIC):
+ if hasattr(f, VALUES_ATTRIBUTE_NAME):
i = 0
- for v in getattr(f, MAGIC):
+ for v in getattr(f, VALUES_ATTRIBUTE_NAME):
test_name = getattr(v, "__name__", "{0}_{1}".format(name, v))
setattr(cls, test_name, feed_data(f, v))
i = i + 1
View
@@ -0,0 +1,23 @@
+# tests.py
+import unittest
+from ddt import ddt, data, data_provider
+
+class DataProviders(object):
+ def larger_then_two(self, count=4):
+ return [3, 4, 5, 6, 7, 8][0:count]
+_data_providers = DataProviders()
+
+@ddt
+class FooTestCase(unittest.TestCase):
+
+ @data(3, 4, 5, 6)
+ def test_data_annotation(self, value):
+ self.assertTrue(value > 2)
+
+ @data_provider(_data_providers.larger_then_two, 2)
+ def test_data_provider_annotation(self, value):
+ self.assertTrue(value > 2)
+
+if __name__ == '__main__':
+ suite = unittest.TestLoader().loadTestsFromTestCase(FooTestCase)
+ unittest.TextTestRunner(verbosity=2).run(suite)

0 comments on commit 620050c

Please sign in to comment.