Uses contextlib.redirect_stderr/stdout to capture the output and
compare it with the snapshots. dirty_equals.IsStr can be used to ignore
the output if needed.
@contextlib.contextmanagerdefprints(*,stdout:Snapshot[str]="",stderr:Snapshot[str]=""):"""Uses `contextlib.redirect_stderr/stdout` to capture the output and compare it with the snapshots. `dirty_equals.IsStr` can be used to ignore the output if needed. Parameters: stdout: snapshot which is compared to the recorded output stderr: snapshot which is compared to the recorded error output === "original" <!-- inline-snapshot: first_block outcome-passed=1 outcome-errors=1 --> ``` python from inline_snapshot import snapshot from inline_snapshot.extra import prints import sys def test_prints(): with prints(stdout=snapshot(), stderr=snapshot()): print("hello world") print("some error", file=sys.stderr) ``` === "--inline-snapshot=create" <!-- inline-snapshot: create outcome-passed=1 outcome-errors=1 --> ``` python hl_lines="7 8 9" from inline_snapshot import snapshot from inline_snapshot.extra import prints import sys def test_prints(): with prints( stdout=snapshot("hello world\\n"), stderr=snapshot("some error\\n") ): print("hello world") print("some error", file=sys.stderr) ``` === "ignore stdout" <!-- inline-snapshot: outcome-passed=1 --> ``` python hl_lines="3 9 10" from inline_snapshot import snapshot from inline_snapshot.extra import prints from dirty_equals import IsStr import sys def test_prints(): with prints( stdout=IsStr(), stderr=snapshot("some error\\n"), ): print("hello world") print("some error", file=sys.stderr) ``` """withredirect_stdout(io.StringIO())asstdout_io:withredirect_stderr(io.StringIO())asstderr_io:yieldassertstderr_io.getvalue()==stderrassertstdout_io.getvalue()==stdout
@contextlib.contextmanagerdefraises(exception:Snapshot[str]):"""Check that an exception is raised. Parameters: exception: snapshot which is compared with `#!python f"{type}: {message}"` if an exception occurred or `#!python "<no exception>"` if no exception was raised. === "original" <!-- inline-snapshot: first_block outcome-passed=1 outcome-errors=1 --> ``` python from inline_snapshot import snapshot from inline_snapshot.extra import raises def test_raises(): with raises(snapshot()): 1 / 0 ``` === "--inline-snapshot=create" <!-- inline-snapshot: create outcome-passed=1 outcome-errors=1 --> ``` python hl_lines="6" from inline_snapshot import snapshot from inline_snapshot.extra import raises def test_raises(): with raises(snapshot("ZeroDivisionError: division by zero")): 1 / 0 ``` """try:yieldexceptExceptionasex:msg=str(ex)if"\n"inmsg:assertf"{type(ex).__name__}:\n{ex}"==exceptionelse:assertf"{type(ex).__name__}: {ex}"==exceptionelse:assert"<no exception>"==exception
frominline_snapshotimportsnapshotfrominline_snapshot.extraimportwarnsfromwarningsimportwarndeftest_warns():withwarns(snapshot([(8,"UserWarning: some problem")]),include_line=True):warn("some problem")
@contextlib.contextmanagerdefwarns(expected_warnings:Snapshot[List[Warning]],/,include_line:bool=False,include_file:bool=False,):""" Captures warnings with `warnings.catch_warnings` and compares them against expected warnings. Parameters: expected_warnings: Snapshot containing a list of expected warnings. include_line: If `True`, each expected warning is a tuple `(linenumber, message)`. include_file: If `True`, each expected warning is a tuple `(filename, message)`. The format of the expected warning: - `(filename, linenumber, message)` if both `include_line` and `include_file` are `True`. - `(linenumber, message)` if only `include_line` is `True`. - `(filename, message)` if only `include_file` is `True`. - A string `message` if both are `False`. === "original" <!-- inline-snapshot: first_block outcome-passed=1 outcome-errors=1 --> ``` python from inline_snapshot import snapshot from inline_snapshot.extra import warns from warnings import warn def test_warns(): with warns(snapshot(), include_line=True): warn("some problem") ``` === "--inline-snapshot=create" <!-- inline-snapshot: create fix outcome-passed=1 outcome-errors=1 --> ``` python hl_lines="7" from inline_snapshot import snapshot from inline_snapshot.extra import warns from warnings import warn def test_warns(): with warns(snapshot([(8, "UserWarning: some problem")]), include_line=True): warn("some problem") ``` """withwarnings.catch_warnings(record=True)asresult:warnings.simplefilter("always")yielddefmake_warning(w):message=f"{w.category.__name__}: {w.message}"ifnotinclude_lineandnotinclude_file:returnmessagemessage=(message,)ifinclude_line:message=(w.lineno,*message)ifinclude_file:message=(w.filename,*message)returnmessageassert[make_warning(w)forwinresult]==expected_warnings