[gnome-bluetooth/wip/hadess/split-tests] tests: Split the integration test into individual tests




commit 08e03911da4341cb29a747fc92b0d6ceb6454a64
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Jan 20 13:17:21 2022 +0100

    tests: Split the integration test into individual tests
    
    unittest_inspector.py lists the tests in the integration-test.py script,
    which are then added as individual tests.

 tests/integration-test.py   | 32 ++++++++++++++++++++-----------
 tests/meson.build           | 21 +++++++++++++++------
 tests/unittest_inspector.py | 46 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 82 insertions(+), 17 deletions(-)
---
diff --git a/tests/integration-test.py b/tests/integration-test.py
index a79066ef..09ebb0cc 100755
--- a/tests/integration-test.py
+++ b/tests/integration-test.py
@@ -35,29 +35,37 @@ except ImportError as e:
     sys.stderr.write('Skipping tests, PyGobject not available for Python 3, or missing GI typelibs: %s\n' % 
str(e))
     sys.exit(77)
 
+gi.require_version('GIRepository', '2.0')
+from gi.repository import GIRepository
+builddir = os.getenv('top_builddir', '.')
+GIRepository.Repository.prepend_library_path(builddir + '/lib/')
+GIRepository.Repository.prepend_search_path(builddir + '/lib/')
+
+GNOME_BLUETOOTH_PRIV_UNAVAILABLE = False
+DBUSMOCK_UNAVAILABLE = False
 try:
-    gi.require_version('GIRepository', '2.0')
-    from gi.repository import GIRepository
-    builddir = os.getenv('top_builddir', '.')
-    GIRepository.Repository.prepend_library_path(builddir + '/lib/')
-    GIRepository.Repository.prepend_search_path(builddir + '/lib/')
-
     gi.require_version('GnomeBluetoothPriv', '3.0')
     from gi.repository import GnomeBluetoothPriv
-except ImportError as e:
-    sys.stderr.write('Could not find GnomeBluetoothPriv gobject-introspection data in the build dir: %s\n' % 
str(e))
-    sys.exit(1)
+except (ImportError, ValueError) as e:
+    GNOME_BLUETOOTH_PRIV_UNAVAILABLE = True
 
 try:
     import dbusmock
 except ImportError:
-    sys.stderr.write('Skipping tests, python-dbusmock not available 
(http://pypi.python.org/pypi/python-dbusmock).\n')
-    sys.exit(77)
+    DBUSMOCK_UNAVAILABLE = True
 
 # Out-of-process tests
 class OopTests(dbusmock.DBusTestCase):
     @classmethod
     def setUp(self):
+        if GNOME_BLUETOOTH_PRIV_UNAVAILABLE:
+            sys.stderr.write('Could not find GnomeBluetoothPriv gobject-introspection data in the build dir: 
%s\n' % str(e))
+            sys.exit(1)
+
+        if DBUSMOCK_UNAVAILABLE:
+            sys.stderr.write('Skipping tests, python-dbusmock not available 
(http://pypi.python.org/pypi/python-dbusmock).\n')
+            sys.exit(77)
+
         self.client = GnomeBluetoothPriv.Client.new()
         # used in test_pairing
         self.paired = False
@@ -449,6 +457,8 @@ class Tests(dbusmock.DBusTestCase):
         print(f"Running out-of-process test {test_name}")
         # And run the test with the same name in the OopTests class in a separate process
         out = subprocess.run(self.exec_path + ['OopTests.' + test_name], capture_output=True)
+        if out.returncode == 77:
+            sys.exit(77)
         self.assertEqual(out.returncode, 0, "Running test " + test_name + " failed:" + 
out.stderr.decode('UTF-8') + '\n\n\nSTDOUT:\n' + out.stdout.decode('UTF-8'))
         if os.getenv('VALGRIND') != None:
             print(out.stderr.decode('UTF-8'))
diff --git a/tests/meson.build b/tests/meson.build
index 419cf59e..6f275f34 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -7,12 +7,21 @@ if enable_gir
 
   test_deps = [ gnomebt_priv_gir, ]
 
-  test('gnome-bluetooth-integration-test',
-    integration_test,
-    args: [ 'Tests' ],
-    env: envs,
-    depends: test_deps
-  )
+  python3 = find_program('python3')
+  unittest_inspector = find_program('unittest_inspector.py')
+  r = run_command(unittest_inspector, files('integration-test.py'))
+  unit_tests = r.stdout().strip().split('\n')
+
+  foreach ut: unit_tests
+    ut_args = files('integration-test.py')
+    ut_args += ut
+    test(ut,
+      python3,
+      args: ut_args,
+      env: envs,
+      depends: test_deps
+    )
+  endforeach
 endif
 
 test_bluetooth_device = executable('test-bluetooth-device',
diff --git a/tests/unittest_inspector.py b/tests/unittest_inspector.py
new file mode 100755
index 00000000..fe830468
--- /dev/null
+++ b/tests/unittest_inspector.py
@@ -0,0 +1,46 @@
+#! /usr/bin/env python3
+# Copyright © 2020, Canonical Ltd
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# 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/>.
+# Authors:
+#       Marco Trevisan <marco trevisan canonical com>
+
+import argparse
+import importlib.util
+import inspect
+import os
+import unittest
+
+def list_tests(module):
+    tests = []
+    for name, obj in inspect.getmembers(module):
+        if inspect.isclass(obj) and issubclass(obj, unittest.TestCase) and not name.startswith('OopTests'):
+            cases = unittest.defaultTestLoader.getTestCaseNames(obj)
+            tests += [ (obj, '{}.{}'.format(name, t)) for t in cases ]
+    return tests
+
+
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser()
+    parser.add_argument('unittest_source', type=argparse.FileType('r'))
+
+    args = parser.parse_args()
+    source_path = args.unittest_source.name
+    spec = importlib.util.spec_from_file_location(
+        os.path.basename(source_path), source_path)
+    module = importlib.util.module_from_spec(spec)
+    spec.loader.exec_module(module)
+
+    for machine, human in list_tests(module):
+        print(human)


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