It's true that there isn't any rich API for the Error/Fail distinction. The
best I can think of would be to create a custom exception eg
FailedAssumption, which is raised when the TestCase.failedException (same as
AssertionError) is caught while testing an assumption.

Here are four ways I can think of implementing the above, each with varying
convenience/aesthetics. A few of these can be added to
django.utils.unittest.TestCase for convenience, or provided as separate
functions/classes to prevent any potential backwards incompatibility or to
just keep TestCase clean.


1. plain old try/except in the test code:

    try:
        self.assertEqual(2,3)
    except self.failedException, e:
        raise FailedAssumption(e.message)


2. wrap tests self.assert* in a special function/method

    self.assumption(self.assertEqual, 2, 3)


    where the "method looks something like this:

    def assumption(self, func, *args, **kwargs):
        try:
            func(*args, **kwargs)
        except self.failureException, e:
            raise FailedAssumption(e.message)


3. wrap a series of test assumptions with state changing methods:

    self.start_assumption_tests()
    self.assertEqual(2, 3)
    self.finish_assumption_tests()


    where the methods look something like this:

    def start_assumption_tests(self):
        self._failureException = self.failureException
        self.failureException = FailedAssumption
    def finish_assumption_tests(self):
        self.failureException = self._failureException


4. use a fancy py2.5 "with" syntax:

    with self.assumptions():
        self.assertEqual(2,3)


    where the start_ and finish_ methods in the previous incarnation are
__enter__ and __exit__



(NB none of these actually have to be in the TestCase class)

Cheers,

Will

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to