@@ -922,11 +922,13 @@ def __init__(
922
922
captureclass : type [CaptureBase [AnyStr ]],
923
923
request : SubRequest ,
924
924
* ,
925
+ config : dict [str , Any ] | None = None ,
925
926
_ispytest : bool = False ,
926
927
) -> None :
927
928
check_ispytest (_ispytest )
928
929
self .captureclass : type [CaptureBase [AnyStr ]] = captureclass
929
930
self .request = request
931
+ self ._config = config if config else {}
930
932
self ._capture : MultiCapture [AnyStr ] | None = None
931
933
self ._captured_out : AnyStr = self .captureclass .EMPTY_BUFFER
932
934
self ._captured_err : AnyStr = self .captureclass .EMPTY_BUFFER
@@ -935,8 +937,8 @@ def _start(self) -> None:
935
937
if self ._capture is None :
936
938
self ._capture = MultiCapture (
937
939
in_ = None ,
938
- out = self .captureclass (1 ),
939
- err = self .captureclass (2 ),
940
+ out = self .captureclass (1 , ** self . _config ),
941
+ err = self .captureclass (2 , ** self . _config ),
940
942
)
941
943
self ._capture .start_capturing ()
942
944
@@ -1022,6 +1024,41 @@ def test_output(capsys):
1022
1024
capman .unset_fixture ()
1023
1025
1024
1026
1027
+ @fixture
1028
+ def capteesys (request : SubRequest ) -> Generator [CaptureFixture [str ]]:
1029
+ r"""Enable simultaneous text capturing and pass-through of writes
1030
+ to ``sys.stdout`` and ``sys.stderr`` as defined by ``--capture=``.
1031
+
1032
+
1033
+ The captured output is made available via ``capteesys.readouterr()`` method
1034
+ calls, which return a ``(out, err)`` namedtuple.
1035
+ ``out`` and ``err`` will be ``text`` objects.
1036
+
1037
+ The output is also passed-through, allowing it to be "live-printed",
1038
+ reported, or both as defined by ``--capture=``.
1039
+
1040
+ Returns an instance of :class:`CaptureFixture[str] <pytest.CaptureFixture>`.
1041
+
1042
+ Example:
1043
+
1044
+ .. code-block:: python
1045
+
1046
+ def test_output(capsys):
1047
+ print("hello")
1048
+ captured = capteesys.readouterr()
1049
+ assert captured.out == "hello\n"
1050
+ """
1051
+ capman : CaptureManager = request .config .pluginmanager .getplugin ("capturemanager" )
1052
+ capture_fixture = CaptureFixture (
1053
+ SysCapture , request , config = dict (tee = True ), _ispytest = True
1054
+ )
1055
+ capman .set_fixture (capture_fixture )
1056
+ capture_fixture ._start ()
1057
+ yield capture_fixture
1058
+ capture_fixture .close ()
1059
+ capman .unset_fixture ()
1060
+
1061
+
1025
1062
@fixture
1026
1063
def capsysbinary (request : SubRequest ) -> Generator [CaptureFixture [bytes ]]:
1027
1064
r"""Enable bytes capturing of writes to ``sys.stdout`` and ``sys.stderr``.
0 commit comments