[libvirt] [dbus PATCH] Convert testsuite from unittest to pytest

Pavel Hrdina phrdina at redhat.com
Thu Mar 22 16:32:10 UTC 2018


On Thu, Mar 22, 2018 at 01:06:56PM +0100, Katerina Koukiou wrote:
> Also add flake8 target in tests Makefile and rewrite
> some parts to be more pythonic.
> 
> Signed-off-by: Katerina Koukiou <kkoukiou at redhat.com>
> ---
>  HACKING.md           |  2 +-
>  test/Makefile.am     | 15 ++++++++++++---
>  test/conftest.py     | 15 +++++++++++++++
>  test/libvirttest.py  | 51 ++++++++++++++++++++++++++++-----------------------
>  test/test_connect.py | 35 +++++++++++++++--------------------
>  test/test_domain.py  | 39 +++++++++++++++++----------------------
>  test/travis-run      |  4 ++--
>  7 files changed, 90 insertions(+), 71 deletions(-)
>  create mode 100644 test/conftest.py
> 
> diff --git a/HACKING.md b/HACKING.md
> index 75aa6d0..30d321c 100644
> --- a/HACKING.md
> +++ b/HACKING.md
> @@ -35,7 +35,7 @@ Running from git repository
>      make check
>      ```
>  
> -    The test tool requires python3 and python3-dbus.
> +    The test tool requires python3, pytest and python3-dbus.

We should explicitly say that we need python3-pytest.

>    * To run libvirt-dbus directly from the build dir without installing it
> diff --git a/test/Makefile.am b/test/Makefile.am
> index d3997f3..554c433 100644
> --- a/test/Makefile.am
> +++ b/test/Makefile.am
> @@ -1,17 +1,26 @@
>  test_helpers = \
> -	libvirttest.py
> +	libvirttest.py \
> +	conftest.py
>  
>  test_programs = \
>  	test_connect.py \
>  	test_domain.py
>  
> -TESTS = $(test_programs)
> -
>  EXTRA_DIST = \
>  	$(test_helpers) \
>  	$(test_programs) \
>  	travis-run
>  
> +TEST_PATH=./

Just a hint, this is never a good idea, it's always better to use
absolute path if possible, in this case it could be

TEST_PATH = $(abs_top_srcdir)/test

> +
> +lint:
> +	flake8 --exclude=.tox --ignore=E501

I'm not sure, whether we need to run flake8 on the test files, I would
leave it out for now, we can add it later.

> +
> +test:
> +	py.test --verbose --color=yes $(TEST_PATH)

This would not work as expected, the py.test is the same as py.test-3.

> +
> +check: lint test
> +

We don't need to change the makefile at all since we can make the
test_connect.py and test_domain.py as a standalone executable, see
my notes below.

>  TESTS_ENVIRONMENT = \
>  	abs_top_builddir=$(abs_top_builddir) \
>  	VIRT_DBUS_INTERFACES_DIR=$(abs_top_srcdir)/data
> diff --git a/test/conftest.py b/test/conftest.py
> new file mode 100644
> index 0000000..a468599
> --- /dev/null
> +++ b/test/conftest.py
> @@ -0,0 +1,15 @@
> +import os
> +import subprocess
> +import pytest

Nitpick: alphabetic order is nicer :).

Anyway, the test changes and the rewrite to pytest is OK.  I'm attaching
a patch with changes that would make the tests as standalone executable.
In this case the patch is better then describing it.

If you agree with the changes you can send a v2.

With these changes it should be possible to run "make check" to run
the whole test suite or "./run test/test_connect.py [pytest args]"
to run the single test with pytest arguments.

Thanks for the patch!

Pavel
-------------- next part --------------
diff --git a/HACKING.md b/HACKING.md
index 30d321c..bb22fd6 100644
--- a/HACKING.md
+++ b/HACKING.md
@@ -35,7 +35,7 @@ Running from git repository
     make check
     ```
 
-    The test tool requires python3, pytest and python3-dbus.
+    The test tool requires python3, python3-pytest and python3-dbus.
 
 
   * To run libvirt-dbus directly from the build dir without installing it
diff --git a/test/Makefile.am b/test/Makefile.am
index 554c433..acb2d33 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -11,15 +11,7 @@ EXTRA_DIST = \
 	$(test_programs) \
 	travis-run
 
-TEST_PATH=./
-
-lint:
-	flake8 --exclude=.tox --ignore=E501
-
-test:
-	py.test --verbose --color=yes $(TEST_PATH)
-
-check: lint test
+TESTS = $(test_programs)
 
 TESTS_ENVIRONMENT = \
 	abs_top_builddir=$(abs_top_builddir) \
diff --git a/test/conftest.py b/test/conftest.py
index a468599..3bce7b7 100644
--- a/test/conftest.py
+++ b/test/conftest.py
@@ -1,6 +1,6 @@
 import os
-import subprocess
 import pytest
+import subprocess
 
 
 @pytest.fixture(scope="session", autouse=True)
diff --git a/test/libvirttest.py b/test/libvirttest.py
index fc8c5c4..a7e242e 100644
--- a/test/libvirttest.py
+++ b/test/libvirttest.py
@@ -1,12 +1,12 @@
-#!/usr/bin/python3
-
+from dbus.mainloop.glib import DBusGMainLoop
+from gi.repository import GLib
+import dbus
 import os
+import pytest
 import subprocess
+import sys
 import time
-import pytest
-from gi.repository import GLib
-from dbus.mainloop.glib import DBusGMainLoop
-import dbus
+
 
 ROOT = os.environ.get('abs_top_builddir', os.path.dirname(os.path.dirname(__file__)))
 EXE = os.path.join(ROOT, 'src', 'libvirt-dbus')
@@ -14,6 +14,10 @@ EXE = os.path.join(ROOT, 'src', 'libvirt-dbus')
 DBusGMainLoop(set_as_default=True)
 
 
+def run():
+    exit(pytest.main(sys.argv))
+
+
 class BaseTestClass():
     """ Base test class for whole test suite
     """
diff --git a/test/test_connect.py b/test/test_connect.py
index 14f70d5..a52140c 100755
--- a/test/test_connect.py
+++ b/test/test_connect.py
@@ -1,3 +1,5 @@
+#!/usr/bin/python3
+
 import dbus
 import libvirttest
 
@@ -50,3 +52,7 @@ class TestConnect(libvirttest.BaseTestClass):
         assert isinstance(path, dbus.ObjectPath)
 
         self.main_loop()
+
+
+if __name__ == '__main__':
+    libvirttest.run()
diff --git a/test/test_domain.py b/test/test_domain.py
index b176c29..1bf9c1b 100755
--- a/test/test_domain.py
+++ b/test/test_domain.py
@@ -1,3 +1,5 @@
+#!/usr/bin/python3
+
 import dbus
 import libvirttest
 
@@ -63,3 +65,7 @@ class TestDomain(libvirttest.BaseTestClass):
         domain.Undefine()
 
         self.main_loop()
+
+
+if __name__ == '__main__':
+    libvirttest.run()
diff --git a/test/travis-run b/test/travis-run
index c2ab729..80b6dec 100755
--- a/test/travis-run
+++ b/test/travis-run
@@ -22,7 +22,7 @@ sudo chroot "$CHROOT" << EOF
 set -ex
 # install build deps
 apt-get update
-apt-get install -y dh-autoreconf pkg-config libvirt-dev libglib2.0-dev libtool python3-gi python3-dbus dbus pytest
+apt-get install -y dh-autoreconf pkg-config libvirt-dev libglib2.0-dev libtool python3-gi python3-dbus python3-pytest dbus
 
 # run build and tests as user
 chown -R buildd:buildd /build
@@ -31,6 +31,6 @@ set -ex
 cd /build/src
 ./autogen.sh
 make -j4
-make check
+make check || { cat test-suite.log; exit 1; }
 EOU
 EOF
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20180322/5687f809/attachment-0001.sig>


More information about the libvir-list mailing list