1
0
Fork 0
mirror of synced 2025-03-06 20:59:54 +01:00
linux/tools/testing/selftests/net/lib/py/ksft.py
Jakub Kicinski eeb409bde9 selftests: net: print report check location in python tests
Developing Python tests is a bit annoying because when test fails
we only print the fail message and no info about which exact check
led to it. Print the location (the first line of this example is new):

  # At /root/ksft-net-drv/./net/nl_netdev.py line 38:
  # Check failed 0 != 10
  not ok 3 nl_netdev.page_pool_check

Reviewed-by: Petr Machata <petrm@nvidia.com>
Link: https://lore.kernel.org/r/20240412141436.828666-4-kuba@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2024-04-15 11:21:12 -07:00

99 lines
2.2 KiB
Python

# SPDX-License-Identifier: GPL-2.0
import builtins
import inspect
from .consts import KSFT_MAIN_NAME
KSFT_RESULT = None
class KsftSkipEx(Exception):
pass
class KsftXfailEx(Exception):
pass
def ksft_pr(*objs, **kwargs):
print("#", *objs, **kwargs)
def _fail(*args):
global KSFT_RESULT
KSFT_RESULT = False
frame = inspect.stack()[2]
ksft_pr("At " + frame.filename + " line " + str(frame.lineno) + ":")
ksft_pr(*args)
def ksft_eq(a, b, comment=""):
global KSFT_RESULT
if a != b:
_fail("Check failed", a, "!=", b, comment)
def ksft_true(a, comment=""):
if not a:
_fail("Check failed", a, "does not eval to True", comment)
def ksft_in(a, b, comment=""):
if a not in b:
_fail("Check failed", a, "not in", b, comment)
def ksft_ge(a, b, comment=""):
if a < b:
_fail("Check failed", a, "<", b, comment)
def ktap_result(ok, cnt=1, case="", comment=""):
res = ""
if not ok:
res += "not "
res += "ok "
res += str(cnt) + " "
res += KSFT_MAIN_NAME
if case:
res += "." + str(case.__name__)
if comment:
res += " # " + comment
print(res)
def ksft_run(cases, args=()):
totals = {"pass": 0, "fail": 0, "skip": 0, "xfail": 0}
print("KTAP version 1")
print("1.." + str(len(cases)))
global KSFT_RESULT
cnt = 0
for case in cases:
KSFT_RESULT = True
cnt += 1
try:
case(*args)
except KsftSkipEx as e:
ktap_result(True, cnt, case, comment="SKIP " + str(e))
totals['skip'] += 1
continue
except KsftXfailEx as e:
ktap_result(True, cnt, case, comment="XFAIL " + str(e))
totals['xfail'] += 1
continue
except Exception as e:
for line in str(e).split('\n'):
ksft_pr("Exception|", line)
ktap_result(False, cnt, case)
totals['fail'] += 1
continue
ktap_result(KSFT_RESULT, cnt, case)
totals['pass'] += 1
print(
f"# Totals: pass:{totals['pass']} fail:{totals['fail']} xfail:{totals['xfail']} xpass:0 skip:{totals['skip']} error:0"
)