Coverage for /Applications/PyCharm.app/Contents/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py: 4%

53 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-11 18:09 +0200

1# coding=utf-8 

2 

3# Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. 

4 

5import pytest 

6import sys 

7from _pytest.config import get_plugin_manager 

8import warnings 

9 

10if sys.version_info[:2] >= (3, 10): 

11 from importlib.metadata import entry_points as iter_entry_points 

12else: 

13 with warnings.catch_warnings(): 

14 warnings.simplefilter("ignore", category=DeprecationWarning) 

15 from pkg_resources import iter_entry_points 

16 

17from _jb_runner_tools import jb_patch_separator, jb_doc_args, JB_DISABLE_BUFFERING, \ 

18 start_protocol, parse_arguments, set_parallel_mode, jb_finish_tests, \ 

19 jb_patch_targets 

20from teamcity import pytest_plugin 

21import os 

22 

23_DOCTEST_MODULES_ARG = "--doctest-modules" 

24 

25def _add_module_to_target(module_name, python_parts): 

26 # Doctest: Find the fully qualified name of the target module by checking each 

27 # directory level if they have an __init__.py file 

28 fully_qualified_name = [] 

29 path = os.path.abspath(module_name.replace("/", os.sep)) 

30 while True: 

31 fully_qualified_name.insert(0, os.path.basename(path)) 

32 path = os.path.dirname(path) 

33 init_py_path = os.path.join(path, "__init__.py") 

34 if not os.path.exists(init_py_path): 

35 break 

36 

37 return ".".join(fully_qualified_name) + "." + python_parts 

38 

39 

40if __name__ == '__main__': 

41 path, targets, additional_args = parse_arguments() 

42 sys.argv += additional_args 

43 

44 # Path pytest targets: 

45 if _DOCTEST_MODULES_ARG in additional_args: 

46 # Doctest: path_to_file.py::module_name.class_name.fun_name 

47 joined_targets = jb_patch_targets(targets, '/', '::', '.', '.py::', _add_module_to_target) 

48 else: 

49 # Pytest: path_to_file.py::module_name::class_name::fun_name 

50 joined_targets = jb_patch_separator(targets, fs_glue="/", python_glue="::", fs_to_python_glue=".py::") 

51 

52 # When file is launched in pytest it should be file.py: you can't provide it as bare module 

53 joined_targets = [t + ".py" if ":" not in t else t for t in joined_targets] 

54 sys.argv += [path] if path else joined_targets 

55 

56 # plugin is discovered automatically in 3, but not in 2 

57 # to prevent "plugin already registered" problem we check it first 

58 plugins_to_load = [] 

59 if not get_plugin_manager().hasplugin("pytest-teamcity"): 

60 if "pytest-teamcity" not in map(lambda e: e.name, iter_entry_points(group='pytest11')): 

61 plugins_to_load.append(pytest_plugin) 

62 

63 args = sys.argv[1:] 

64 if "--jb-show-summary" in args: 

65 args.remove("--jb-show-summary") 

66 elif int(pytest.__version__.split('.')[0]) >= 6: 

67 args += ["--no-header", "--no-summary", "-q"] 

68 

69 if JB_DISABLE_BUFFERING and "-s" not in args: 

70 args += ["-s"] 

71 

72 

73 jb_doc_args("pytest", args) 

74 

75 

76 class Plugin: 

77 @staticmethod 

78 def pytest_configure(config): 

79 if getattr(config.option, "numprocesses", None): 79 ↛ 80line 79 didn't jump to line 80 because the condition on line 79 was never true

80 set_parallel_mode() 

81 start_protocol() 

82 

83 os.environ["_JB_PPRINT_PRIMITIVES"] = "1" 

84 try: 

85 sys.exit(pytest.main(args, plugins_to_load + [Plugin])) 

86 finally: 

87 jb_finish_tests()