[PATCH v2 2/3] tests/functional: add --list-tests CLI arg

Manos Pitsidianakis posted 3 patches 3 months, 4 weeks ago
[PATCH v2 2/3] tests/functional: add --list-tests CLI arg
Posted by Manos Pitsidianakis 3 months, 4 weeks ago
Add CLI argument to list tests and exit.

Example output (current dir is build directory under root dir):

  $ export PYTHONPATH=../python:../tests/functional
  $ export QEMU_TEST_QEMU_BINARY="$(pwd)/qemu-system-aarch64"
  $ ./pyvenv/bin/python3 ../tests/functional/test_aarch64_virt.py --list-tests
  test_aarch64_virt_gicv2 (test_aarch64_virt.Aarch64VirtMachine.test_aarch64_virt_gicv2)
  test_aarch64_virt_gicv3 (test_aarch64_virt.Aarch64VirtMachine.test_aarch64_virt_gicv3)
  test_alpine_virt_tcg_gic_max (test_aarch64_virt.Aarch64VirtMachine.test_alpine_virt_tcg_gic_max)

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

diff --git a/tests/functional/qemu_test/testcase.py b/tests/functional/qemu_test/testcase.py
index 9b00c63e6ca7a2a669fd456f1d1b51501ce4a726..bfee6638edf6f9853ead1e3809ae3c9152089406 100644
--- a/tests/functional/qemu_test/testcase.py
+++ b/tests/functional/qemu_test/testcase.py
@@ -50,6 +50,11 @@ def parse_args(test_name: str) -> argparse.Namespace:
         "This is equivalent to setting QEMU_TEST_KEEP_SCRATCH=1 in the "
         "environment.",
     )
+    parser.add_argument(
+        "--list-tests",
+        action="store_true",
+        help="List all tests that would be executed and exit.",
+    )
     return parser.parse_args()
 
 
@@ -280,10 +285,13 @@ def tearDown(self):
 
     def main():
         path = os.path.basename(sys.argv[0])[:-3]
-        # If argparse receives --help or an unknown argument, it will raise a
-        # SystemExit which will get caught by the test runner. Parse the
-        # arguments here too to handle that case.
-        parse_args(path)
+        args = parse_args(path)
+        if args.list_tests:
+            loader = unittest.TestLoader()
+            for test_suite in loader.loadTestsFromName(path):
+                for test in test_suite:
+                    print(test)
+            return
 
         cache = os.environ.get("QEMU_TEST_PRECACHE", None)
         if cache is not None:

-- 
2.47.2
Re: [PATCH v2 2/3] tests/functional: add --list-tests CLI arg
Posted by Thomas Huth 3 months, 3 weeks ago
On 18/07/2025 13.04, Manos Pitsidianakis wrote:
> Add CLI argument to list tests and exit.
> 
> Example output (current dir is build directory under root dir):
> 
>    $ export PYTHONPATH=../python:../tests/functional
>    $ export QEMU_TEST_QEMU_BINARY="$(pwd)/qemu-system-aarch64"
>    $ ./pyvenv/bin/python3 ../tests/functional/test_aarch64_virt.py --list-tests
>    test_aarch64_virt_gicv2 (test_aarch64_virt.Aarch64VirtMachine.test_aarch64_virt_gicv2)
>    test_aarch64_virt_gicv3 (test_aarch64_virt.Aarch64VirtMachine.test_aarch64_virt_gicv3)
>    test_alpine_virt_tcg_gic_max (test_aarch64_virt.Aarch64VirtMachine.test_alpine_virt_tcg_gic_max)
> 
> Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
> ---
>   tests/functional/qemu_test/testcase.py | 16 ++++++++++++----
>   1 file changed, 12 insertions(+), 4 deletions(-)
> 
> diff --git a/tests/functional/qemu_test/testcase.py b/tests/functional/qemu_test/testcase.py
> index 9b00c63e6ca7a2a669fd456f1d1b51501ce4a726..bfee6638edf6f9853ead1e3809ae3c9152089406 100644
> --- a/tests/functional/qemu_test/testcase.py
> +++ b/tests/functional/qemu_test/testcase.py
> @@ -50,6 +50,11 @@ def parse_args(test_name: str) -> argparse.Namespace:
>           "This is equivalent to setting QEMU_TEST_KEEP_SCRATCH=1 in the "
>           "environment.",
>       )
> +    parser.add_argument(
> +        "--list-tests",
> +        action="store_true",
> +        help="List all tests that would be executed and exit.",
> +    )
>       return parser.parse_args()
>   
>   
> @@ -280,10 +285,13 @@ def tearDown(self):
>   
>       def main():
>           path = os.path.basename(sys.argv[0])[:-3]
> -        # If argparse receives --help or an unknown argument, it will raise a
> -        # SystemExit which will get caught by the test runner. Parse the
> -        # arguments here too to handle that case.
> -        parse_args(path)
> +        args = parse_args(path)

As mentioned for the initial --debug patch already: How's about changing 
that "args" into a class variable instead and get rid of the parse_args() in 
the setUp() function?

  Thomas

> +        if args.list_tests:
> +            loader = unittest.TestLoader()
> +            for test_suite in loader.loadTestsFromName(path):
> +                for test in test_suite:
> +                    print(test)
> +            return
>   
>           cache = os.environ.get("QEMU_TEST_PRECACHE", None)
>           if cache is not None:
>
Re: [PATCH v2 2/3] tests/functional: add --list-tests CLI arg
Posted by Manos Pitsidianakis 3 months, 3 weeks ago
On Fri, Jul 25, 2025 at 7:40 AM Thomas Huth <thuth@redhat.com> wrote:
>
> On 18/07/2025 13.04, Manos Pitsidianakis wrote:
> > Add CLI argument to list tests and exit.
> >
> > Example output (current dir is build directory under root dir):
> >
> >    $ export PYTHONPATH=../python:../tests/functional
> >    $ export QEMU_TEST_QEMU_BINARY="$(pwd)/qemu-system-aarch64"
> >    $ ./pyvenv/bin/python3 ../tests/functional/test_aarch64_virt.py --list-tests
> >    test_aarch64_virt_gicv2 (test_aarch64_virt.Aarch64VirtMachine.test_aarch64_virt_gicv2)
> >    test_aarch64_virt_gicv3 (test_aarch64_virt.Aarch64VirtMachine.test_aarch64_virt_gicv3)
> >    test_alpine_virt_tcg_gic_max (test_aarch64_virt.Aarch64VirtMachine.test_alpine_virt_tcg_gic_max)
> >
> > Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
> > ---
> >   tests/functional/qemu_test/testcase.py | 16 ++++++++++++----
> >   1 file changed, 12 insertions(+), 4 deletions(-)
> >
> > diff --git a/tests/functional/qemu_test/testcase.py b/tests/functional/qemu_test/testcase.py
> > index 9b00c63e6ca7a2a669fd456f1d1b51501ce4a726..bfee6638edf6f9853ead1e3809ae3c9152089406 100644
> > --- a/tests/functional/qemu_test/testcase.py
> > +++ b/tests/functional/qemu_test/testcase.py
> > @@ -50,6 +50,11 @@ def parse_args(test_name: str) -> argparse.Namespace:
> >           "This is equivalent to setting QEMU_TEST_KEEP_SCRATCH=1 in the "
> >           "environment.",
> >       )
> > +    parser.add_argument(
> > +        "--list-tests",
> > +        action="store_true",
> > +        help="List all tests that would be executed and exit.",
> > +    )
> >       return parser.parse_args()
> >
> >
> > @@ -280,10 +285,13 @@ def tearDown(self):
> >
> >       def main():
> >           path = os.path.basename(sys.argv[0])[:-3]
> > -        # If argparse receives --help or an unknown argument, it will raise a
> > -        # SystemExit which will get caught by the test runner. Parse the
> > -        # arguments here too to handle that case.
> > -        parse_args(path)
> > +        args = parse_args(path)
>
> As mentioned for the initial --debug patch already: How's about changing
> that "args" into a class variable instead and get rid of the parse_args() in
> the setUp() function?
>
>   Thomas

Yes that sounds good. Will merge the two patch series and resubmit,
thanks Thomas!

-- 
Manos Pitsidianakis
Emulation and Virtualization Engineer at Linaro Ltd