Skip to content

Commit 6fcb3ff

Browse files
fundakolkartben
authored andcommitted
twister: refactoring: Improve error message when test failed
Modified the assertion statements to raise informative exceptions that indicate which lines did not pass the assertions. Added unit tests using pytest to validate the functionality of the modified functions. Signed-off-by: Lukasz Fundakowski <[email protected]>
1 parent 4977c5c commit 6fcb3ff

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

scripts/pylib/pytest-twister-harness/src/twister_harness/helpers/utils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ def find_in_config(config_file: Path | str, config_key: str) -> str:
2727

2828
def match_lines(output_lines: list[str], searched_lines: list[str]) -> None:
2929
"""Check all lines exist in the output"""
30-
for sl in searched_lines:
31-
assert any(sl in line for line in output_lines)
30+
__tracebackhide__ = True # pylint: disable=unused-variable
31+
missing_lines = [sl for sl in searched_lines if not any(sl in line for line in output_lines)]
32+
if missing_lines:
33+
raise AssertionError(f"Missing lines: {missing_lines}")
3234

3335

3436
def match_no_lines(output_lines: list[str], searched_lines: list[str]) -> None:
3537
"""Check lines not found in the output"""
36-
for sl in searched_lines:
37-
assert all(sl not in line for line in output_lines)
38+
__tracebackhide__ = True # pylint: disable=unused-variable
39+
found_lines = [sl for sl in searched_lines if any(sl in line for line in output_lines)]
40+
if found_lines:
41+
raise AssertionError(f"Found lines that should not be present: {found_lines}")
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Copyright (c) 2025 Nordic Semiconductor ASA
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
import textwrap
6+
7+
import pytest
8+
from twister_harness.helpers.utils import match_lines, match_no_lines
9+
10+
OUTPUT_LINES = textwrap.dedent("""\
11+
The Zen of Python, by Tim Peters
12+
Beautiful is better than ugly.
13+
Explicit is better than implicit.
14+
Simple is better than complex.\
15+
""").split('\n')
16+
17+
18+
CHECK_LINES = textwrap.dedent("""\
19+
Although never is often better than right now.
20+
If the implementation is hard to explain, it's a bad idea.
21+
If the implementation is easy to explain, it may be a good idea.
22+
Namespaces are one honking great idea -- let's do more of those!\
23+
""").split('\n')
24+
25+
26+
@pytest.fixture
27+
def output_lines() -> list[str]:
28+
return OUTPUT_LINES
29+
30+
31+
@pytest.mark.parametrize(
32+
"expected",
33+
[
34+
OUTPUT_LINES[0:1], # one line
35+
OUTPUT_LINES[0:2], # two lines
36+
OUTPUT_LINES[-1:], # last line
37+
[OUTPUT_LINES[0][2:-2]], # check partial of a text
38+
],
39+
)
40+
def test_match_lines_positive(expected, output_lines):
41+
match_lines(output_lines, expected)
42+
43+
44+
@pytest.mark.parametrize(
45+
"not_expected",
46+
[
47+
CHECK_LINES[0:1],
48+
CHECK_LINES[0:2],
49+
CHECK_LINES[-1:],
50+
[CHECK_LINES[-1][2:-2]],
51+
[CHECK_LINES[0], OUTPUT_LINES[0]], # one line match, one not
52+
CHECK_LINES[::-1], # reverted order
53+
],
54+
)
55+
def test_match_lines_negative(not_expected, output_lines):
56+
with pytest.raises(AssertionError, match="Missing lines.*"):
57+
match_lines(output_lines, not_expected)
58+
59+
60+
@pytest.mark.parametrize(
61+
"not_expected",
62+
[
63+
OUTPUT_LINES[0:1],
64+
OUTPUT_LINES[1:3],
65+
OUTPUT_LINES[-1:],
66+
[CHECK_LINES[0], OUTPUT_LINES[0]], # one line match, one not
67+
OUTPUT_LINES[::-1], # reverted order
68+
],
69+
)
70+
def test_match_no_lines_negative(not_expected, output_lines):
71+
with pytest.raises(AssertionError, match="Found lines that should not be present.*"):
72+
match_no_lines(output_lines, not_expected)
73+
74+
75+
@pytest.mark.parametrize(
76+
"expected",
77+
[
78+
CHECK_LINES[0:1],
79+
CHECK_LINES[3:5],
80+
CHECK_LINES[-1:],
81+
],
82+
)
83+
def test_match_no_lines_positive(expected, output_lines):
84+
match_no_lines(output_lines, expected)

0 commit comments

Comments
 (0)