Make your tests self-documenting

14 02 2007

Inspired by RSpec (which in turn was inspired by TestDox).

A piece of code that transforms standard unittest output:

test_doesnt_raise_and_exception_when_None_was_passed (test_webui.TestSomeArbitraryMethod) ... ok
test_returns_this_and_that_when_string_was_passed (test_webui.TestSomeArbitraryMethod) ... ok


Some arbitrary method doesn't raise and exception when None was passed ... ok
Some arbitrary method returns this and that when string was passed ... ok

Test case that generated output above looks like this:

class TestSomeArbitraryMethod(TestCaseWithSpec):
    def test_returns_this_and_that_when_string_was_passed(self):
        assert "this and that" == some_arbitrary_method("string")

    def test_doesnt_raise_and_exception_when_None_was_passed(self):
            assert False

If you like it, grab the source code of TestCaseWithSpec.



4 responses

15 02 2007
Titus Brown

hmm, nose used to print the docstrings out, which would IMO be a better way of doing this. can’t figure out how to make nose do that now, at least not in 5 minutes. More later.

15 02 2007

Yeah, it prints the docstring, but it still looks suboptimal:

(test_file_name.TestClassName) “Test method docstring.”

I like TestDox-style output because it reads like a specification, not like a test output. And you don’t have to repeat the context name many times (docstrings can’t be inherited and extended, so it is non-trivial to solve this). On error you get a traceback, so there’s no problem in determining in which file test was defined.

I use RSpec for some time now and can ensure you that it causes no problems, while you got an agile documentation for free.

What would be really cool is nose using TestDox-style output for all tests it runs. This way you’ll get best of both worlds (no need to inherit from unittest.TestCase and automatic specs based on test cases).

16 02 2007
Titus Brown

I’m guessing that could be easily arranged ;)

16 02 2007

In fact I have already started a related discussion on nose-users mailing list:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: