def test_typeerror_encodedfile_write(pytester: Pytester) -> None:
    """It should behave the same with and without output capturing (#4861)."""
    p = pytester.makepyfile(
        """
        def test_fails():
            import sys
            sys.stdout.write(b"foo")
    """
    )
    result_without_capture = pytester.runpytest("-s", str(p))
    result_with_capture = pytester.runpytest(str(p))

    assert result_with_capture.ret == result_without_capture.ret
    out = result_with_capture.stdout.str()
    assert ("TypeError: write() argument must be str, not bytes" in out) or (
        "TypeError: unicode argument expected, got 'bytes'" in out
    )
It should behave the same with and without output capturing (#4861).
testing/test_capture.py