3

I have some similar unit tests in python. There are so similar that only one argument is changing.

class TestFoo(TestCase):
    def test_typeA(self):
        self.assertTrue(foo(bar=TYPE_A))

    def test_typeB(self):
        self.assertTrue(foo(bar=TYPE_B))

    def test_typeC(self):
        self.assertTrue(foo(bar=TYPE_C))

    ...

Obviously this is not very DRY, and if you have even 4-5 different options the code is going to be very repetitive

Now I could do something like this

class TestFoo(TestCase):
    BAR_TYPES = (
        TYPE_A,
        TYPE_B,
        TYPE_C,
        ...
    )

    def _foo_test(self, bar_type):
        self.assertTrue(foo(bar=bar_type))

    def test_foo_bar_type(self):
        for bar_type in BAR_TYPES:
            _foo_test(bar=bar_type))

Which works, however when an exception gets raised, how will I know whether _foo_test failed with argument TYPE_A, TYPE_B or TYPE_C ?

Perhaps there is a better way of structuring these very similar tests?

2
  • 1
    it's OK to be repetitive in tests if it keeps them simple and understandable. You don't want to write tests for your tests do you? Aug 25, 2015 at 8:40
  • Ive read that in a few places that being repetitive in tests is OK. But then its all just python code. The above example is obviously trivial. Sometimes I might have 5 different systems tests in just one test suite for one bar_type and each test could run to 10 lines. So thats 50 lines of code that now has to be copied and pasted X number of times for X number of bar_type's. Then I have to go and edit each 'bar_type` reference by hand. Any any time I add another 'bar_type` i now have to go and copy and paste for hundreds of test just so I can cover another condition. Definitely not DRY.
    – lukeaus
    Aug 25, 2015 at 21:13

2 Answers 2

3

What are you trying to do is essentially a parameterized test. This feature isn't included in standard django or python unittest modules, but a number of libs provide it: nose-parameterized, py.test, ddt

My favorite so far is ddt: it resembles NUnit-JUnit style parameterized tests most, pretty lightweight, don't get in your way and does not require dedicated test runner (like nose-parameterized do). The way it can help you is that it modifies test name to include all parameters, so you would clearly see which test case failed by looking at a test name.

With ddt your example would look like this:

import ddt

@ddt.ddt
class TestProcessCreateAgencyOfferAndDispatch(TestCase):

    @ddt.data(TYPE_A, TYPE_B, TYPE_C)
    def test_foo_bar_type(self, type):
        self.assertTrue(foo(bar=type))

In such case names will look like test_foo_bar_type__TYPE_A (technically, it constructs it something like [test_name]__[repr(parameter_1)]__[repr(parameter_2)]).

As a bonus, it is much cleaner (no helper method), and you get three methods instead of one. The advantage here is that you can test various code paths in a method and get one test case per each path (but a certain amount of thinking is needed, sometimes it's better to have a dedicated test for some of code paths)

2
  • ddt definitely worked. Although I found nose-parameterized is a bit easier for passing in parameters already defined in a tuple. Thanks John.
    – lukeaus
    Aug 26, 2015 at 4:17
  • @luke_aus you're welcome. With ddt you can pass parameters defined in a tuple with @data(*PARAMETERS_TUPLE) (note asterisk).
    – J0HN
    Aug 26, 2015 at 8:51
0

Most TestCase assertion methods, including assertTrue, take an optional msg argument.

If you change your BAR_TYPES tuple to include the variable names, then you can include this in the message that is shown when the assertion fails.

class TestProcessCreateAgencyOfferAndDispatch(TestCase):
    BAR_TYPES = (
        ('TYPE_A', TYPE_A),
        ('TYPE_B', TYPE_B),
        ('TYPE_C', TYPE_C),
        ...
    )

    def _foo_test(self, var_name, bar_type):
        self.assertTrue(foo(bar=bar_type), var_name)

    def test_foo_bar_type(self):
        for (var_name, bar_type) in BAR_TYPES:
            _foo_test(bar=bar_type), var_name=var_name)
2
  • Good idea. The issue for me is that if something fails inside the foo function before the self.assertTrue call then you don't know which bar_type test caused it, you just get an error test_foo_bar_type failed
    – lukeaus
    Aug 25, 2015 at 8:51
  • Good point. Including var_name means you can debug quicker by adding print(var_name) once a test fails, but I agree it's not ideal.
    – Alasdair
    Aug 25, 2015 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.