Hacker News new | past | comments | ask | show | jobs | submit login

My problem is more that I never know what to test. I mean, suppose I have some nontrivial code -- one of my latest random projects contains this:

    def permute(tpl):
        """Gives the set of valid permutations for this tuple."""
        if len(tpl) == 0:
            yield ()
        else:
            for t in permute(tpl[1:]):
                for n in range(0, len(t) + 1):
                    yield t[0:n] + (tpl[0],) + t[n:]
It's a recursive generator-driven permutation engine. Technically I suppose the order of the permutations doesn't matter, so long as they are all distinct and there are n! of them. Is that what I should be testing? That is, should my test code read:

    permute_test = tuple(permute((1, 2, 3, 4)))
    assert(len(permute_test) == 24)
    for i in range(0, 24):
        assert(len(permute_test[i]) == 4)

    for i in range(0, 23):
        for j in range(i + 1, 24):
            assert(permute_test[i] != permute_test[j])
...? And if so, how does that help me write the original function? Or am I supposed to test a base case and one or two recursion steps, so that it helps me write the function, but "hard-wires" a particular order?



You're right to want to avoid duplicating your code inside your test. In this case, I'd work out some simple permutations and test against those values, sorting the results so order doesn't matter.

    assert(sorted(permute((1, 2))) == [(1, 2), (2, 1)])


...so that it helps me write the function, but "hard-wires" a particular order?

Use assertItemsEqual.

http://docs.python.org/library/unittest.html#unittest.TestCa...

The way I do it:

In the doctest, I include a few toy examples, stuff I can verify by hand, and which helps the user understand what I'm doing.

In the unit tests, I try to do things in the style of Haskell's Quickcheck (the greatest testing library out there). Generate some random values and test properties (e.g., len(permute(x)) == factorial(len(x))).


You have a good point about TDD, and I'm unsure how to address it.

However, you might find the itertools library helpful, particularly itertools.permutations(): http://docs.python.org/library/itertools.html#itertools.perm...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: