summaryrefslogtreecommitdiff
path: root/tests/__init__.py
diff options
context:
space:
mode:
authorChristoph Reiter <reiter.christoph@gmail.com>2018-02-11 18:08:07 +0100
committerChristoph Reiter <reiter.christoph@gmail.com>2018-02-12 00:25:28 +0100
commit5a25c98cb6d387791e41c7dc240c55814988519a (patch)
tree9b92cbf881cc8458a0532ce686fe6c8f8e7c4970 /tests/__init__.py
parent21cee6cc4fbc7fb1a28a15840924b0da52b49fca (diff)
downloadpygobject-5a25c98cb6d387791e41c7dc240c55814988519a.tar.gz
tests: Make it possible to use pytest directlywip/creiter/pytest-direct
pytest will just import the files passed to it and try to run tests. Since we need to run some setup code convert the tests directory to a Python package and do the initialization in __init__.py. This makes the init code (env vars, typelib search path, dbus) always run when something from the package gets imported. python3 setup.py build_tests # build pygobject and tests py.test-3 tests/test_gi.py # run tests in test_gi.py only
Diffstat (limited to 'tests/__init__.py')
-rw-r--r--tests/__init__.py102
1 files changed, 102 insertions, 0 deletions
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 00000000..4d9b3839
--- /dev/null
+++ b/tests/__init__.py
@@ -0,0 +1,102 @@
+from __future__ import absolute_import
+
+import os
+import sys
+import unittest
+import signal
+import subprocess
+import atexit
+
+
+def init_test_environ():
+ # this was renamed in Python 3, provide backwards compatible name
+ if sys.version_info[:2] == (2, 7):
+ unittest.TestCase.assertRaisesRegex = unittest.TestCase.assertRaisesRegexp
+
+ if sys.version_info[0] == 3:
+ unittest.TestCase.assertRegexpMatches = unittest.TestCase.assertRegex
+ unittest.TestCase.assertRaisesRegexp = unittest.TestCase.assertRaisesRegex
+
+ def dbus_launch_session():
+ if os.name == "nt" or sys.platform == "darwin":
+ return (-1, "")
+
+ try:
+ out = subprocess.check_output([
+ "dbus-daemon", "--session", "--fork", "--print-address=1",
+ "--print-pid=1"])
+ except (subprocess.CalledProcessError, OSError):
+ return (-1, "")
+ else:
+ if sys.version_info[0] == 3:
+ out = out.decode("utf-8")
+ addr, pid = out.splitlines()
+ return int(pid), addr
+
+ pid, addr = dbus_launch_session()
+ if pid >= 0:
+ os.environ["DBUS_SESSION_BUS_ADDRESS"] = addr
+ atexit.register(os.kill, pid, signal.SIGKILL)
+ else:
+ os.environ["DBUS_SESSION_BUS_ADDRESS"] = "."
+
+ tests_builddir = os.path.abspath(os.environ.get('TESTS_BUILDDIR', os.path.dirname(__file__)))
+ builddir = os.path.dirname(tests_builddir)
+ tests_srcdir = os.path.abspath(os.path.dirname(__file__))
+ srcdir = os.path.dirname(tests_srcdir)
+
+ sys.path.insert(0, tests_srcdir)
+ sys.path.insert(0, srcdir)
+ sys.path.insert(0, tests_builddir)
+ sys.path.insert(0, builddir)
+
+ # force untranslated messages, as we check for them in some tests
+ os.environ['LC_MESSAGES'] = 'C'
+ os.environ['G_DEBUG'] = 'fatal-warnings fatal-criticals'
+ if sys.platform == "darwin":
+ # gtk 3.22 has warnings and ciriticals on OS X, ignore for now
+ os.environ['G_DEBUG'] = ''
+
+ # make Gio able to find our gschemas.compiled in tests/. This needs to be set
+ # before importing Gio. Support a separate build tree, so look in build dir
+ # first.
+ os.environ['GSETTINGS_BACKEND'] = 'memory'
+ os.environ['GSETTINGS_SCHEMA_DIR'] = tests_builddir
+ os.environ['G_FILENAME_ENCODING'] = 'UTF-8'
+
+ import gi
+ gi.require_version("GIRepository", "2.0")
+ from gi.repository import GIRepository
+ repo = GIRepository.Repository.get_default()
+ repo.prepend_library_path(os.path.join(tests_builddir))
+ repo.prepend_library_path(os.path.join(tests_builddir, ".libs"))
+ repo.prepend_search_path(tests_builddir)
+
+ def try_require_version(namespace, version):
+ try:
+ gi.require_version(namespace, version)
+ except ValueError:
+ # prevent tests from running with the wrong version
+ sys.modules["gi.repository." + namespace] = None
+
+ # Optional
+ try_require_version("Gtk", os.environ.get("TEST_GTK_VERSION", "3.0"))
+ try_require_version("Gdk", os.environ.get("TEST_GTK_VERSION", "3.0"))
+ try_require_version("GdkPixbuf", "2.0")
+ try_require_version("Pango", "1.0")
+ try_require_version("PangoCairo", "1.0")
+ try_require_version("Atk", "1.0")
+
+ # Required
+ gi.require_versions({
+ "GIMarshallingTests": "1.0",
+ "Regress": "1.0",
+ "GLib": "2.0",
+ "Gio": "2.0",
+ "GObject": "2.0",
+ })
+
+
+init_test_environ()
+
+__path__ = __import__('pkgutil').extend_path(__path__, __name__)