Skip to content

subtests: inconcistent handling of non-string messages #14195

@radoering

Description

@radoering
  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible

pytest: 9.0.2
operating system: any

When calling self.subTest(msg) with a non-string msg, the message is not printed when run via pytest.
In contrast, it is printed when run with unittest.
It is also printed when using the subtests fixture in a pytest test.

Example:

import unittest

class T(unittest.TestCase):
    def test_int_msg(self):
        with self.subTest(42):
            assert False, "subtest failure"

if __name__ == '__main__':
    unittest.main()

Run with unittest - message is printed:

FAIL: test_int_msg (__main__.T.test_int_msg) [42]

Run with pytest - message is replaced with <subtest>:

SUBFAILED(<subtest>) tests/subtest/test_subtest.py::T::test_int_msg - AssertionError: subtest failure

In contrast:

def test_int_msg(subtests):
    with subtests.test(42):
        assert False, "subtest failure"

Run with pytest - message is printed:

SUBFAILED[42] tests/subtest/test_subtest3.py::test_int_msg - AssertionError: subtest failure

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions