[PATCH v4 2/4] python: replace avocado tests with pytest

John Snow posted 4 patches 3 days, 11 hours ago
Maintainers: Paolo Bonzini <pbonzini@redhat.com>, "Alex Bennée" <alex.bennee@linaro.org>, Thomas Huth <thuth@redhat.com>, John Snow <jsnow@redhat.com>, Cleber Rosa <crosa@redhat.com>
[PATCH v4 2/4] python: replace avocado tests with pytest
Posted by John Snow 3 days, 11 hours ago
Following suit with the rest of this repository, drop avocado and
replace it with the Python standard "pytest" package.

Our ultimate goal is to merge these python tests with the meson test
suite, so the use of 'pytest' here is only a stop-gap solution to get
the GitLab CI 'check-python-tox' passing again following recent Python
packaging ecosystem changes.

Signed-off-by: John Snow <jsnow@redhat.com>
---
 python/Makefile          |  4 +-
 python/setup.cfg         |  1 +
 python/tests/linters.py  | 79 ++++++++++++++++++++++++++++++++++++++++
 python/tests/minreqs.txt |  1 +
 4 files changed, 83 insertions(+), 2 deletions(-)
 create mode 100644 python/tests/linters.py

diff --git a/python/Makefile b/python/Makefile
index b6c9cd1bce2..42994d39618 100644
--- a/python/Makefile
+++ b/python/Makefile
@@ -105,7 +105,7 @@ develop:
 
 .PHONY: check
 check:
-	@avocado --config avocado.cfg run tests/
+	@pytest -v tests/*.py
 
 .PHONY: check-tox
 check-tox:
@@ -113,7 +113,7 @@ check-tox:
 
 .PHONY: check-coverage
 check-coverage:
-	@coverage run -m avocado --config avocado.cfg run tests/*.py
+	@coverage run -m pytest -v tests/*.py
 	@coverage combine
 	@coverage html
 	@coverage report
diff --git a/python/setup.cfg b/python/setup.cfg
index c46a95f8d41..03344526730 100644
--- a/python/setup.cfg
+++ b/python/setup.cfg
@@ -43,6 +43,7 @@ devel =
     mypy >= 1.4.0
     pylint >= 2.17.3
     pylint != 3.2.4; python_version<"3.9"
+    pytest >= 6.0.2
     tox >= 3.18.0
     sphinx >= 3.4.3
 
diff --git a/python/tests/linters.py b/python/tests/linters.py
new file mode 100644
index 00000000000..cce29c0a8ea
--- /dev/null
+++ b/python/tests/linters.py
@@ -0,0 +1,79 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+import os
+from subprocess import check_call
+import sys
+
+
+class TestLinters:
+
+    def test_flake8_pkg(self):
+        check_call([sys.executable, "-m", "flake8", "qemu/"])
+
+    def test_flake8_scripts(self):
+        check_call([sys.executable, "-m", "flake8", "scripts/"])
+
+    def test_flake8_qapi(self):
+        check_call([sys.executable, "-m", "flake8",
+              "../scripts/qapi/",
+              "../docs/sphinx/qapidoc.py",
+              "../docs/sphinx/qapi_domain.py"])
+
+    def test_isort_pkg(self):
+        check_call([sys.executable, "-m", "isort", "-c", "qemu/"])
+
+    def test_isort_scripts(self):
+        check_call([sys.executable, "-m", "isort", "-c", "scripts/"])
+
+    def test_isort_qapi(self):
+        check_call([sys.executable, "-m", "isort",
+                    "--sp", ".", "-c", "../scripts/qapi/"])
+
+    def test_isort_qapi_sphinx(self):
+        # Force isort to recognize 'compat' as a local module and not
+        # third-party
+        check_call([sys.executable, "-m", "isort",
+                    "--sp", ".", "-c", "-p", "compat",
+                    "../docs/sphinx/qapi_domain.py",
+                    "../docs/sphinx/qapidoc.py"])
+
+    def test_mypy_pkg(self):
+        check_call([sys.executable, "-m", "mypy", "-p", "qemu"])
+
+    def test_mypy_scripts(self):
+        check_call([sys.executable, "-m", "mypy", "scripts/"])
+
+    def test_mypy_qapi(self):
+        check_call([sys.executable, "-m", "mypy", "../scripts/qapi"])
+
+    def test_mypy_iotests(self):
+        check_call([sys.executable, "-m", "linters", "--mypy"],
+                   cwd="../tests/qemu-iotests/")
+
+    # Setuptools v60 introduced the SETUPTOOLS_USE_DISTUTILS=stdlib
+    # workaround; stdlib distutils was fully removed in Python
+    # 3.12+. Once we are on >=3.12+ exclusively, this workaround can be
+    # dropped safely. Until then, it is needed for some versions on
+    # Fedora/Debian distributions which relied upon distro-patched
+    # setuptools present in CPython, but not within setuptools itself.
+
+    def test_pylint_pkg(self):
+        os.environ['SETUPTOOLS_USE_DISTUTILS'] = 'stdlib'
+        check_call([sys.executable, "-m", "pylint", "qemu/"])
+
+    def test_pylint_scripts(self):
+        os.environ['SETUPTOOLS_USE_DISTUTILS'] = 'stdlib'
+        check_call([sys.executable, "-m", "pylint", "scripts/"])
+
+    def test_pylint_qapi(self):
+        os.environ['SETUPTOOLS_USE_DISTUTILS'] = 'stdlib'
+        check_call([sys.executable, "-m", "pylint",
+              "--rcfile=../scripts/qapi/pylintrc",
+              "../scripts/qapi/",
+              "../docs/sphinx/qapidoc.py",
+              "../docs/sphinx/qapi_domain.py"])
+
+    def test_pylint_iotests(self):
+        os.environ['SETUPTOOLS_USE_DISTUTILS'] = 'stdlib'
+        check_call([sys.executable, "-m", "linters", "--pylint"],
+                   cwd="../tests/qemu-iotests/")
diff --git a/python/tests/minreqs.txt b/python/tests/minreqs.txt
index 38b17d17e14..19912027076 100644
--- a/python/tests/minreqs.txt
+++ b/python/tests/minreqs.txt
@@ -32,6 +32,7 @@ fusepy==2.0.4
 
 # Test-runners, utilities, etc.
 avocado-framework==90.0
+pytest==6.0.2
 
 # Linters
 flake8==5.0.4
-- 
2.53.0
Re: [PATCH v4 2/4] python: replace avocado tests with pytest
Posted by Thomas Huth 2 days, 23 hours ago
On 26/02/2026 22.33, John Snow wrote:
> Following suit with the rest of this repository, drop avocado and
> replace it with the Python standard "pytest" package.
> 
> Our ultimate goal is to merge these python tests with the meson test
> suite, so the use of 'pytest' here is only a stop-gap solution to get
> the GitLab CI 'check-python-tox' passing again following recent Python
> packaging ecosystem changes.
> 
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
>   python/Makefile          |  4 +-
>   python/setup.cfg         |  1 +
>   python/tests/linters.py  | 79 ++++++++++++++++++++++++++++++++++++++++
>   python/tests/minreqs.txt |  1 +
>   4 files changed, 83 insertions(+), 2 deletions(-)
>   create mode 100644 python/tests/linters.py

Reviewed-by: Thomas Huth <thuth@redhat.com>
Re: [PATCH v4 2/4] python: replace avocado tests with pytest
Posted by Daniel P. Berrangé 3 days ago
On Thu, Feb 26, 2026 at 04:33:58PM -0500, John Snow wrote:
> Following suit with the rest of this repository, drop avocado and
> replace it with the Python standard "pytest" package.
> 
> Our ultimate goal is to merge these python tests with the meson test
> suite, so the use of 'pytest' here is only a stop-gap solution to get
> the GitLab CI 'check-python-tox' passing again following recent Python
> packaging ecosystem changes.
> 
> Signed-off-by: John Snow <jsnow@redhat.com>
> ---
>  python/Makefile          |  4 +-
>  python/setup.cfg         |  1 +
>  python/tests/linters.py  | 79 ++++++++++++++++++++++++++++++++++++++++
>  python/tests/minreqs.txt |  1 +
>  4 files changed, 83 insertions(+), 2 deletions(-)
>  create mode 100644 python/tests/linters.py

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


> +class TestLinters:
> +
> +    def test_flake8_pkg(self):
> +        check_call([sys.executable, "-m", "flake8", "qemu/"])
> +
> +    def test_flake8_scripts(self):
> +        check_call([sys.executable, "-m", "flake8", "scripts/"])
> +
> +    def test_flake8_qapi(self):
> +        check_call([sys.executable, "-m", "flake8",
> +              "../scripts/qapi/",
> +              "../docs/sphinx/qapidoc.py",
> +              "../docs/sphinx/qapi_domain.py"])

Underindented.

> +
> +    def test_isort_pkg(self):
> +        check_call([sys.executable, "-m", "isort", "-c", "qemu/"])
> +
> +    def test_isort_scripts(self):
> +        check_call([sys.executable, "-m", "isort", "-c", "scripts/"])
> +
> +    def test_isort_qapi(self):
> +        check_call([sys.executable, "-m", "isort",
> +                    "--sp", ".", "-c", "../scripts/qapi/"])
> +
> +    def test_isort_qapi_sphinx(self):
> +        # Force isort to recognize 'compat' as a local module and not
> +        # third-party
> +        check_call([sys.executable, "-m", "isort",
> +                    "--sp", ".", "-c", "-p", "compat",
> +                    "../docs/sphinx/qapi_domain.py",
> +                    "../docs/sphinx/qapidoc.py"])
> +

...

> +
> +    def test_pylint_qapi(self):
> +        os.environ['SETUPTOOLS_USE_DISTUTILS'] = 'stdlib'
> +        check_call([sys.executable, "-m", "pylint",
> +              "--rcfile=../scripts/qapi/pylintrc",
> +              "../scripts/qapi/",
> +              "../docs/sphinx/qapidoc.py",
> +              "../docs/sphinx/qapi_domain.py"])

Underindented.

> +
> +    def test_pylint_iotests(self):
> +        os.environ['SETUPTOOLS_USE_DISTUTILS'] = 'stdlib'
> +        check_call([sys.executable, "-m", "linters", "--pylint"],
> +                   cwd="../tests/qemu-iotests/")

With regards,
Daniel
-- 
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|