src/eric7/Testing/Interfaces/PytestRunner.py

branch
eric7
changeset 9311
8e588f403fd9
parent 9221
bf71ee032bb4
child 9371
1da8bc75946f
child 9413
80c06d472826
--- a/src/eric7/Testing/Interfaces/PytestRunner.py	Sun Sep 04 16:11:32 2022 +0200
+++ b/src/eric7/Testing/Interfaces/PytestRunner.py	Mon Sep 05 18:08:43 2022 +0200
@@ -7,6 +7,7 @@
 Module implementing the test runner script for the 'pytest' framework.
 """
 
+import contextlib
 import json
 import os
 import sys
@@ -14,6 +15,9 @@
 
 sys.path.insert(2, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
 
+with contextlib.suppress(ImportError):
+    import pytest
+
 
 class GetPluginVersionsPlugin:
     """
@@ -27,7 +31,7 @@
         """
         super().__init__()
 
-        self.versions = []
+        self.__versions = []
 
     def pytest_cmdline_main(self, config):
         """
@@ -39,7 +43,7 @@
         pluginInfo = config.pluginmanager.list_plugin_distinfo()
         if pluginInfo:
             for _plugin, dist in pluginInfo:
-                self.versions.append(
+                self.__versions.append(
                     {"name": dist.project_name, "version": dist.version}
                 )
 
@@ -50,7 +54,49 @@
         @return list of collected plugin versions
         @rtype list of dict
         """
-        return self.versions
+        return self.__versions
+
+
+class GetMarkersPlugin:
+    """
+    Class implementing a pytest plugin to extract the list of all defined markers.
+    """
+
+    def __init__(self):
+        """
+        Constructor
+        """
+        super().__init__()
+
+        self.__markers = {}
+
+    @pytest.hookimpl(tryfirst=True)
+    def pytest_cmdline_main(self, config):
+        """
+        Public method called for performing the main command line action.
+
+        @param config pytest config object
+        @type Config
+        """
+        config._do_configure()
+        for line in config.getini("markers"):
+            parts = line.split(":", 1)
+            name = parts[0]
+            rest = parts[1] if len(parts) == 2 else ""
+            self.__markers[name] = rest
+        config._ensure_unconfigure()
+
+        print(json.dumps(self.__markers))
+        sys.exit(0)
+
+    def getMarkers(self):
+        """
+        Public method to get the assembled list of markers.
+
+        @return list of collected markers (marker name as key and description as value)
+        @rtype dict
+        """
+        return self.__markers
 
 
 class EricPlugin:
@@ -275,6 +321,23 @@
     sys.exit(0)
 
 
+def getMarkers():
+    """
+    Function to determine the defined markers and their descriptions.
+    """
+    try:
+        import pytest
+
+        # --capture=sys needed on Windows to avoid
+        # ValueError: saved filedescriptor not valid anymore
+        plugin = GetMarkersPlugin()
+        pytest.main(["--markers", "--capture=sys"], plugins=[plugin])
+        # dumping the markers is done in the plugin
+    except ImportError:
+        print(json.dumps({}))
+        sys.exit(0)
+
+
 if __name__ == "__main__":
     command = sys.argv[1]
     if command == "installed":
@@ -288,6 +351,9 @@
     elif command == "versions":
         getVersions()
 
+    elif command == "markers":
+        getMarkers()
+
     elif command == "runtest":
         import pytest
         from EricNetwork.EricJsonStreamWriter import EricJsonWriter

eric ide

mercurial