|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from enum import Enum |
3 | 4 | import sys |
4 | 5 | from typing import Literal |
5 | 6 |
|
| 7 | +from _pytest._io.saferepr import saferepr |
6 | 8 | from _pytest.subtests import SubtestContext |
7 | 9 | from _pytest.subtests import SubtestReport |
8 | 10 | import pytest |
@@ -958,20 +960,52 @@ def test(subtests): |
958 | 960 |
|
959 | 961 |
|
960 | 962 | def test_serialization() -> None: |
| 963 | + """Ensure subtest's kwargs are serialized using `saferepr` (pytest-dev/pytest-xdist#1273).""" |
961 | 964 | from _pytest.subtests import pytest_report_from_serializable |
962 | 965 | from _pytest.subtests import pytest_report_to_serializable |
963 | 966 |
|
| 967 | + class MyEnum(Enum): |
| 968 | + A = "A" |
| 969 | + |
964 | 970 | report = SubtestReport( |
965 | 971 | "test_foo::test_foo", |
966 | 972 | ("test_foo.py", 12, ""), |
967 | 973 | keywords={}, |
968 | 974 | outcome="passed", |
969 | 975 | when="call", |
970 | 976 | longrepr=None, |
971 | | - context=SubtestContext(msg="custom message", kwargs=dict(i=10)), |
| 977 | + context=SubtestContext(msg="custom message", kwargs=dict(i=10, a=MyEnum.A)), |
972 | 978 | ) |
973 | 979 | data = pytest_report_to_serializable(report) |
974 | 980 | assert data is not None |
975 | 981 | new_report = pytest_report_from_serializable(data) |
976 | 982 | assert new_report is not None |
977 | | - assert new_report.context == SubtestContext(msg="custom message", kwargs=dict(i=10)) |
| 983 | + assert new_report.context == SubtestContext( |
| 984 | + msg="custom message", kwargs=dict(i=saferepr(10), a=saferepr(MyEnum.A)) |
| 985 | + ) |
| 986 | + |
| 987 | + |
| 988 | +def test_serialization_xdist(pytester: pytest.Pytester) -> None: |
| 989 | + """Regression test for pytest-dev/pytest-xdist#1273.""" |
| 990 | + pytest.importorskip("xdist") |
| 991 | + pytester.makepyfile( |
| 992 | + """ |
| 993 | + from enum import Enum |
| 994 | + import unittest |
| 995 | +
|
| 996 | + class MyEnum(Enum): |
| 997 | + A = "A" |
| 998 | +
|
| 999 | + def test(subtests): |
| 1000 | + with subtests.test(a=MyEnum.A): |
| 1001 | + pass |
| 1002 | +
|
| 1003 | + class T(unittest.TestCase): |
| 1004 | +
|
| 1005 | + def test(self): |
| 1006 | + with self.subTest(a=MyEnum.A): |
| 1007 | + pass |
| 1008 | + """ |
| 1009 | + ) |
| 1010 | + result = pytester.runpytest("-n1", "-pxdist.plugin") |
| 1011 | + result.assert_outcomes(passed=2) |
0 commit comments