[PATCH 3/3] tests/functional: add -k TEST_NAME_PATTERN CLI arg

Manos Pitsidianakis posted 3 patches 4 months ago
There is a newer version of this series
[PATCH 3/3] tests/functional: add -k TEST_NAME_PATTERN CLI arg
Posted by Manos Pitsidianakis 4 months ago
Add a CLI argument that takes fnmatch(3)-style patterns as value and can
be specified many times. Only tests that match the pattern will be
executed. This argument is passed to unittest.main which takes the same
argument.

Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
---
 tests/functional/qemu_test/testcase.py | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/tests/functional/qemu_test/testcase.py b/tests/functional/qemu_test/testcase.py
index 4455fcc38016c14db04019bbd64eaae9b47363c1..b7a660fd729afe39ff8cf7a0be97c2fc2f2f573f 100644
--- a/tests/functional/qemu_test/testcase.py
+++ b/tests/functional/qemu_test/testcase.py
@@ -55,6 +55,14 @@ def parse_args(test_name: str) -> argparse.Namespace:
         action="store_true",
         help="List all tests that would be executed and exit.",
     )
+    parser.add_argument(
+        "-k",
+        dest="test_name_patterns",
+        action="append",
+        type=str,
+        help="Only run tests which match the given substring. "
+        "This argument is passed to unittest.main verbatim.",
+    )
     return parser.parse_args()
 
 
@@ -301,8 +309,12 @@ def main():
 
         tr = pycotap.TAPTestRunner(message_log = pycotap.LogMode.LogToError,
                                    test_output_log = pycotap.LogMode.LogToError)
+        argv = ["__dummy__", path] + (
+            (["-k"] + args.test_name_patterns)
+            if args.test_name_patterns else []
+        )
         res = unittest.main(module = None, testRunner = tr, exit = False,
-                            argv=["__dummy__", path])
+                            argv=argv)
         for (test, message) in res.result.errors + res.result.failures:
 
             if hasattr(test, "log_filename"):

-- 
2.47.2
Re: [PATCH 3/3] tests/functional: add -k TEST_NAME_PATTERN CLI arg
Posted by Paolo Bonzini 3 months, 4 weeks ago
On 7/18/25 11:12, Manos Pitsidianakis wrote:
> Add a CLI argument that takes fnmatch(3)-style patterns as value and can
> be specified many times. Only tests that match the pattern will be
> executed. This argument is passed to unittest.main which takes the same
> argument.
> 
> Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
> ---
>   tests/functional/qemu_test/testcase.py | 14 +++++++++++++-
>   1 file changed, 13 insertions(+), 1 deletion(-)
> 
> diff --git a/tests/functional/qemu_test/testcase.py b/tests/functional/qemu_test/testcase.py
> index 4455fcc38016c14db04019bbd64eaae9b47363c1..b7a660fd729afe39ff8cf7a0be97c2fc2f2f573f 100644
> --- a/tests/functional/qemu_test/testcase.py
> +++ b/tests/functional/qemu_test/testcase.py
> @@ -55,6 +55,14 @@ def parse_args(test_name: str) -> argparse.Namespace:
>           action="store_true",
>           help="List all tests that would be executed and exit.",
>       )
> +    parser.add_argument(
> +        "-k",
> +        dest="test_name_patterns",
> +        action="append",
> +        type=str,
> +        help="Only run tests which match the given substring. "
> +        "This argument is passed to unittest.main verbatim.",
> +    )
>       return parser.parse_args()
>   
>   
> @@ -301,8 +309,12 @@ def main():
>   
>           tr = pycotap.TAPTestRunner(message_log = pycotap.LogMode.LogToError,
>                                      test_output_log = pycotap.LogMode.LogToError)
> +        argv = ["__dummy__", path] + (
> +            (["-k"] + args.test_name_patterns)
> +            if args.test_name_patterns else []
> +        )

This does not work for >1 occurrences of -k. Maybe something like:

argv = list(itertools.chain(
     ["__dummy__", "path"],
     *[["-k", x] for x in args.test_name_patterns]))

Paolo

>           res = unittest.main(module = None, testRunner = tr, exit = False,
> -                            argv=["__dummy__", path])
> +                            argv=argv)
>           for (test, message) in res.result.errors + res.result.failures:
>   
>               if hasattr(test, "log_filename"):
>
Re: [PATCH 3/3] tests/functional: add -k TEST_NAME_PATTERN CLI arg
Posted by Manos Pitsidianakis 3 months, 4 weeks ago
On Fri, Jul 18, 2025 at 12:37 PM Paolo Bonzini <pbonzini@redhat.com> wrote:
>
> On 7/18/25 11:12, Manos Pitsidianakis wrote:
> > Add a CLI argument that takes fnmatch(3)-style patterns as value and can
> > be specified many times. Only tests that match the pattern will be
> > executed. This argument is passed to unittest.main which takes the same
> > argument.
> >
> > Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
> > ---
> >   tests/functional/qemu_test/testcase.py | 14 +++++++++++++-
> >   1 file changed, 13 insertions(+), 1 deletion(-)
> >
> > diff --git a/tests/functional/qemu_test/testcase.py b/tests/functional/qemu_test/testcase.py
> > index 4455fcc38016c14db04019bbd64eaae9b47363c1..b7a660fd729afe39ff8cf7a0be97c2fc2f2f573f 100644
> > --- a/tests/functional/qemu_test/testcase.py
> > +++ b/tests/functional/qemu_test/testcase.py
> > @@ -55,6 +55,14 @@ def parse_args(test_name: str) -> argparse.Namespace:
> >           action="store_true",
> >           help="List all tests that would be executed and exit.",
> >       )
> > +    parser.add_argument(
> > +        "-k",
> > +        dest="test_name_patterns",
> > +        action="append",
> > +        type=str,
> > +        help="Only run tests which match the given substring. "
> > +        "This argument is passed to unittest.main verbatim.",
> > +    )
> >       return parser.parse_args()
> >
> >
> > @@ -301,8 +309,12 @@ def main():
> >
> >           tr = pycotap.TAPTestRunner(message_log = pycotap.LogMode.LogToError,
> >                                      test_output_log = pycotap.LogMode.LogToError)
> > +        argv = ["__dummy__", path] + (
> > +            (["-k"] + args.test_name_patterns)
> > +            if args.test_name_patterns else []
> > +        )
>
> This does not work for >1 occurrences of -k. Maybe something like:
>
> argv = list(itertools.chain(
>      ["__dummy__", "path"],
>      *[["-k", x] for x in args.test_name_patterns]))


Oops, good catch!

Thanks

> Paolo
>
> >           res = unittest.main(module = None, testRunner = tr, exit = False,
> > -                            argv=["__dummy__", path])
> > +                            argv=argv)
> >           for (test, message) in res.result.errors + res.result.failures:
> >
> >               if hasattr(test, "log_filename"):
> >
>