Added documentation for __name__ feature.

Also moved the document example to be a real test case to make sure
it is also tested (so it is not accidentally broken).
This commit is contained in:
Santeri Paavolainen
2012-09-30 04:23:18 +03:00
parent cc1e5bc1ae
commit 95bc1a6c6f
5 changed files with 66 additions and 22 deletions

4
ddt.py
View File

@@ -26,6 +26,10 @@ def ddt(cls):
For each method decorated with ``@data``, this will effectively create as
many methods as data items are passed as parameters to ``@data``.
The names of the test methods follow the pattern ``test_func_name
+ "_" + str(data)``. If ``data.__name__`` exists, it is used
instead for the test method name.
"""
def feed_data(func, *args, **kwargs):

View File

@@ -6,31 +6,16 @@ and a method decorator ``data`` (for your tests that want to be multiplied).
This allows you to write your tests as:
.. code-block:: python
# tests.py
import unittest
from ddt import ddt, data
from mycode import larger_than_two
@ddt
class FooTestCase(unittest.TestCase):
@data(3, 4, 12, 23)
def test_larger_than_two(self, value):
self.assertTrue(larger_than_two(value))
@data(1, -3, 2, 0)
def test_not_larger_than_two(self, value):
self.assertFalse(larger_than_two(value))
.. literalinclude:: ../test/test_example.py
:language: python
And then run them with::
% nosetests tests.py
........
% nosetests test_example.py
..........
----------------------------------------------------------------------
Ran 8 tests in 0.002s
Ran 10 tests in 0.002s
OK
2 test methods + some *magic* decorators = 8 test cases.
3 test methods + some *magic* decorators = 10 test cases.

2
test/mycode.py Normal file
View File

@@ -0,0 +1,2 @@
def larger_than_two(value):
return value > 2

27
test/test_example.py Normal file
View File

@@ -0,0 +1,27 @@
import unittest
from ddt import ddt, data
from mycode import larger_than_two
class mylist(list):
pass
def annotated(a, b):
r = mylist([a, b])
setattr(r, "__name__", "test_%d_greater_than_%d" % (a, b))
return r
@ddt
class FooTestCase(unittest.TestCase):
@data(3, 4, 12, 23)
def test_larger_than_two(self, value):
self.assertTrue(larger_than_two(value))
@data(1, -3, 2, 0)
def test_not_larger_than_two(self, value):
self.assertFalse(larger_than_two(value))
@data(annotated(2, 1), annotated(10, 5))
def test_greater(self, value):
a, b = value
self.assertGreater(a, b)

View File

@@ -1,5 +1,5 @@
from ddt import ddt, data
from nose.tools import assert_equal
from nose.tools import assert_equal, assert_is_not_none
@ddt
@@ -51,3 +51,29 @@ def test_feed_data():
values.append(method())
assert_equal(set(values), set([1, 2, 3, 4]))
def test_ddt_data_name_attribute():
"""Test the ``__name__`` attribute handling of ``data`` items with ``ddt``"""
def hello():
passo
class myint(int):
pass
class mytest(object):
pass
d1 = myint(1)
d1.__name__ = 'test_d1'
d2 = myint(2)
data_hello = data(d1, d2)(hello)
setattr(mytest, 'test_hello', data_hello)
ddt_mytest = ddt(mytest)
assert_is_not_none(getattr(ddt_mytest, 'test_d1'))
assert_is_not_none(getattr(ddt_mytest, 'test_hello_2'))