Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Option to show/hide local variables output in test results #338 #366

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ repos:
hooks:
- id: black
- repo: https://github.com/PyCQA/isort
rev: 5.10.1
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/asottile/yesqa
Expand Down
55 changes: 55 additions & 0 deletions tests/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,18 @@ def writer(console=mock_rich_console):
)


@fixture
def writer_hiding_locals(console=mock_rich_console):
yield TestResultWriter(
console,
Suite([]),
TestOutputStyle.LIVE,
[TestProgressStyle.INLINE],
None,
show_locals=False,
)


for left, right in [
("abc", "abd"),
(123, 124),
Expand Down Expand Up @@ -398,3 +410,46 @@ def _(console=mock_rich_console):
result = result_writer.output_all_test_results(_ for _ in ())
assert result == []
assert not console.print.called


@test("TestResultWriter.print_traceback with stacktrace showing locals")
def _(writer=writer):
def internal_func3(**params):
return 1 / 0

def internal_func2(name):
age = 18
internal_func3(name=name, age=age)

def internal_func1():
name = "John"
return internal_func2(name=name)

try:
internal_func1()
except ZeroDivisionError as ex:
writer.print_traceback(ex)


@test("TestResultWriter.print_traceback with stacktrace and hiding locals")
def _(writer=writer_hiding_locals):
def internal_func3(**params):
return 1 / 0

def internal_func2(name):
age = 18
internal_func3(name=name, age=age)

def internal_func1():
name = "John"
return internal_func2(name=name)

try:
internal_func1()
except ZeroDivisionError as ex:
writer.print_traceback(ex)


@test("TestResultWriter.print_traceback without stacktrace")
def _(writer=writer):
writer.print_traceback(Exception("Some error"))
7 changes: 7 additions & 0 deletions ward/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ def run(ctx: click.Context):
help="Record and display duration of n longest running tests",
default=0,
)
@click.option(
"--show-locals/--hide-locals",
help="Print all tests without executing them",
default=True,
)
@click.option(
"--dry-run/--no-dry-run",
help="Print all tests without executing them",
Expand All @@ -174,6 +179,7 @@ def test(
capture_output: bool,
show_slowest: int,
show_diff_symbols: bool,
show_locals: bool,
dry_run: bool,
hook_module: Tuple[str],
):
Expand Down Expand Up @@ -227,6 +233,7 @@ def test(
progress_styles=progress_styles,
config_path=config_path,
show_diff_symbols=show_diff_symbols,
show_locals=show_locals,
)
for renderable in print_before:
rich_console.print(renderable)
Expand Down
6 changes: 5 additions & 1 deletion ward/_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,13 +676,15 @@ def __init__(
progress_styles: List[TestProgressStyle],
config_path: Optional[Path],
show_diff_symbols: bool = False,
show_locals: bool = True,
):
self.console = console
self.suite = suite
self.test_output_style = test_output_style
self.progress_styles = progress_styles
self.config_path = config_path
self.show_diff_symbols = show_diff_symbols
self.show_locals = show_locals
self.terminal_size = get_terminal_size()

def output_all_test_results(
Expand Down Expand Up @@ -951,7 +953,9 @@ def print_traceback(self, err):
# The first frame contains library internal code which is not
# relevant to end users, so skip over it.
trace = trace.tb_next
tb = Traceback.from_exception(err.__class__, err, trace, show_locals=True)
tb = Traceback.from_exception(
err.__class__, err, trace, show_locals=self.show_locals
)
self.console.print(Padding(tb, pad=(0, 2, 1, 2)))
else:
self.console.print(str(err))
Expand Down
1 change: 1 addition & 0 deletions ward/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Config:
capture_output: bool
show_slowest: int
show_diff_symbols: bool
show_locals: bool
dry_run: bool
hook_module: Tuple[str]
progress_style: Tuple[str]
Expand Down