From 11de6f55f48447325325ec2f334af5576d12e2ed Mon Sep 17 00:00:00 2001 From: Christoph Reiter Date: Sat, 10 Feb 2018 11:37:55 +0100 Subject: tests: switch to pytest as the default test runner. See #153 The TEST_NAMES env var gets translated to work with the pytest syntax (foo.py::class::method). Rename one class which triggers a pytest warning because it starts with "Test" but isn't one. Remove erroring out on Python warnings as pytest triggers some deprecation warnings by default. --- .gitignore | 2 ++ .gitlab-ci/test-docker.sh | 2 +- docs/devguide/building_testing.rst | 9 +++++++++ docs/devguide/dev_environ.rst | 6 ++++-- tests/Makefile.am | 2 +- tests/runtests.py | 16 +++++++++------- tests/test_generictreemodel.py | 16 ++++++++-------- 7 files changed, 34 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 2c676a9c..09063e45 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,8 @@ .deps .libs .idea +.cache +.pytest_cache gschemas.compiled pygobject-*.tar.xz Makefile diff --git a/.gitlab-ci/test-docker.sh b/.gitlab-ci/test-docker.sh index da34d5db..2ec40936 100755 --- a/.gitlab-ci/test-docker.sh +++ b/.gitlab-ci/test-docker.sh @@ -5,7 +5,7 @@ set -e python --version python -m pip install git+https://github.com/pygobject/pycairo.git -python -m pip install flake8 +python -m pip install flake8 pytest PY_PREFIX="$(python -c 'import sys; sys.stdout.write(sys.prefix)')" export PKG_CONFIG_PATH="${PY_PREFIX}/lib/pkgconfig" diff --git a/docs/devguide/building_testing.rst b/docs/devguide/building_testing.rst index f3ac215a..79c73f52 100644 --- a/docs/devguide/building_testing.rst +++ b/docs/devguide/building_testing.rst @@ -2,6 +2,15 @@ Building & Testing ================== +To pass extra arguments to pytest you can set "PYTEST_ADDOPTS": + +.. code:: shell + + # don't hide stdout + export PYTEST_ADDOPTS="-s" + python3 setup.py test + + Using Autotools --------------- diff --git a/docs/devguide/dev_environ.rst b/docs/devguide/dev_environ.rst index 9ca37dd9..081cd775 100644 --- a/docs/devguide/dev_environ.rst +++ b/docs/devguide/dev_environ.rst @@ -14,7 +14,7 @@ on ":ref:`gettingstarted`" first, as they are a pre-requirement. .. code:: console sudo apt build-dep pygobject - sudo apt install autoconf-archive + sudo apt install autoconf-archive python3-pytest python3-flake8 git clone https://gitlab.gnome.org/GNOME/pygobject.git cd pygobject ./autogen.sh @@ -30,7 +30,9 @@ on ":ref:`gettingstarted`" first, as they are a pre-requirement. pacman -S --needed --noconfirm base-devel mingw-w64-i686-toolchain git \ mingw-w64-i686-python3 mingw-w64-i686-python3-cairo \ mingw-w64-i686-gobject-introspection mingw-w64-i686-gtk3 \ - mingw-w64-i686-libffi autoconf-archive + mingw-w64-i686-libffi autoconf-archive mingw-w64-i686-python3-pytest \ + mingw-w64-i686-python3-pip + pip3 install --user flake8 git clone https://gitlab.gnome.org/GNOME/pygobject.git cd pygobject ./autogen.sh diff --git a/tests/Makefile.am b/tests/Makefile.am index 1873ec5f..6c0cedf6 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -167,7 +167,7 @@ RUN_TESTS_ENV_VARS= \ # clobber global name space check-local: $(target_libraries) $(test_typelibs) gschemas.compiled $(RUN_TESTS_ENV_VARS) $(EXTRA_ENV) $(EXEC_NAME) $(PYTHON) -Wd $(srcdir)/runtests.py; rc=$$?; \ - [ "$$rc" -ne 0 ] || [ -n "$$TEST_NAMES" ] || { TEST_NAMES=compat_test_pygtk $(RUN_TESTS_ENV_VARS) $(EXTRA_ENV) $(EXEC_NAME) $(PYTHON) -Wd -Werror::PendingDeprecationWarning -Werror::DeprecationWarning -Werror::RuntimeWarning $(srcdir)/runtests.py; rc=$$?; }; \ + [ "$$rc" -ne 0 ] || [ -n "$$TEST_NAMES" ] || { TEST_NAMES=compat_test_pygtk $(RUN_TESTS_ENV_VARS) $(EXTRA_ENV) $(EXEC_NAME) $(PYTHON) -Wd $(srcdir)/runtests.py; rc=$$?; }; \ exit $$rc check.gdb: diff --git a/tests/runtests.py b/tests/runtests.py index b8f64b99..457553fb 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -9,6 +9,9 @@ import unittest import subprocess import atexit +import pytest + + # this was renamed in Python 3, provide backwards compatible name if sys.version_info[:2] == (2, 7): unittest.TestCase.assertRaisesRegex = unittest.TestCase.assertRaisesRegexp @@ -121,12 +124,11 @@ else: for filename in glob.iglob(os.path.join(mydir, 'test_*.py')): names.append(os.path.basename(filename)[:-3]) -loader = unittest.TestLoader() -suite = loader.loadTestsFromNames(names) + +def unittest_to_pytest_name(name): + parts = name.split(".") + parts[0] = os.path.join(mydir, parts[0] + ".py") + return "::".join(parts) -# Run tests. -runner = unittest.TextTestRunner(verbosity=2) -result = runner.run(suite) -if not result.wasSuccessful(): - sys.exit(1) # exit code so "make check" reports error +sys.exit(pytest.main([unittest_to_pytest_name(n) for n in names])) diff --git a/tests/test_generictreemodel.py b/tests/test_generictreemodel.py index 30ae1250..24301109 100644 --- a/tests/test_generictreemodel.py +++ b/tests/test_generictreemodel.py @@ -55,9 +55,9 @@ class Node(object): return 'Node("%s", %s)' % (self.name, self.value) -class TesterModel(GenericTreeModel): +class ATesterModel(GenericTreeModel): def __init__(self): - super(TesterModel, self).__init__() + super(ATesterModel, self).__init__() self.root = Node('root', 0, Node('spam', 1, Node('sushi', 2), @@ -150,7 +150,7 @@ class TestReferences(unittest.TestCase): self.assertEqual(sys.getrefcount(obj), ref_count + 1) def test_leak_references_on(self): - model = TesterModel() + model = ATesterModel() obj_ref = weakref.ref(model.root) # Initial refcount is 1 for model.root + the temporary self.assertEqual(sys.getrefcount(model.root), 2) @@ -179,7 +179,7 @@ class TestReferences(unittest.TestCase): self.assertEqual(obj_ref(), None) def test_row_deleted_frees_refs(self): - model = TesterModel() + model = ATesterModel() obj_ref = weakref.ref(model.root) # Initial refcount is 1 for model.root + the temporary self.assertEqual(sys.getrefcount(model.root), 2) @@ -200,7 +200,7 @@ class TestReferences(unittest.TestCase): self.assertEqual(obj_ref(), None) def test_leak_references_off(self): - model = TesterModel() + model = ATesterModel() model.leak_references = False obj_ref = weakref.ref(model.root) @@ -226,7 +226,7 @@ class TestReferences(unittest.TestCase): def test_iteration_refs(self): # Pull iterators off the model using the wrapped C API which will # then call back into the python overrides. - model = TesterModel() + model = ATesterModel() nodes = [node for node in model.iter_depth_first()] values = [node.value for node in nodes] @@ -287,7 +287,7 @@ class TestReferences(unittest.TestCase): @unittest.skipUnless(has_gtk, 'Gtk not available') class TestIteration(unittest.TestCase): def test_iter_next_root(self): - model = TesterModel() + model = ATesterModel() it = model.get_iter([0]) self.assertEqual(it.user_data, id(model.root)) self.assertEqual(model.root.next, None) @@ -296,7 +296,7 @@ class TestIteration(unittest.TestCase): self.assertEqual(it, None) def test_iter_next_multiple(self): - model = TesterModel() + model = ATesterModel() it = model.get_iter([0, 0]) self.assertEqual(it.user_data, id(model.root.children[0])) -- cgit v1.2.1