osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Marking a subtest as an expected failure


Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:

> def test_spam(self):
>     for i in range(100):
>         for j in range(100):
>             with self.subtest(i=i, j=j):
>                 if (i, j) != (97, 83):
>                     self.assertEqual(spam(i, j), 999)
>
>
> but is there a nicer way to mark a specific subtest as an expected
> failure?

Expected failures and subtests are apparently not easily mixed using the
current API.

Digging into the ?unittest.case? module, the ?expectedFailure? decorator
does its job by setting the test case's ?__unittest_expecting_failure__?
attribute to ?True?.

Perhaps you can get the subtest object from the context manager, and
decide to set that magic attribute::

    # ?
        with self.subtest(i=i, j=j) as subtest:
            if (i, j) == (97, 83):
                # The inhabitants of (97, 83) have always been trouble.
                subtest.__unittest_expecting_failure__ = True
            self.assertEqual(spam(i, j), 999)

That attribute is undocumented though, so I don't know whether to
consider that a nasty hack or a clever one. (Nor do I know whether it
works as I expect.)

You would be justified to report a bug that this interaction of useful
features should really be easier to access.

-- 
 \     ?Try adding ?as long as you don't breach the terms of service ? |
  `\          according to our sole judgement? to the end of any cloud |
_o__)                      computing pitch.? ?Simon Phipps, 2010-12-11 |
Ben Finney