[TIP] Generating Somewhat Meaningful Error Messages

Travis Parks jehugaleahsa at gmail.com
Thu Aug 9 16:45:06 PDT 2012


Hello,

We have a somewhat interesting opportunity to build meaningful error
messages whenever a test fails.
The way we write tests, we have a single fixture per test class. We are
naming our classes things like:
WhenSomethingIsTrue, and we are naming our functions like:
should_cause_something_to_happen.
I'd like to know if there is a way, after a test method runs, whether to
see whether it failed and to then
look at the name of the class (and its sub-classes) and the function to
spit out a short story about what
the context of the failure was. With my example:

When something is true.
It should cause something to happen.

In our particular environment, we often will create a subclass for each
branch through the code. While
this is a lot of work, it makes it easier to limit the number of assertions
in each test method. It also
makes naming like this possible. The good thing is all these subclasses are
the result of verifying
different behavior, not just because their's an if/else statement.
Nevertheless, these hierarchies can
get quite long and it becomes difficult to ascertain the context in which
the failure occurred. If we
could get a short printout saying the conditions, it would make fixing the
bug a no-brainer most of
the time. The good thing is we already are inheriting from a subclass of
unittest.TestCase, called
UnitTestCase. If there was a way to achieve this functionality, I could
slip it in there and no one would
need to change a line of code. This could be a major productivity booster.

Thanks for any feedback,
Travis Parks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20120809/2d70a2e2/attachment.htm>


More information about the testing-in-python mailing list