release
[pygobject.git] / tests / runtests.py
blob457553fb0bff7d9c97f0c2dc0ca2fe910434d5f6
1 #!/usr/bin/env python
2 # -*- Mode: Python -*-
4 import os
5 import glob
6 import sys
7 import signal
8 import unittest
9 import subprocess
10 import atexit
12 import pytest
15 # this was renamed in Python 3, provide backwards compatible name
16 if sys.version_info[:2] == (2, 7):
17 unittest.TestCase.assertRaisesRegex = unittest.TestCase.assertRaisesRegexp
19 if sys.version_info[0] == 3:
20 unittest.TestCase.assertRegexpMatches = unittest.TestCase.assertRegex
21 unittest.TestCase.assertRaisesRegexp = unittest.TestCase.assertRaisesRegex
23 if '--help' in sys.argv:
24 print("Usage: ./runtests.py <testfiles>")
25 sys.exit(0)
28 def dbus_launch_session():
29 if os.name == "nt" or sys.platform == "darwin":
30 return (-1, "")
32 try:
33 out = subprocess.check_output([
34 "dbus-daemon", "--session", "--fork", "--print-address=1",
35 "--print-pid=1"])
36 except (subprocess.CalledProcessError, OSError):
37 return (-1, "")
38 else:
39 if sys.version_info[0] == 3:
40 out = out.decode("utf-8")
41 addr, pid = out.splitlines()
42 return int(pid), addr
45 pid, addr = dbus_launch_session()
46 if pid >= 0:
47 os.environ["DBUS_SESSION_BUS_ADDRESS"] = addr
48 atexit.register(os.kill, pid, signal.SIGKILL)
49 else:
50 os.environ["DBUS_SESSION_BUS_ADDRESS"] = "."
52 mydir = os.path.dirname(os.path.abspath(__file__))
53 tests_builddir = os.path.abspath(os.environ.get('TESTS_BUILDDIR', os.path.dirname(__file__)))
54 builddir = os.path.dirname(tests_builddir)
55 tests_srcdir = os.path.abspath(os.path.dirname(__file__))
56 srcdir = os.path.dirname(tests_srcdir)
58 sys.path.insert(0, tests_srcdir)
59 sys.path.insert(0, srcdir)
60 sys.path.insert(0, tests_builddir)
61 sys.path.insert(0, builddir)
63 # force untranslated messages, as we check for them in some tests
64 os.environ['LC_MESSAGES'] = 'C'
65 os.environ['G_DEBUG'] = 'fatal-warnings fatal-criticals'
66 if sys.platform == "darwin":
67 # gtk 3.22 has warnings and ciriticals on OS X, ignore for now
68 os.environ['G_DEBUG'] = ''
70 # make Gio able to find our gschemas.compiled in tests/. This needs to be set
71 # before importing Gio. Support a separate build tree, so look in build dir
72 # first.
73 os.environ['GSETTINGS_BACKEND'] = 'memory'
74 os.environ['GSETTINGS_SCHEMA_DIR'] = tests_builddir
75 os.environ['G_FILENAME_ENCODING'] = 'UTF-8'
77 import gi
78 gi.require_version("GIRepository", "2.0")
79 from gi.repository import GIRepository
80 repo = GIRepository.Repository.get_default()
81 repo.prepend_library_path(os.path.join(tests_builddir))
82 repo.prepend_library_path(os.path.join(tests_builddir, ".libs"))
83 repo.prepend_search_path(tests_builddir)
86 def try_require_version(namespace, version):
87 try:
88 gi.require_version(namespace, version)
89 except ValueError:
90 # prevent tests from running with the wrong version
91 sys.modules["gi.repository." + namespace] = None
94 # Optional
95 try_require_version("Gtk", os.environ.get("TEST_GTK_VERSION", "3.0"))
96 try_require_version("Gdk", os.environ.get("TEST_GTK_VERSION", "3.0"))
97 try_require_version("GdkPixbuf", "2.0")
98 try_require_version("Pango", "1.0")
99 try_require_version("PangoCairo", "1.0")
100 try_require_version("Atk", "1.0")
102 # Required
103 gi.require_versions({
104 "GIMarshallingTests": "1.0",
105 "Regress": "1.0",
106 "GLib": "2.0",
107 "Gio": "2.0",
108 "GObject": "2.0",
111 # Load tests.
112 if 'TEST_NAMES' in os.environ:
113 names = os.environ['TEST_NAMES'].split()
114 elif 'TEST_FILES' in os.environ:
115 names = []
116 for filename in os.environ['TEST_FILES'].split():
117 names.append(filename[:-3])
118 elif len(sys.argv) > 1:
119 names = []
120 for filename in sys.argv[1:]:
121 names.append(filename.replace('.py', ''))
122 else:
123 names = []
124 for filename in glob.iglob(os.path.join(mydir, 'test_*.py')):
125 names.append(os.path.basename(filename)[:-3])
128 def unittest_to_pytest_name(name):
129 parts = name.split(".")
130 parts[0] = os.path.join(mydir, parts[0] + ".py")
131 return "::".join(parts)
134 sys.exit(pytest.main([unittest_to_pytest_name(n) for n in names]))