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

45 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-06-06 12:04 +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: Add module name to path_to_file.py::module_name.class_name.fun_name 

27 return module_name + "." + python_parts 

28 

29 

30if __name__ == '__main__': 

31 path, targets, additional_args = parse_arguments() 

32 sys.argv += additional_args 

33 

34 # Path pytest targets: 

35 if _DOCTEST_MODULES_ARG in additional_args: 

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

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

38 else: 

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

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

41 

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

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

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

45 

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

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

48 plugins_to_load = [] 

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

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

51 plugins_to_load.append(pytest_plugin) 

52 

53 args = sys.argv[1:] 

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

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

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

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

58 

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

60 args += ["-s"] 

61 

62 

63 jb_doc_args("pytest", args) 

64 

65 

66 class Plugin: 

67 @staticmethod 

68 def pytest_configure(config): 

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

70 set_parallel_mode() 

71 start_protocol() 

72 

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

74 try: 

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

76 finally: 

77 jb_finish_tests()