classification
Title: TestCase.subTest and expectedFailure
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, louielu, michael.foord, rbcollins, ronaldoussoren
Priority: low Keywords:

Created on 2017-07-23 10:47 by ronaldoussoren, last changed 2017-07-23 15:04 by louielu.

Messages (5)
msg298895 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2017-07-23 10:47
It would be nice if there were a way to mark a TestCase.subTest as an expected failure.

I have a number of testcases that use the subTest feature and where a small subset of those tests are expected failures due to platform issues. It would be nice if it were possible to mark those tests as such. 

I'm currently using self.skipTest() to mark these subTests as special, but that's less than ideal.
msg298898 - (view) Author: Louie Lu (louielu) * Date: 2017-07-23 11:11
So for example:


    for i in range(10):
        with self.subTest(i=i):
            DoSomething(i)

we have some platform specific in i=8, i=9, and you want to labeled as expectedFailure. Do I miss something?
msg298899 - (view) Author: Louie Lu (louielu) * Date: 2017-07-23 12:07
There is a mailing-list discusss this before:

https://mail.python.org/pipermail/python-list/2016-June/710575.html


But after checking, current subTest contextmanager won't yield subtest, so the method of with self.subTest() as sub won't work.

Another problem is, inside the subTest, if there have expectfailed, it will raise _ShouldStop, causing the after test won't run it.
msg298903 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2017-07-23 12:50
That's correct, I have a test like:


def test_something(self):

    for info in CASES:
       with self.subTest(info):
          self.assert_something(info)

For some values of 'info' the test is known to fail and I want to mark those as exptectedFailure somehow and there doesn't appear to be a way to do so right now.

I'm currently doing:
    with self.subTest(info):
       try:
           ... # actual test

       except:
           if info in KNOWN_FAILURES:
               self.skipTest()
           raise

That suppresses the test failures, but in an unclean way and without getting a warning when the testcase starts working again.

I could generate testcases manually the old fashioned way without using subTest, but that results in more complicated test code and requires rewriting a number of tests.

One possible design for making it possible to mark subTests as known failures it to return a value in the subTest context manager that has a method for marking the subTest:

     with self.subTest(...) as tc:
        if ...:
           tc.expectedFailure(...)
 
        ... # actual test

I don't know how invasive such a change would be.
msg298906 - (view) Author: Louie Lu (louielu) * Date: 2017-07-23 15:04
I'm successful to add a sub.expectedFailure() to subTest, but in current API, it isn't that will done.

First, you will not get any alarm or information that there is a  test been treat as "expected failure" at the result text.

Second, it just act like you putting @unittest.expectedFailure at the top of the test.

Third, current result didn't put expected failure as a result category, so it will not show it at the result if we skip some test in the subTest, and it will need to changed to add an expected failure list to handle multiple expected failure in a test.
History
Date User Action Args
2017-07-23 15:04:54louielusetmessages: + msg298906
2017-07-23 12:50:56ronaldoussorensetmessages: + msg298903
2017-07-23 12:07:14louielusetmessages: + msg298899
2017-07-23 11:11:04louielusetnosy: + louielu
messages: + msg298898
2017-07-23 10:47:19ronaldoussorencreate