diff mbox series

[net-next,v3,7/8] selftests: net: support matching cases by name prefix

Message ID 20240417231146.2435572-8-kuba@kernel.org (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series selftests: drv-net: support testing with a remote system | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 7 of 7 maintainers
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 25 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-04-18--21-00 (tests: 960)

Commit Message

Jakub Kicinski April 17, 2024, 11:11 p.m. UTC
While writing tests with a lot more cases I got tired of having
to jump back and forth to add the name of the test to the ksft_run()
list. Most unittest frameworks do some name matching, e.g. assume
that functions with names starting with test_ are test cases.

Support similar flow in ksft_run(). Let the author list the desired
prefixes. globals() need to be passed explicitly, IDK how to work
around that.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
 tools/testing/selftests/drivers/net/ping.py |  3 +--
 tools/testing/selftests/net/lib/py/ksft.py  | 10 +++++++++-
 2 files changed, 10 insertions(+), 3 deletions(-)

Comments

Willem de Bruijn April 18, 2024, 2:26 p.m. UTC | #1
Jakub Kicinski wrote:
> While writing tests with a lot more cases I got tired of having
> to jump back and forth to add the name of the test to the ksft_run()
> list. Most unittest frameworks do some name matching, e.g. assume
> that functions with names starting with test_ are test cases.
> 
> Support similar flow in ksft_run(). Let the author list the desired
> prefixes. globals() need to be passed explicitly, IDK how to work
> around that.
> 
> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
> ---
>  tools/testing/selftests/drivers/net/ping.py |  3 +--
>  tools/testing/selftests/net/lib/py/ksft.py  | 10 +++++++++-
>  2 files changed, 10 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/testing/selftests/drivers/net/ping.py b/tools/testing/selftests/drivers/net/ping.py
> index 7dd197836ff1..58aefd3e740f 100755
> --- a/tools/testing/selftests/drivers/net/ping.py
> +++ b/tools/testing/selftests/drivers/net/ping.py
> @@ -24,8 +24,7 @@ from lib.py import cmd
>  
>  def main() -> None:
>      with NetDrvEpEnv(__file__) as cfg:
> -        ksft_run([test_v4, test_v6],
> -                 args=(cfg, ))
> +        ksft_run(globs=globals(), case_pfx={"test_"}, args=(cfg, ))
>      ksft_exit()
>  
>  
> diff --git a/tools/testing/selftests/net/lib/py/ksft.py b/tools/testing/selftests/net/lib/py/ksft.py
> index 25f2572fa540..fe4025dc5a16 100644
> --- a/tools/testing/selftests/net/lib/py/ksft.py
> +++ b/tools/testing/selftests/net/lib/py/ksft.py
> @@ -81,7 +81,15 @@ KSFT_RESULT_ALL = True
>      print(res)
>  
>  
> -def ksft_run(cases, args=()):
> +def ksft_run(cases=None, globs=None, case_pfx=None, args=()):
> +    cases = cases or []
> +
> +    if globs and case_pfx:
> +        for key, value in globs.items():
> +            stats_with_pfx = bool([pfx for pfx in case_pfx if key.startswith(pfx)])

stats -> starts

for the reader, just spell out prefix instead of pfx?

perhaps less pythonic, but just

    if key.startswith(prefix) and callable(value):
      cases.append(value)

?

> +            if callable(value) and stats_with_pfx:
> +                cases.append(value)
> +
>      totals = {"pass": 0, "fail": 0, "skip": 0, "xfail": 0}
>  
>      print("KTAP version 1")
> -- 
> 2.44.0
>
Jakub Kicinski April 18, 2024, 7:06 p.m. UTC | #2
On Thu, 18 Apr 2024 10:26:19 -0400 Willem de Bruijn wrote:
> > -def ksft_run(cases, args=()):
> > +def ksft_run(cases=None, globs=None, case_pfx=None, args=()):
> > +    cases = cases or []
> > +
> > +    if globs and case_pfx:
> > +        for key, value in globs.items():
> > +            stats_with_pfx = bool([pfx for pfx in case_pfx if key.startswith(pfx)])  
> 
> stats -> starts
> 
> for the reader, just spell out prefix instead of pfx?
> 
> perhaps less pythonic, but just
> 
>     if key.startswith(prefix) and callable(value):
>       cases.append(value)

like this?

diff --git a/tools/testing/selftests/net/lib/py/ksft.py b/tools/testing/selftests/net/lib/py/ksft.py
index fe4025dc5a16..8018bf98a9d2 100644
--- a/tools/testing/selftests/net/lib/py/ksft.py
+++ b/tools/testing/selftests/net/lib/py/ksft.py
@@ -86,9 +86,12 @@ KSFT_RESULT_ALL = True
 
     if globs and case_pfx:
         for key, value in globs.items():
-            stats_with_pfx = bool([pfx for pfx in case_pfx if key.startswith(pfx)])
-            if callable(value) and stats_with_pfx:
-                cases.append(value)
+            if not callable(value):
+                continue
+            for prefix in case_pfx:
+                if key.startswith(prefix):
+                    cases.append(value)
+                    break
 
     totals = {"pass": 0, "fail": 0, "skip": 0, "xfail": 0}
Willem de Bruijn April 18, 2024, 7:35 p.m. UTC | #3
Jakub Kicinski wrote:
> On Thu, 18 Apr 2024 10:26:19 -0400 Willem de Bruijn wrote:
> > > -def ksft_run(cases, args=()):
> > > +def ksft_run(cases=None, globs=None, case_pfx=None, args=()):
> > > +    cases = cases or []
> > > +
> > > +    if globs and case_pfx:
> > > +        for key, value in globs.items():
> > > +            stats_with_pfx = bool([pfx for pfx in case_pfx if key.startswith(pfx)])  
> > 
> > stats -> starts
> > 
> > for the reader, just spell out prefix instead of pfx?
> > 
> > perhaps less pythonic, but just
> > 
> >     if key.startswith(prefix) and callable(value):
> >       cases.append(value)
> 
> like this?
> 
> diff --git a/tools/testing/selftests/net/lib/py/ksft.py b/tools/testing/selftests/net/lib/py/ksft.py
> index fe4025dc5a16..8018bf98a9d2 100644
> --- a/tools/testing/selftests/net/lib/py/ksft.py
> +++ b/tools/testing/selftests/net/lib/py/ksft.py
> @@ -86,9 +86,12 @@ KSFT_RESULT_ALL = True
>  
>      if globs and case_pfx:
>          for key, value in globs.items():
> -            stats_with_pfx = bool([pfx for pfx in case_pfx if key.startswith(pfx)])
> -            if callable(value) and stats_with_pfx:
> -                cases.append(value)
> +            if not callable(value):
> +                continue
> +            for prefix in case_pfx:
> +                if key.startswith(prefix):
> +                    cases.append(value)
> +                    break

Yes. I would not have brought this up if it wasn't for the typo as
well. Python developers perhaps find this less pythonic, but I do find
it easier to follow.
diff mbox series

Patch

diff --git a/tools/testing/selftests/drivers/net/ping.py b/tools/testing/selftests/drivers/net/ping.py
index 7dd197836ff1..58aefd3e740f 100755
--- a/tools/testing/selftests/drivers/net/ping.py
+++ b/tools/testing/selftests/drivers/net/ping.py
@@ -24,8 +24,7 @@  from lib.py import cmd
 
 def main() -> None:
     with NetDrvEpEnv(__file__) as cfg:
-        ksft_run([test_v4, test_v6],
-                 args=(cfg, ))
+        ksft_run(globs=globals(), case_pfx={"test_"}, args=(cfg, ))
     ksft_exit()
 
 
diff --git a/tools/testing/selftests/net/lib/py/ksft.py b/tools/testing/selftests/net/lib/py/ksft.py
index 25f2572fa540..fe4025dc5a16 100644
--- a/tools/testing/selftests/net/lib/py/ksft.py
+++ b/tools/testing/selftests/net/lib/py/ksft.py
@@ -81,7 +81,15 @@  KSFT_RESULT_ALL = True
     print(res)
 
 
-def ksft_run(cases, args=()):
+def ksft_run(cases=None, globs=None, case_pfx=None, args=()):
+    cases = cases or []
+
+    if globs and case_pfx:
+        for key, value in globs.items():
+            stats_with_pfx = bool([pfx for pfx in case_pfx if key.startswith(pfx)])
+            if callable(value) and stats_with_pfx:
+                cases.append(value)
+
     totals = {"pass": 0, "fail": 0, "skip": 0, "xfail": 0}
 
     print("KTAP version 1")