add fixture to capture output in tests less fragile

Now we always remove previous output before capturing.
This commit is contained in:
Jörg Thalheim
2024-08-15 18:21:27 +02:00
parent aec45d3a1a
commit fe66a740b5
6 changed files with 50 additions and 24 deletions

View File

@@ -0,0 +1,23 @@
from typing import Any
import pytest
from pytest import CaptureFixture
class CaptureOutput:
def __init__(self, capsys: CaptureFixture) -> None:
self.capsys = capsys
def __enter__(self) -> "CaptureOutput":
self.capsys.readouterr()
return self
def __exit__(self, exc_type: Any, exc_value: Any, exc_traceback: Any) -> bool:
res = self.capsys.readouterr()
self.out = res.out
self.err = res.err
@pytest.fixture
def capture_output(capsys: CaptureFixture) -> CaptureOutput:
return CaptureOutput(capsys)