Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python unit test expectedFailureIf

I may be blind and missing something in the Python Unit Test FrameWork (Python 2.7.10). I'm trying to mark a class as an expected failure but only if the class is run on Windows. Other platforms work correctly. So the basic concept would be:

@unittest.expectedFailureIf(sys.platform.startswith("win"), "Windows Fails")
class MyTestCase(unittest.TestCase):
    # some class here
like image 242
Keith Avatar asked Aug 23 '26 01:08

Keith


1 Answers

As mentioned, neither Python 2 nor Python 3 (as at 3.8) have this built in.

You can pretty easily create this yourself, however, by defining it at the top of your file:

def expectedFailureIf(condition):
    """The test is marked as an expectedFailure if the condition is satisfied."""
    def wrapper(func):
        if condition:
            return unittest.expectedFailure(func)
        else:
            return func
    return wrapper

Then you can do essentially as you suggest (I have not added reason, as that isn't in the existing expectedFailure):

class MyTestCase(unittest.TestCase):
    # some class here

    @expectedFailureIf(sys.platform.startswith("win"))
    def test_known_to_fail_on_windows_only(self):
like image 151
Aaron Avatar answered Aug 25 '26 15:08

Aaron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!