src/eric7/Testing/Interfaces/PytestRunner.py

branch
eric7
changeset 9311
8e588f403fd9
parent 9221
bf71ee032bb4
child 9371
1da8bc75946f
child 9413
80c06d472826
equal deleted inserted replaced
9310:8ab45a4a6d96 9311:8e588f403fd9
5 5
6 """ 6 """
7 Module implementing the test runner script for the 'pytest' framework. 7 Module implementing the test runner script for the 'pytest' framework.
8 """ 8 """
9 9
10 import contextlib
10 import json 11 import json
11 import os 12 import os
12 import sys 13 import sys
13 import time 14 import time
14 15
15 sys.path.insert(2, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))) 16 sys.path.insert(2, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
16 17
18 with contextlib.suppress(ImportError):
19 import pytest
20
17 21
18 class GetPluginVersionsPlugin: 22 class GetPluginVersionsPlugin:
19 """ 23 """
20 Class implementing a pytest plugin to extract the version info of all 24 Class implementing a pytest plugin to extract the version info of all
21 installed plugins. 25 installed plugins.
25 """ 29 """
26 Constructor 30 Constructor
27 """ 31 """
28 super().__init__() 32 super().__init__()
29 33
30 self.versions = [] 34 self.__versions = []
31 35
32 def pytest_cmdline_main(self, config): 36 def pytest_cmdline_main(self, config):
33 """ 37 """
34 Public method called for performing the main command line action. 38 Public method called for performing the main command line action.
35 39
37 @type Config 41 @type Config
38 """ 42 """
39 pluginInfo = config.pluginmanager.list_plugin_distinfo() 43 pluginInfo = config.pluginmanager.list_plugin_distinfo()
40 if pluginInfo: 44 if pluginInfo:
41 for _plugin, dist in pluginInfo: 45 for _plugin, dist in pluginInfo:
42 self.versions.append( 46 self.__versions.append(
43 {"name": dist.project_name, "version": dist.version} 47 {"name": dist.project_name, "version": dist.version}
44 ) 48 )
45 49
46 def getVersions(self): 50 def getVersions(self):
47 """ 51 """
48 Public method to get the assembled list of plugin versions. 52 Public method to get the assembled list of plugin versions.
49 53
50 @return list of collected plugin versions 54 @return list of collected plugin versions
51 @rtype list of dict 55 @rtype list of dict
52 """ 56 """
53 return self.versions 57 return self.__versions
58
59
60 class GetMarkersPlugin:
61 """
62 Class implementing a pytest plugin to extract the list of all defined markers.
63 """
64
65 def __init__(self):
66 """
67 Constructor
68 """
69 super().__init__()
70
71 self.__markers = {}
72
73 @pytest.hookimpl(tryfirst=True)
74 def pytest_cmdline_main(self, config):
75 """
76 Public method called for performing the main command line action.
77
78 @param config pytest config object
79 @type Config
80 """
81 config._do_configure()
82 for line in config.getini("markers"):
83 parts = line.split(":", 1)
84 name = parts[0]
85 rest = parts[1] if len(parts) == 2 else ""
86 self.__markers[name] = rest
87 config._ensure_unconfigure()
88
89 print(json.dumps(self.__markers))
90 sys.exit(0)
91
92 def getMarkers(self):
93 """
94 Public method to get the assembled list of markers.
95
96 @return list of collected markers (marker name as key and description as value)
97 @rtype dict
98 """
99 return self.__markers
54 100
55 101
56 class EricPlugin: 102 class EricPlugin:
57 """ 103 """
58 Class implementing a pytest plugin which reports the data in a format 104 Class implementing a pytest plugin which reports the data in a format
273 319
274 print(json.dumps(versions)) 320 print(json.dumps(versions))
275 sys.exit(0) 321 sys.exit(0)
276 322
277 323
324 def getMarkers():
325 """
326 Function to determine the defined markers and their descriptions.
327 """
328 try:
329 import pytest
330
331 # --capture=sys needed on Windows to avoid
332 # ValueError: saved filedescriptor not valid anymore
333 plugin = GetMarkersPlugin()
334 pytest.main(["--markers", "--capture=sys"], plugins=[plugin])
335 # dumping the markers is done in the plugin
336 except ImportError:
337 print(json.dumps({}))
338 sys.exit(0)
339
340
278 if __name__ == "__main__": 341 if __name__ == "__main__":
279 command = sys.argv[1] 342 command = sys.argv[1]
280 if command == "installed": 343 if command == "installed":
281 try: 344 try:
282 import pytest # __IGNORE_WARNING__ 345 import pytest # __IGNORE_WARNING__
286 sys.exit(1) 349 sys.exit(1)
287 350
288 elif command == "versions": 351 elif command == "versions":
289 getVersions() 352 getVersions()
290 353
354 elif command == "markers":
355 getMarkers()
356
291 elif command == "runtest": 357 elif command == "runtest":
292 import pytest 358 import pytest
293 from EricNetwork.EricJsonStreamWriter import EricJsonWriter 359 from EricNetwork.EricJsonStreamWriter import EricJsonWriter
294 360
295 writer = EricJsonWriter(sys.argv[2], int(sys.argv[3])) 361 writer = EricJsonWriter(sys.argv[2], int(sys.argv[3]))

eric ide

mercurial