@@ -370,6 +370,36 @@ def test_foo(subtests):
370370 )
371371
372372
373+ def test_msg_not_a_string (
374+ pytester : pytest .Pytester , monkeypatch : pytest .MonkeyPatch
375+ ) -> None :
376+ """
377+ Using a non-string in subtests.test() should still show it in the terminal (#14195).
378+
379+ Note: this was not a problem originally with the subtests fixture, only with TestCase.subTest; this test
380+ was added for symmetry.
381+ """
382+ monkeypatch .setenv ("COLUMNS" , "120" )
383+ pytester .makepyfile (
384+ """
385+ def test_int_msg(subtests):
386+ with subtests.test(42):
387+ assert False, "subtest failure"
388+
389+ def test_no_msg(subtests):
390+ with subtests.test():
391+ assert False, "subtest failure"
392+ """
393+ )
394+ result = pytester .runpytest ()
395+ result .stdout .fnmatch_lines (
396+ [
397+ "SUBFAILED[[]42[]] test_msg_not_a_string.py::test_int_msg - AssertionError: subtest failure" ,
398+ "SUBFAILED(<subtest>) test_msg_not_a_string.py::test_no_msg - AssertionError: subtest failure" ,
399+ ]
400+ )
401+
402+
373403@pytest .mark .parametrize ("flag" , ["--last-failed" , "--stepwise" ])
374404def test_subtests_last_failed_step_wise (pytester : pytest .Pytester , flag : str ) -> None :
375405 """Check that --last-failed and --step-wise correctly rerun tests with failed subtests."""
@@ -619,6 +649,33 @@ def test_foo(self):
619649 "SUBSKIPPED[[]subtest 1[]] [[]1[]] *.py:*: skip subtest 1"
620650 )
621651
652+ def test_msg_not_a_string (
653+ self , pytester : pytest .Pytester , monkeypatch : pytest .MonkeyPatch
654+ ) -> None :
655+ """Using a non-string in TestCase.subTest should still show it in the terminal (#14195)."""
656+ monkeypatch .setenv ("COLUMNS" , "120" )
657+ pytester .makepyfile (
658+ """
659+ from unittest import TestCase
660+
661+ class T(TestCase):
662+ def test_int_msg(self):
663+ with self.subTest(42):
664+ assert False, "subtest failure"
665+
666+ def test_no_msg(self):
667+ with self.subTest():
668+ assert False, "subtest failure"
669+ """
670+ )
671+ result = pytester .runpytest ()
672+ result .stdout .fnmatch_lines (
673+ [
674+ "SUBFAILED[[]42[]] test_msg_not_a_string.py::T::test_int_msg - AssertionError: subtest failure" ,
675+ "SUBFAILED(<subtest>) test_msg_not_a_string.py::T::test_no_msg - AssertionError: subtest failure" ,
676+ ]
677+ )
678+
622679
623680class TestCapture :
624681 def create_file (self , pytester : pytest .Pytester ) -> None :
0 commit comments