[pygobject/wip/creiter/pytest-direct] tests: Make it possible to use pytest directly



commit 5a25c98cb6d387791e41c7dc240c55814988519a
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Sun Feb 11 18:08:07 2018 +0100

    tests: Make it possible to use pytest directly
    
    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

 docs/devguide/building_testing.rst |   6 ++
 setup.py                           |  10 +--
 tests/Makefile.am                  |   8 ++-
 tests/__init__.py                  | 102 ++++++++++++++++++++++++++
 tests/helper.py                    |   4 +-
 tests/runtests.py                  | 143 +++++++------------------------------
 tests/test_atoms.py                |   4 +-
 tests/test_cairo.py                |   6 +-
 tests/test_docstring.py            |   2 +
 tests/test_error.py                |   6 +-
 tests/test_everything.py           |   6 +-
 tests/test_fields.py               |   2 +
 tests/test_gdbus.py                |   2 +
 tests/test_generictreemodel.py     |   5 +-
 tests/test_gi.py                   |   6 +-
 tests/test_gio.py                  |   4 +-
 tests/test_glib.py                 |   2 +
 tests/test_gobject.py              |   8 +--
 tests/test_gtype.py                |   2 +
 tests/test_import_machinery.py     |   2 +
 tests/test_interface.py            |   4 +-
 tests/test_internal_api.py         |  10 ++-
 tests/test_iochannel.py            |   6 +-
 tests/test_mainloop.py             |   2 +
 tests/test_object_marshaling.py    |   2 +
 tests/test_option.py               |   2 +
 tests/test_ossig.py                |   2 +
 tests/test_overrides_gdk.py        |   4 +-
 tests/test_overrides_glib.py       |   4 +-
 tests/test_overrides_gtk.py        |   4 +-
 tests/test_overrides_pango.py      |   2 +
 tests/test_properties.py           |  10 ++-
 tests/test_pygtkcompat.py          |   4 +-
 tests/test_repository.py           |   8 +--
 tests/test_resulttuple.py          |   2 +
 tests/test_signal.py               |  13 ++--
 tests/test_source.py               |   8 +--
 tests/test_subprocess.py           |   2 +
 tests/test_thread.py               |   5 +-
 tests/test_typeclass.py            |   6 +-
 tests/test_unknown.py              |   5 +-
 tests/testmodule.py                |   2 +
 42 files changed, 244 insertions(+), 193 deletions(-)
---
diff --git a/docs/devguide/building_testing.rst b/docs/devguide/building_testing.rst
index 79c73f52..f2c9624e 100644
--- a/docs/devguide/building_testing.rst
+++ b/docs/devguide/building_testing.rst
@@ -53,6 +53,9 @@ Using Setuptools
     # Build in-tree
     python3 setup.py build_ext --inplace
 
+    # Build in-tree including tests
+    python3 setup.py build_tests
+
     # Executing some code after the build
     PYTHONPATH=. python3 foo.py
 
@@ -64,5 +67,8 @@ Using Setuptools
     TEST_NAMES=test_gi.TestUtf8 python3 setup.py test
     TEST_NAMES=test_gi.TestUtf8.test_utf8_full_return python3 setup.py test
 
+    # using pytest directly
+    py.test-3 tests/test_gi.py
+
     # Running flake8 tests
     python3 setup.py quality
diff --git a/setup.py b/setup.py
index fd781092..8bddefc3 100755
--- a/setup.py
+++ b/setup.py
@@ -237,6 +237,11 @@ class build_tests(Command):
             return True
 
     def run(self):
+        cmd = self.reinitialize_command("build_ext")
+        cmd.inplace = True
+        cmd.ensure_finalized()
+        cmd.run()
+
         from distutils.ccompiler import new_compiler
         from distutils.sysconfig import customize_compiler
 
@@ -440,11 +445,6 @@ class test(Command):
         pass
 
     def run(self):
-        cmd = self.reinitialize_command("build_ext")
-        cmd.inplace = True
-        cmd.ensure_finalized()
-        cmd.run()
-
         cmd = self.reinitialize_command("build_tests")
         cmd.ensure_finalized()
         cmd.run()
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 4a9ec2aa..d9122ece 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -163,10 +163,14 @@ RUN_TESTS_ENV_VARS= \
        MSYSTEM= \
        TESTS_BUILDDIR=$(builddir)
 
+# for non-srcdir builds
+$(abs_builddir)/__init__.py: $(abs_srcdir)/__init__.py
+       echo "__path__ = __import__('pkgutil').extend_path(__path__, __name__)" > $@
+
 # pygtkcompat tests need to be run in a separate process as they
 # 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;
+check-local: $(target_libraries) $(test_typelibs) gschemas.compiled $(abs_builddir)/__init__.py
+       $(RUN_TESTS_ENV_VARS) $(EXTRA_ENV) $(EXEC_NAME) $(PYTHON) $(srcdir)/runtests.py;
 
 check.gdb:
        EXEC_NAME="gdb --args" $(MAKE) check
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__)
diff --git a/tests/helper.py b/tests/helper.py
index 0d2f204c..b683a4af 100644
--- a/tests/helper.py
+++ b/tests/helper.py
@@ -1,3 +1,5 @@
+from __future__ import absolute_import
+
 import contextlib
 import unittest
 import inspect
@@ -10,7 +12,7 @@ import gi
 from gi import PyGIDeprecationWarning
 from gi.repository import GLib
 
-from compathelper import StringIO
+from .compathelper import StringIO
 
 
 ExceptionInfo = namedtuple("ExceptionInfo", ["type", "value", "traceback"])
diff --git a/tests/runtests.py b/tests/runtests.py
index eeeb9b27..652dc99f 100755
--- a/tests/runtests.py
+++ b/tests/runtests.py
@@ -1,134 +1,41 @@
 #!/usr/bin/env python
 # -*- Mode: Python -*-
 
+from __future__ import absolute_import
+
 import os
-import glob
 import sys
-import signal
-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
-
-if sys.version_info[0] == 3:
-    unittest.TestCase.assertRegexpMatches = unittest.TestCase.assertRegex
-    unittest.TestCase.assertRaisesRegexp = unittest.TestCase.assertRaisesRegex
-
-if '--help' in sys.argv:
-    print("Usage: ./runtests.py <testfiles>")
-    sys.exit(0)
+def main(argv):
+    if '--help' in argv:
+        print("Usage: ./runtests.py <testfiles>")
+        return
 
+    mydir = os.path.dirname(os.path.abspath(__file__))
 
-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, "")
+    if 'TEST_NAMES' in os.environ:
+        names = os.environ['TEST_NAMES'].split()
+    elif 'TEST_FILES' in os.environ:
+        names = []
+        for filename in os.environ['TEST_FILES'].split():
+            names.append(filename[:-3])
+    elif len(argv) > 1:
+        names = []
+        for filename in argv[1:]:
+            names.append(filename.replace('.py', ''))
     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"] = "."
-
-mydir = os.path.dirname(os.path.abspath(__file__))
-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",
-})
-
-# Load tests.
-if 'TEST_NAMES' in os.environ:
-    names = os.environ['TEST_NAMES'].split()
-elif 'TEST_FILES' in os.environ:
-    names = []
-    for filename in os.environ['TEST_FILES'].split():
-        names.append(filename[:-3])
-elif len(sys.argv) > 1:
-    names = []
-    for filename in sys.argv[1:]:
-        names.append(filename.replace('.py', ''))
-else:
-    names = []
-    for filename in sorted(glob.iglob(os.path.join(mydir, 'test_*.py'))):
-        names.append(os.path.basename(filename)[:-3])
+        return pytest.main([mydir])
 
+    def unittest_to_pytest_name(name):
+        parts = name.split(".")
+        parts[0] = os.path.join(mydir, parts[0] + ".py")
+        return "::".join(parts)
 
-def unittest_to_pytest_name(name):
-    parts = name.split(".")
-    parts[0] = os.path.join(mydir, parts[0] + ".py")
-    return "::".join(parts)
+    return pytest.main([unittest_to_pytest_name(n) for n in names])
 
 
-sys.exit(pytest.main([unittest_to_pytest_name(n) for n in names]))
+if __name__ == "__main__":
+    sys.exit(main(sys.argv))
diff --git a/tests/test_atoms.py b/tests/test_atoms.py
index a2c2d5be..0793d3ff 100644
--- a/tests/test_atoms.py
+++ b/tests/test_atoms.py
@@ -1,3 +1,5 @@
+from __future__ import absolute_import
+
 import os
 import sys
 import unittest
@@ -9,7 +11,7 @@ except ImportError:
     Atk = None
     Gtk = None
 
-from helper import capture_glib_deprecation_warnings
+from .helper import capture_glib_deprecation_warnings
 
 
 @unittest.skipUnless(Gdk, 'Gdk not available')
diff --git a/tests/test_cairo.py b/tests/test_cairo.py
index 06289e24..8ba5553f 100644
--- a/tests/test_cairo.py
+++ b/tests/test_cairo.py
@@ -2,6 +2,8 @@
 # coding=utf-8
 # vim: tabstop=4 shiftwidth=4 expandtab
 
+from __future__ import absolute_import
+
 import unittest
 
 import gi
@@ -158,7 +160,3 @@ class TestSignalMarshaling(unittest.TestCase):
         result = self.pass_object_through_signal(pattern, self.tester.sig_pattern)
         self.assertTrue(isinstance(result, cairo.Pattern))
         self.assertTrue(isinstance(result, cairo.SolidPattern))
-
-
-if __name__ == '__main__':
-    unittest.main()
diff --git a/tests/test_docstring.py b/tests/test_docstring.py
index 29b7e5ef..adee174c 100644
--- a/tests/test_docstring.py
+++ b/tests/test_docstring.py
@@ -1,3 +1,5 @@
+from __future__ import absolute_import
+
 import unittest
 
 import gi.docstring
diff --git a/tests/test_error.py b/tests/test_error.py
index 57024900..3f483265 100644
--- a/tests/test_error.py
+++ b/tests/test_error.py
@@ -22,6 +22,8 @@
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 # USA
 
+from __future__ import absolute_import
+
 import unittest
 
 from gi.repository import GLib
@@ -138,7 +140,3 @@ class TestMarshalling(unittest.TestCase):
         error1 = GLib.Error.new_literal(1, "error", 1)
 
         GIMarshallingTests.compare_two_gerrors_in_gvalue(error, error1)
-
-
-if __name__ == '__main__':
-    unittest.main()
diff --git a/tests/test_everything.py b/tests/test_everything.py
index e206954a..ce79cc2f 100644
--- a/tests/test_everything.py
+++ b/tests/test_everything.py
@@ -2,6 +2,8 @@
 # coding=utf-8
 # vim: tabstop=4 shiftwidth=4 expandtab
 
+from __future__ import absolute_import
+
 import unittest
 import traceback
 import ctypes
@@ -20,8 +22,8 @@ try:
 except:
     Gtk = None
 
-from compathelper import PY3
-from helper import capture_exceptions
+from .compathelper import PY3
+from .helper import capture_exceptions
 
 
 if sys.version_info < (3, 0):
diff --git a/tests/test_fields.py b/tests/test_fields.py
index ac09949f..0181d288 100644
--- a/tests/test_fields.py
+++ b/tests/test_fields.py
@@ -1,6 +1,8 @@
 # -*- Mode: Python; py-indent-offset: 4 -*-
 # coding=utf-8
 
+from __future__ import absolute_import
+
 import math
 import unittest
 
diff --git a/tests/test_gdbus.py b/tests/test_gdbus.py
index 5fb4f5d8..2e96eb8f 100644
--- a/tests/test_gdbus.py
+++ b/tests/test_gdbus.py
@@ -1,6 +1,8 @@
 # -*- Mode: Python; py-indent-offset: 4 -*-
 # vim: tabstop=4 shiftwidth=4 expandtab
 
+from __future__ import absolute_import
+
 import unittest
 
 from gi.repository import GLib
diff --git a/tests/test_generictreemodel.py b/tests/test_generictreemodel.py
index 24301109..d99c0560 100644
--- a/tests/test_generictreemodel.py
+++ b/tests/test_generictreemodel.py
@@ -17,6 +17,7 @@
 # You should have received a copy of the GNU Lesser General Public
 # License along with this library; if not, see <http://www.gnu.org/licenses/>.
 
+from __future__ import absolute_import
 
 # system
 import gc
@@ -414,7 +415,3 @@ class TestReturnsAfterError(unittest.TestCase):
         with ExceptHook(NotImplementedError):
             res = self.model.iter_parent(child)
         self.assertEqual(res, None)
-
-
-if __name__ == '__main__':
-    unittest.main()
diff --git a/tests/test_gi.py b/tests/test_gi.py
index 9be28dbe..31085206 100644
--- a/tests/test_gi.py
+++ b/tests/test_gi.py
@@ -2,6 +2,8 @@
 # coding=utf-8
 # vim: tabstop=4 shiftwidth=4 expandtab
 
+from __future__ import absolute_import
+
 import sys
 
 import unittest
@@ -21,8 +23,8 @@ from gi.repository import GObject, GLib, Gio
 
 from gi.repository import GIMarshallingTests
 
-from compathelper import PY2, PY3
-from helper import capture_exceptions
+from .compathelper import PY2, PY3
+from .helper import capture_exceptions
 
 
 CONSTANT_UTF8 = "const ♥ utf8"
diff --git a/tests/test_gio.py b/tests/test_gio.py
index c7239ce8..92159c17 100644
--- a/tests/test_gio.py
+++ b/tests/test_gio.py
@@ -1,6 +1,8 @@
 # -*- Mode: Python; py-indent-offset: 4 -*-
 # vim: tabstop=4 shiftwidth=4 expandtab
 
+from __future__ import absolute_import
+
 import os
 import unittest
 import warnings
@@ -9,7 +11,7 @@ import gi.overrides
 from gi import PyGIWarning
 from gi.repository import GLib, Gio
 
-from helper import ignore_gi_deprecation_warnings
+from .helper import ignore_gi_deprecation_warnings
 
 
 class TestGio(unittest.TestCase):
diff --git a/tests/test_glib.py b/tests/test_glib.py
index fa48cdb5..7a782e9c 100644
--- a/tests/test_glib.py
+++ b/tests/test_glib.py
@@ -1,6 +1,8 @@
 # -*- Mode: Python -*-
 # encoding: UTF-8
 
+from __future__ import absolute_import
+
 import os
 import sys
 import unittest
diff --git a/tests/test_gobject.py b/tests/test_gobject.py
index c380d721..a72c41a8 100644
--- a/tests/test_gobject.py
+++ b/tests/test_gobject.py
@@ -1,5 +1,7 @@
 # -*- Mode: Python -*-
 
+from __future__ import absolute_import
+
 import sys
 import gc
 import unittest
@@ -10,7 +12,7 @@ from gi import PyGIDeprecationWarning
 from gi.module import get_introspection_module
 from gi import _gi
 
-import testhelper
+from . import testhelper
 
 
 class TestGObjectAPI(unittest.TestCase):
@@ -694,7 +696,3 @@ class TestGValue(unittest.TestCase):
         value = GObject.Value(GLib.Error)
         self.assertEqual(value.g_type, GObject.type_from_name('GError'))
         self.assertEqual(value.get_value(), None)
-
-
-if __name__ == '__main__':
-    unittest.main()
diff --git a/tests/test_gtype.py b/tests/test_gtype.py
index 80991019..26b7ff58 100644
--- a/tests/test_gtype.py
+++ b/tests/test_gtype.py
@@ -1,3 +1,5 @@
+from __future__ import absolute_import
+
 import unittest
 
 from gi.repository import GObject
diff --git a/tests/test_import_machinery.py b/tests/test_import_machinery.py
index f1a5f30c..27377cd6 100644
--- a/tests/test_import_machinery.py
+++ b/tests/test_import_machinery.py
@@ -1,6 +1,8 @@
 # -*- Mode: Python; py-indent-offset: 4 -*-
 # vim: tabstop=4 shiftwidth=4 expandtab
 
+from __future__ import absolute_import
+
 import sys
 import unittest
 
diff --git a/tests/test_interface.py b/tests/test_interface.py
index ba20cb41..bed37f36 100644
--- a/tests/test_interface.py
+++ b/tests/test_interface.py
@@ -1,9 +1,11 @@
 # -*- Mode: Python -*-
 
+from __future__ import absolute_import
+
 import unittest
 
 from gi.repository import GObject
-import testhelper
+from . import testhelper
 
 
 GUnknown = GObject.type_from_name("TestUnknown")
diff --git a/tests/test_internal_api.py b/tests/test_internal_api.py
index eb66fddc..0cf3e6fd 100644
--- a/tests/test_internal_api.py
+++ b/tests/test_internal_api.py
@@ -1,11 +1,13 @@
 # -*- Mode: Python -*-
 
+from __future__ import absolute_import
+
 import unittest
 
 from gi.repository import GLib, GObject
 
-import testhelper
-import testmodule
+from . import testhelper
+from . import testmodule
 
 
 class TestObject(unittest.TestCase):
@@ -67,7 +69,3 @@ class TestErrors(unittest.TestCase):
     def test_no_gerror(self):
         callable_ = lambda: GLib.file_get_contents(__file__)
         self.assertEqual(testhelper.test_gerror_exception(callable_), None)
-
-
-if __name__ == '__main__':
-    unittest.main()
diff --git a/tests/test_iochannel.py b/tests/test_iochannel.py
index 95b8d1f8..56a0aea9 100644
--- a/tests/test_iochannel.py
+++ b/tests/test_iochannel.py
@@ -1,6 +1,8 @@
 # -*- Mode: Python -*-
 # encoding: UTF-8
 
+from __future__ import absolute_import
+
 import os
 import unittest
 import tempfile
@@ -474,7 +476,3 @@ second line
             self.assertEqual(GLib.IOFlags.NONBLOCK, GLib.IO_FLAG_NONBLOCK)
             self.assertEqual(GLib.IOFlags.IS_SEEKABLE, GLib.IO_FLAG_IS_SEEKABLE)
             self.assertEqual(GLib.IOStatus.NORMAL, GLib.IO_STATUS_NORMAL)
-
-
-if __name__ == '__main__':
-    unittest.main()
diff --git a/tests/test_mainloop.py b/tests/test_mainloop.py
index fda6787a..1c1b1227 100644
--- a/tests/test_mainloop.py
+++ b/tests/test_mainloop.py
@@ -1,5 +1,7 @@
 # -*- Mode: Python -*-
 
+from __future__ import absolute_import
+
 import os
 import sys
 import select
diff --git a/tests/test_object_marshaling.py b/tests/test_object_marshaling.py
index c881a9ab..4fce5611 100644
--- a/tests/test_object_marshaling.py
+++ b/tests/test_object_marshaling.py
@@ -1,6 +1,8 @@
 # -*- Mode: Python; py-indent-offset: 4 -*-
 # vim: tabstop=4 shiftwidth=4 expandtab
 
+from __future__ import absolute_import
+
 import unittest
 import weakref
 import gc
diff --git a/tests/test_option.py b/tests/test_option.py
index fe257464..33a12882 100644
--- a/tests/test_option.py
+++ b/tests/test_option.py
@@ -1,5 +1,7 @@
 #!/usr/bin/env python
 
+from __future__ import absolute_import
+
 import unittest
 import sys
 
diff --git a/tests/test_ossig.py b/tests/test_ossig.py
index 0b6eea65..b59f2f5a 100644
--- a/tests/test_ossig.py
+++ b/tests/test_ossig.py
@@ -14,6 +14,8 @@
 # You should have received a copy of the GNU Lesser General Public
 # License along with this library; if not, see <http://www.gnu.org/licenses/>.
 
+from __future__ import absolute_import
+
 import os
 import signal
 import unittest
diff --git a/tests/test_overrides_gdk.py b/tests/test_overrides_gdk.py
index 14527375..1dfe8e31 100644
--- a/tests/test_overrides_gdk.py
+++ b/tests/test_overrides_gdk.py
@@ -1,6 +1,8 @@
 # -*- Mode: Python; py-indent-offset: 4 -*-
 # vim: tabstop=4 shiftwidth=4 expandtab
 
+from __future__ import absolute_import
+
 import os
 import sys
 import unittest
@@ -15,7 +17,7 @@ except ImportError:
     Gdk = None
     Gdk_version = None
 
-from helper import capture_glib_deprecation_warnings
+from .helper import capture_glib_deprecation_warnings
 
 
 @unittest.skipUnless(Gdk, 'Gdk not available')
diff --git a/tests/test_overrides_glib.py b/tests/test_overrides_glib.py
index a9d5b58b..6dcfd586 100644
--- a/tests/test_overrides_glib.py
+++ b/tests/test_overrides_glib.py
@@ -1,12 +1,14 @@
 # -*- Mode: Python; py-indent-offset: 4 -*-
 # vim: tabstop=4 shiftwidth=4 expandtab
 
+from __future__ import absolute_import
+
 import gc
 import unittest
 
 import gi
 from gi.repository import GLib
-from compathelper import _long
+from .compathelper import _long
 
 
 class TestGVariant(unittest.TestCase):
diff --git a/tests/test_overrides_gtk.py b/tests/test_overrides_gtk.py
index 338ca72c..aa55564a 100644
--- a/tests/test_overrides_gtk.py
+++ b/tests/test_overrides_gtk.py
@@ -2,13 +2,15 @@
 # coding: UTF-8
 # vim: tabstop=4 shiftwidth=4 expandtab
 
+from __future__ import absolute_import
+
 import contextlib
 import unittest
 import time
 import sys
 import warnings
 
-from helper import ignore_gi_deprecation_warnings, capture_glib_warnings
+from .helper import ignore_gi_deprecation_warnings, capture_glib_warnings
 
 import gi.overrides
 import gi.types
diff --git a/tests/test_overrides_pango.py b/tests/test_overrides_pango.py
index 5c09a6ac..a789715d 100644
--- a/tests/test_overrides_pango.py
+++ b/tests/test_overrides_pango.py
@@ -1,6 +1,8 @@
 # -*- Mode: Python; py-indent-offset: 4 -*-
 # vim: tabstop=4 shiftwidth=4 expandtab
 
+from __future__ import absolute_import
+
 import unittest
 
 try:
diff --git a/tests/test_properties.py b/tests/test_properties.py
index ce035cd7..268ee93a 100644
--- a/tests/test_properties.py
+++ b/tests/test_properties.py
@@ -1,5 +1,7 @@
 # coding=utf-8
 
+from __future__ import absolute_import
+
 import os
 import gc
 import sys
@@ -27,8 +29,8 @@ from gi.repository import GIMarshallingTests
 from gi.repository import Regress
 from gi import _propertyhelper as propertyhelper
 
-from compathelper import _long
-from helper import capture_glib_warnings, capture_output
+from .compathelper import _long
+from .helper import capture_glib_warnings, capture_output
 
 
 class PropertyObject(GObject.GObject):
@@ -1317,7 +1319,3 @@ class TestCGetPropertyMethod(CPropertiesTestBase, unittest.TestCase):
 
     def set_prop(self, obj, name, value):
         obj.set_property(name, value)
-
-
-if __name__ == '__main__':
-    unittest.main()
diff --git a/tests/test_pygtkcompat.py b/tests/test_pygtkcompat.py
index 3580ca4c..da220b59 100644
--- a/tests/test_pygtkcompat.py
+++ b/tests/test_pygtkcompat.py
@@ -1,13 +1,15 @@
 # -*- Mode: Python; py-indent-offset: 4 -*-
 # vim: tabstop=4 shiftwidth=4 expandtab
 
+from __future__ import absolute_import
+
 import unittest
 import base64
 
 import pygtkcompat
 from pygtkcompat.pygtkcompat import _disable_all as disable_all
 
-from helper import capture_gi_deprecation_warnings, capture_glib_warnings
+from .helper import capture_gi_deprecation_warnings, capture_glib_warnings
 
 try:
     from gi.repository import Gtk, Gdk
diff --git a/tests/test_repository.py b/tests/test_repository.py
index d7b6d2e1..a074a35f 100644
--- a/tests/test_repository.py
+++ b/tests/test_repository.py
@@ -20,6 +20,8 @@
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 # USA
 
+from __future__ import absolute_import
+
 import unittest
 import collections
 
@@ -29,7 +31,7 @@ from gi.repository import GObject
 from gi.repository import GIMarshallingTests
 from gi.repository import GIRepository as IntrospectedRepository
 
-from helper import capture_glib_warnings
+from .helper import capture_glib_warnings
 
 
 def find_child_info(info, getter_name, name):
@@ -384,7 +386,3 @@ class Test(unittest.TestCase):
         IntrospectedRepository.Argument.__info__ = 'not an info'
         self.assertRaises(TypeError, IntrospectedRepository.Argument)
         IntrospectedRepository.Argument.__info__ = old_info
-
-
-if __name__ == '__main__':
-    unittest.main()
diff --git a/tests/test_resulttuple.py b/tests/test_resulttuple.py
index 20f80f3c..1a4aadf9 100644
--- a/tests/test_resulttuple.py
+++ b/tests/test_resulttuple.py
@@ -18,6 +18,8 @@
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 # USA
 
+from __future__ import absolute_import
+
 import unittest
 import pickle
 
diff --git a/tests/test_signal.py b/tests/test_signal.py
index 642708bf..35fc8ef3 100644
--- a/tests/test_signal.py
+++ b/tests/test_signal.py
@@ -1,5 +1,7 @@
 # -*- Mode: Python -*-
 
+from __future__ import absolute_import
+
 import gc
 import unittest
 import sys
@@ -9,11 +11,12 @@ import time
 
 from gi.repository import GObject, GLib, Regress, Gio
 from gi import _signalhelper as signalhelper
-import testhelper
-from compathelper import _long
-from helper import capture_glib_warnings, capture_gi_deprecation_warnings
 from gi.module import repository as repo
 
+from . import testhelper
+from .compathelper import _long
+from .helper import capture_glib_warnings, capture_gi_deprecation_warnings
+
 
 class C(GObject.GObject):
     __gsignals__ = {'my_signal': (GObject.SignalFlags.RUN_FIRST, None,
@@ -1541,7 +1544,3 @@ class TestClosureRefCycle(unittest.TestCase):
 
         self.assertEqual(len(called), 1)
         self.assertTrue(called[0].__grefcount__ > 0)
-
-
-if __name__ == '__main__':
-    unittest.main()
diff --git a/tests/test_source.py b/tests/test_source.py
index b5447341..64fe5bd2 100644
--- a/tests/test_source.py
+++ b/tests/test_source.py
@@ -1,5 +1,7 @@
 # -*- Mode: Python -*-
 
+from __future__ import absolute_import
+
 import sys
 import unittest
 import warnings
@@ -7,7 +9,7 @@ import warnings
 from gi.repository import GLib
 from gi import PyGIDeprecationWarning
 
-from helper import capture_glib_warnings
+from .helper import capture_glib_warnings
 
 
 class Idle(GLib.Idle):
@@ -421,7 +423,3 @@ class TestUserData(unittest.TestCase):
         GLib.idle_add(self.cb_with_data, data)
         self.loop.run()
         self.assertTrue(data['called'])
-
-
-if __name__ == '__main__':
-    unittest.main()
diff --git a/tests/test_subprocess.py b/tests/test_subprocess.py
index deea58fb..3ffdf934 100644
--- a/tests/test_subprocess.py
+++ b/tests/test_subprocess.py
@@ -1,5 +1,7 @@
 # -*- Mode: Python -*-
 
+from __future__ import absolute_import
+
 import sys
 import os
 import unittest
diff --git a/tests/test_thread.py b/tests/test_thread.py
index 3da3310e..e2bbda08 100644
--- a/tests/test_thread.py
+++ b/tests/test_thread.py
@@ -1,10 +1,13 @@
 # -*- Mode: Python -*-
 
+from __future__ import absolute_import
+
 import unittest
-import testhelper
 
 from gi.repository import GLib
 
+from . import testhelper
+
 
 class TestThread(unittest.TestCase):
     def setUp(self):
diff --git a/tests/test_typeclass.py b/tests/test_typeclass.py
index 3ece684e..b584fdd9 100644
--- a/tests/test_typeclass.py
+++ b/tests/test_typeclass.py
@@ -20,6 +20,8 @@
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 # USA
 
+from __future__ import absolute_import
+
 import unittest
 
 from gi.repository import GObject
@@ -74,7 +76,3 @@ class TestTypeClassMethodsMovedToClass(unittest.TestCase):
     def test_find_child_property(self):
         pspec = GIMarshallingTests.PropertiesObject.find_property('some-int')
         self.assertEqual(pspec.name, 'some-int')
-
-
-if __name__ == '__main__':
-    unittest.main()
diff --git a/tests/test_unknown.py b/tests/test_unknown.py
index 20f30055..e483003d 100644
--- a/tests/test_unknown.py
+++ b/tests/test_unknown.py
@@ -1,9 +1,12 @@
 # -*- Mode: Python -*-
 
+from __future__ import absolute_import
+
 import unittest
 
 from gi.repository import GObject
-import testhelper
+
+from . import testhelper
 
 
 TestInterface = GObject.GType.from_name('TestInterface')
diff --git a/tests/testmodule.py b/tests/testmodule.py
index c083f626..f79eed8d 100644
--- a/tests/testmodule.py
+++ b/tests/testmodule.py
@@ -1,3 +1,5 @@
+from __future__ import absolute_import
+
 from gi.repository import GObject
 
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]