Skip to content

python: Keep a failed subtest's status when a later skip is reported - #1658

Closed
phr4nky wants to merge 1 commit into
meta:mainfrom
phr4nky:fix-test-main-subtest-status
Closed

phr4nky wants to merge 1 commit into
meta:mainfrom
phr4nky:fix-test-main-subtest-status

Conversation

@phr4nky

@phr4nky phr4nky commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Summary

The default python_test main, prelude/python/tools/__test_main__.py, writes a test as SUCCESS in its -o results when one subtest fails and a later subtest is skipped. This is the usual pattern of a parametrized test that skips unsupported platforms inside subTest. The run itself exits 70, so the per-test result contradicts it. A failed subtest followed by self.skipTest() is written as ASSUMPTION_VIOLATION in the same way.

Affected version

Checked on main at 4b47e14 and on the release 2026-11-15 (5b3b13d); __test_main__.py is identical in both. The subtest-skip case came with 9218a1b ("python: Report subtest skips and failures in the default test main"); the main from before that commit reports the same test as FAILURE.

Reproduction

Put prelude/python/tools/__test_main__.py in an empty directory next to this test module and run it the way python_test does:

# t_sub.py
import unittest


class T(unittest.TestCase):
    def test_platforms(self):
        for platform in ["linux", "windows"]:
            with self.subTest(platform=platform):
                if platform == "windows":
                    self.skipTest("windows not supported")
                self.assertEqual(platform, "macos")
echo 'TEST_MODULES = ["t_sub"]' > __test_modules__.py
python3 __test_main__.py --hide-output -o out.json; echo "exit=$?"
python3 -c "import json; [print(r['testCase'], r['type']) for r in json.load(open('out.json')) if 'testCase' in r]"

Expected

test_platforms FAILURE: unittest counts the failed subtest (wasSuccessful() is false, exit 70), and the main from before 9218a1b reports FAILURE.

Actual

exit=70
test_platforms SUCCESS

Analysis

_setStatus overwrites _current_status and _stacktrace on every call. addSubTest records FAILED for the failing subtest; the subtest branch of addSkip then sets PASSED and clears the stacktrace. unittest calls no further result method for the test, so PASSED is what stopTest writes. A plain addSkip after a failed subtest sets SKIPPED the same way.

Patch

A skip no longer replaces a failure or error already recorded for the same test; its message is still appended.

Test Plan: two new cases in prelude/python/tools/tests/test_main_test.py (a later skipped subtest; skipTest after a failed subtest). Both fail on the parent commit (SUCCESS and ASSUMPTION_VIOLATION instead of FAILURE) and pass with the change; the rest of the module passes. The reproduction above prints test_platforms FAILURE with the change.

@meta-cla

meta-cla Bot commented Nov 25, 2026

Copy link
Copy Markdown

Hi @phr4nky!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

Summary:
The default test main writes each test's status to the `-o` JSON from
the last `_setStatus` call. A test whose subtest fails and whose later
subtest is skipped was written as SUCCESS: the subtest branch of
`addSkip` (added in 9218a1b) sets PASSED over the FAILURE that
`addSubTest` recorded, and clears the stacktrace. A failed subtest
followed by `self.skipTest()` was written as ASSUMPTION_VIOLATION the
same way. unittest counts the failure in both cases and the run exits
70, so the per-test result disagrees with the run.

A skip no longer replaces a failure or error recorded earlier in the
same test; its message is still appended.

Test Plan:
Two new cases in `tests/test_main_test.py`. Both fail on the parent
commit (SUCCESS and ASSUMPTION_VIOLATION instead of FAILURE) and pass
with this change; the rest of the module passes.
@phr4nky
phr4nky force-pushed the fix-test-main-subtest-status branch from e293bb5 to 5506e5f Compare November 25, 2026 21:48
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 25, 2026
@meta-codesync

meta-codesync Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D134594404. (Because this pull request was imported automatically, there will not be any future comments.)

@dtolnay dtolnay left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@meta-codesync meta-codesync Bot closed this in 580490d Nov 28, 2026
@meta-codesync

meta-codesync Bot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in 580490d.

@meta-codesync meta-codesync Bot added the Merged label Nov 28, 2026
meta-codesync Bot pushed a commit to meta/buck2-prelude that referenced this pull request Nov 28, 2026
Summary:
The default `python_test` main, `prelude/python/tools/__test_main__.py`, writes a test as `SUCCESS` in its `-o` results when one subtest fails and a later subtest is skipped. This is the usual pattern of a parametrized test that skips unsupported platforms inside `subTest`. The run itself exits 70, so the per-test result contradicts it. A failed subtest followed by `self.skipTest()` is written as `ASSUMPTION_VIOLATION` in the same way.

## Affected version

Checked on `main` at 4b47e1430e and on the release `2026-11-15` (5b3b13d3ea); `__test_main__.py` is identical in both. The subtest-skip case came with 9218a1bc9c ("python: Report subtest skips and failures in the default test main"); the main from before that commit reports the same test as `FAILURE`.

## Reproduction

Put `prelude/python/tools/__test_main__.py` in an empty directory next to this test module and run it the way `python_test` does:

```python
# t_sub.py
import unittest

class T(unittest.TestCase):
    def test_platforms(self):
        for platform in ["linux", "windows"]:
            with self.subTest(platform=platform):
                if platform == "windows":
                    self.skipTest("windows not supported")
                self.assertEqual(platform, "macos")
```

```
echo 'TEST_MODULES = ["t_sub"]' > __test_modules__.py
python3 __test_main__.py --hide-output -o out.json; echo "exit=$?"
python3 -c "import json; [print(r['testCase'], r['type']) for r in json.load(open('out.json')) if 'testCase' in r]"
```

## Expected

`test_platforms FAILURE`: unittest counts the failed subtest (`wasSuccessful()` is false, exit 70), and the main from before 9218a1bc9c reports `FAILURE`.

## Actual

```
exit=70
test_platforms SUCCESS
```

## Analysis

`_setStatus` overwrites `_current_status` and `_stacktrace` on every call. `addSubTest` records `FAILED` for the failing subtest; the subtest branch of `addSkip` then sets `PASSED` and clears the stacktrace. unittest calls no further result method for the test, so `PASSED` is what `stopTest` writes. A plain `addSkip` after a failed subtest sets `SKIPPED` the same way.

## Patch

A skip no longer replaces a failure or error already recorded for the same test; its message is still appended.

X-link: meta/buck2#1658

Reviewed By: dtolnay

Differential Revision: D134594404

fbshipit-source-id: 7d400704f5f578ca0d957e54ef0d4027503de2bf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants