diff mbox series

[kms-test,7/7] kmstest: Implement test runner when executed directly

Message ID 20220731152024.24090-8-laurent.pinchart@ideasonboard.com (mailing list archive)
State New
Delegated to: Kieran Bingham
Headers show
Series Miscellaneous fixes and improvements | expand

Commit Message

Laurent Pinchart July 31, 2022, 3:20 p.m. UTC
Run all tests from the current directory when the kmstest.py script it
executed directly, as opposed to being imported by individual tests.
This simplifies running all tests.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 tests/kmstest.py | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
diff mbox series

Patch

diff --git a/tests/kmstest.py b/tests/kmstest.py
index 960c27f301ce..e84d8fe859f4 100755
--- a/tests/kmstest.py
+++ b/tests/kmstest.py
@@ -334,7 +334,17 @@  class KMSTest(object):
         if use_default_key_handler:
             self.loop.register(sys.stdin, selectors.EVENT_READ, self.__read_key)
 
+    def __enter__(self):
+        return self
+
+    def __exit__(self, *err):
+        self.card = None
+        self.loop.close()
+        self.logger.close()
+
     def __del__(self):
+        self.card = None
+        self.loop.close()
         self.logger.close()
 
     def atomic_crtc_disable(self, crtc, sync=True):
@@ -520,3 +530,33 @@  class KMSTest(object):
         sys.stdout.write(f'\rTesting {self.test_name}: SUCCESS\n')
         sys.stdout.flush()
 
+
+if __name__ == '__main__':
+    import importlib
+    import inspect
+    import os
+
+    files = []
+    for path in os.scandir():
+        if path.is_file() and path.name.startswith('kms-test-') and path.name.endswith('.py'):
+            files.append(path.name)
+
+    files.sort()
+    for file in files:
+        print(f'- {file}')
+        module = importlib.import_module(file[:-3])
+        tests = []
+        for name in dir(module):
+            obj = getattr(module, name)
+            if not isinstance(obj, type):
+                continue
+
+            if 'KMSTest' in [cls.__name__ for cls in inspect.getmro(obj)]:
+                tests.append(obj)
+
+        for test in tests:
+            # Use a context manager to ensure proper cleanup after each test,
+            # otherwise state from one test may leak to the other based on when
+            # objects end up being deleted.
+            with test() as test:
+                test.execute()