Marking a subtest as an expected failure

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Jun 21 04:28:25 EDT 2016


I'm using unittest and the subtest context manager to run some tests:


def test_spam(self):
    for i in range(100):
        for j in range(100):
            with self.subtest(i=i, j=j):
                self.assertEqual(spam(i, j), 999)


Now, it turns out that spam(i, j) == 999 for all i, j *except* one or two
exceptional cases. E.g. let's say spam(97, 83) != 999. And further, this
is not a bug to be fixed, but an expected failure. I can write:

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?
I don't want the entire test_spam test to be counted as an expected failure, 
just that one subtest.

https://docs.python.org/3/library/unittest.html#distinguishing-test-iterations-using-subtests





-- 
Steve




More information about the Python-list mailing list