Skip to content

Unittest unexpected success should be translated to XPASS #7393

Closed
@bluetech

Description

@bluetech

pytest version: 5.4.x & current master, Python version: 3.8

xfail background

pytest's unittest plugin tries to translate unittest concepts to pytest concept. One concept in pytest is xfail/XFAIL/XPASS (see docs).

Example:

import pytest

@pytest.mark.xfail
def test_expected_failure():
    assert False

@pytest.mark.xfail
def test_unexpected_success():
    assert True

running:

x.py::test_expected_failure XFAIL
x.py::test_unexpected_success XPASS

also, if running with strict xfail, this becomes

x.py::test_expected_failure XFAIL
x.py::test_unexpected_success FAILED

============== FAILURES =============
______ test_unexpected_success ______
[XPASS(strict)] 

unittest background

unittest also has expected-failure/unexpected-success support:

import unittest

class ExpectedFailureTestCase(unittest.TestCase):
    @unittest.expectedFailure
    def test_expected_failure(self):
        self.assertTrue(False)

    @unittest.expectedFailure
    def test_unexpected_success(self):
        self.assertTrue(True)

running:

xu
----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (expected failures=1, unexpected successes=1)
ran@ran:~/src/pytest$ py -m unittest -v ut.py 
test_expected_failure (ut.ExpectedFailureTestCase) ... expected failure
test_unexpected_success (ut.ExpectedFailureTestCase) ... unexpected success

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (expected failures=1, unexpected successes=1)

pytest unittest plugin

Given the correspondence described above, when running the unittest under pytest I would expect test_expected_failure to be translated to an XFAIL -- this happens, and test_unexpected_success to be translated to an XPASS -- this doesn't happen:

ut.py::ExpectedFailureTestCase::test_expected_failure XFAIL                     [ 50%]
ut.py::ExpectedFailureTestCase::test_unexpected_success FAILED                  [100%]

====================================== FAILURES =======================================
___________________ ExpectedFailureTestCase.test_unexpected_success ___________________
Unexpected success
=============================== short test summary info ===============================
FAILED ut.py::ExpectedFailureTestCase::test_unexpected_success
============================ 1 failed, 1 xfailed in 0.13s =============================

To preserve the unittest behavior of unexpected-success being considered a failure, pytest should probably translate to xfail strict=True.

Implementation notes

The current behavior is implemented in the skipping and unittest plugins using a unexpectedsuccess_key hack. Ideally this hack would be removed in favor of using straight xfail, this would also be a good code cleanup. Not sure how hard that would be to achieve.

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: unittestrelated to the unittest integration builtin plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions