Coverage for src/pytest_xray/plugin.py: 96.7%

89 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-11-06 12:46 +0000

1# Modified in the parameter-details prototype; see NOTICE. 

2import os 

3from pathlib import Path 

4from typing import Union 

5 

6import pytest 

7from requests.auth import AuthBase 

8 

9from pytest_xray import hooks 

10from pytest_xray.case_report import CaseReportPlugin 

11from pytest_xray.comment_limits import DEFAULT_COMMENT_LIMIT 

12from pytest_xray.constant import ( 

13 JIRA_API_KEY, 

14 JIRA_CLIENT_SECRET_AUTH, 

15 JIRA_CLOUD, 

16 JIRA_XRAY_FLAG, 

17 TEST_EXECUTION_ENDPOINT, 

18 TEST_EXECUTION_ENDPOINT_CLOUD, 

19 XRAY_ADD_CAPTURES, 

20 XRAY_ALLOW_DUPLICATE_IDS, 

21 XRAY_EXECUTION_ID, 

22 XRAY_PLUGIN, 

23 XRAY_TEST_PLAN_ID, 

24 XRAYPATH, 

25) 

26from pytest_xray.exceptions import XrayError 

27from pytest_xray.file_publisher import FilePublisher 

28from pytest_xray.helper import get_api_key_auth, get_basic_auth, get_bearer_auth 

29from pytest_xray.xray_plugin import Publisher, XrayPlugin 

30from pytest_xray.xray_publisher import ApiKeyAuth, ClientSecretAuth, XrayPublisher 

31 

32 

33def pytest_addoption(parser: pytest.Parser) -> None: 

34 xray = parser.getgroup('Jira Xray report') 

35 xray.addoption( 

36 '--xray-comment-limit', 

37 type=int, 

38 default=DEFAULT_COMMENT_LIMIT, 

39 help='Maximum UTF-8 bytes per exported comment (default: 32000; minimum: 256)', 

40 ) 

41 xray.addoption( 

42 '--xray-full-report', 

43 metavar='path', 

44 default=None, 

45 help='Retain complete legacy Xray JSON locally before bounding comments', 

46 ) 

47 xray.addoption( 

48 '--xray-case-report', 

49 metavar='path', 

50 default=None, 

51 help='Write a transport-independent offline JSON case report', 

52 ) 

53 xray.addoption( 

54 '--xray-case-values', 

55 action='store_true', 

56 default=False, 

57 help='Opt in to potentially sensitive scalar values in the offline case report', 

58 ) 

59 xray.addoption( 

60 '--xray-case-redact-identities', 

61 action='store_true', 

62 default=False, 

63 help='Replace offline node and parameter IDs with per-run opaque aliases', 

64 ) 

65 xray.addoption( 

66 '--xray-parameter-details', 

67 action='store_true', 

68 default=False, 

69 help='Include parameter case identity, values and phase outcomes in test run comments', 

70 ) 

71 xray.addoption(JIRA_XRAY_FLAG, action='store_true', default=False, help='Upload test results to JIRA XRAY') 

72 xray.addoption(JIRA_CLOUD, action='store_true', default=False, help='Use with JIRA XRAY cloud server') 

73 xray.addoption( 

74 JIRA_API_KEY, 

75 action='store_true', 

76 default=False, 

77 help='Use Jira API Key authentication', 

78 ) 

79 xray.addoption( 

80 JIRA_CLIENT_SECRET_AUTH, 

81 action='store_true', 

82 default=False, 

83 help='Use client secret authentication', 

84 ) 

85 xray.addoption( 

86 XRAY_EXECUTION_ID, action='store', metavar='ExecutionId', default=None, help='XRAY Test Execution ID' 

87 ) 

88 xray.addoption(XRAY_TEST_PLAN_ID, action='store', metavar='TestplanId', default=None, help='XRAY Test Plan ID') 

89 xray.addoption( 

90 XRAYPATH, 

91 action='store', 

92 metavar='path', 

93 default=None, 

94 help='Do not upload to a server but create JSON report file at given path', 

95 ) 

96 xray.addoption( 

97 XRAY_ALLOW_DUPLICATE_IDS, 

98 action='store_true', 

99 default=False, 

100 help='Allow test ids to be present on multiple pytest tests', 

101 ) 

102 xray.addoption( 

103 XRAY_ADD_CAPTURES, 

104 action='store_true', 

105 default=False, 

106 help='Add captures from log, stdout or/and stderr, to the report comment field', 

107 ) 

108 

109 

110def pytest_addhooks(pluginmanager): 

111 pluginmanager.add_hookspecs(hooks) 

112 

113 

114def pytest_configure(config: pytest.Config) -> None: 

115 config.addinivalue_line('markers', 'xray(JIRA_ID): mark test with JIRA XRAY test case ID') 

116 if config.option.collectonly: 

117 return 

118 

119 full_path = config.getoption('--xray-full-report') 

120 if config.getoption('--xray-comment-limit') < 256: 

121 raise pytest.UsageError('--xray-comment-limit must be at least 256 bytes') 

122 if full_path and not config.getoption(JIRA_XRAY_FLAG): 

123 raise pytest.UsageError('--xray-full-report requires --jira-xray') 

124 paths = [('--xray-case-report', False), (XRAYPATH, True), ('--xray-full-report', True)] 

125 normalized = [] 

126 for option, expand in paths: 

127 path = config.getoption(option) 

128 if path == '': 

129 raise pytest.UsageError(f'{option} requires a nonempty file path') 

130 if not path: 

131 continue 

132 try: 

133 normalized.append(Path(os.path.expandvars(path) if expand else path).expanduser().resolve()) 

134 except (OSError, RuntimeError) as exc: 

135 raise pytest.UsageError(f'Cannot resolve {option}: {exc}') from None 

136 if len(set(normalized)) != len(normalized): 

137 raise pytest.UsageError('Report paths must be different: --xray-case-report, --xraypath, --xray-full-report') 

138 case_path = config.getoption('--xray-case-report') 

139 redact_ids = config.getoption('--xray-case-redact-identities') 

140 if redact_ids and not case_path: 140 ↛ 141line 140 didn't jump to line 141 because the condition on line 140 was never true

141 raise pytest.UsageError('--xray-case-redact-identities requires --xray-case-report') 

142 if redact_ids and config.getoption('--xray-case-values'): 

143 raise pytest.UsageError('--xray-case-redact-identities cannot be combined with --xray-case-values') 

144 if config.getoption('--xray-case-values') and not case_path: 

145 raise pytest.UsageError('--xray-case-values requires --xray-case-report') 

146 if case_path: 

147 xray_path = config.getoption(XRAYPATH) 

148 if ( 148 ↛ 152line 148 didn't jump to line 152 because the condition on line 148 was never true

149 xray_path 

150 and Path(case_path).expanduser().resolve() == Path(os.path.expandvars(xray_path)).expanduser().resolve() 

151 ): 

152 raise pytest.UsageError('--xray-case-report and --xraypath must use different files') 

153 config.pluginmanager.register( 

154 CaseReportPlugin(config, case_path, config.getoption('--xray-case-values'), redact_ids), 

155 name='xray-offline-cases', 

156 ) 

157 

158 if not config.getoption(JIRA_XRAY_FLAG): 

159 return 

160 

161 xray_path = config.getoption(XRAYPATH) 

162 

163 if xray_path: 

164 publisher: Publisher = FilePublisher(xray_path) 

165 else: 

166 endpoint = TEST_EXECUTION_ENDPOINT_CLOUD if config.getoption(JIRA_CLOUD) else TEST_EXECUTION_ENDPOINT 

167 

168 try: 

169 if config.getoption(JIRA_CLIENT_SECRET_AUTH): 

170 options = get_bearer_auth() 

171 auth: Union[AuthBase, tuple[str, str]] = ClientSecretAuth( 

172 options['BASE_URL'], options['CLIENT_ID'], options['CLIENT_SECRET'], options['VERIFY'] 

173 ) 

174 elif config.getoption(JIRA_API_KEY): 

175 options = get_api_key_auth() 

176 auth = ApiKeyAuth(options['API_KEY']) 

177 else: 

178 options = get_basic_auth() 

179 auth = (options['USER'], options['PASSWORD']) 

180 except XrayError as exc: 

181 raise pytest.UsageError(exc.message) from None 

182 

183 publisher = XrayPublisher(base_url=options['BASE_URL'], endpoint=endpoint, auth=auth, verify=options['VERIFY']) 

184 

185 plugin = XrayPlugin(config, publisher) 

186 config.pluginmanager.register(plugin=plugin, name=XRAY_PLUGIN)