Skip to content

Commit 40e314c

Browse files
rthardinRyan Hardin
andauthored
expose rp_uuid and rp_endpoint to command line
* Added unit tests for .ini and command line arguments These tests verify the names of the arguments added to each. * Refactored `pytest_addoption` and `AgentConfig` This change adds a new helper function `add_shared_option` within `pytest_addoption` to make it easier to keep both sets of arguments consistent with each other. This change also adds a new method, `AgentConfig.find_option`, which is used to find options given from either the command line or from the .ini file. Note: This change does affect the order of the calls to `parser.addini` and `group.addoption`. * Expose `rp_uuid` and `rp_endpoint` as command line arguments Co-authored-by: Ryan Hardin <[email protected]>
1 parent 36e133a commit 40e314c

File tree

5 files changed

+284
-134
lines changed

5 files changed

+284
-134
lines changed

pytest_reportportal/config.py

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,24 @@ def __init__(self, pytest_config):
1818
self._rp_rerun = None
1919
self.pconfig = pytest_config
2020

21-
self.rp_endpoint = self.pconfig.getini('rp_endpoint')
22-
self.rp_ignore_errors = self.pconfig.getini('rp_ignore_errors')
23-
self.rp_ignore_attributes = self.pconfig.getini('rp_ignore_attributes')
24-
self.rp_is_skipped_an_issue = self.pconfig.getini(
25-
'rp_is_skipped_an_issue')
26-
self.rp_launch = self.pconfig.option.rp_launch or self.pconfig.getini(
27-
'rp_launch')
28-
self.rp_launch_id = (self.pconfig.option.rp_launch_id or
29-
self.pconfig.getini('rp_launch_id'))
30-
self.rp_launch_attributes = self.pconfig.getini('rp_launch_attributes')
31-
self.rp_launch_description = (
32-
self.pconfig.option.rp_launch_description or
33-
self.pconfig.getini('rp_launch_description')
21+
self.rp_endpoint = self.find_option('rp_endpoint')
22+
self.rp_ignore_errors = self.find_option('rp_ignore_errors')
23+
self.rp_ignore_attributes = self.find_option('rp_ignore_attributes')
24+
self.rp_is_skipped_an_issue = self.find_option(
25+
'rp_is_skipped_an_issue'
3426
)
35-
self.rp_log_batch_size = int(self.pconfig.getini('rp_log_batch_size'))
27+
self.rp_launch = self.find_option('rp_launch')
28+
self.rp_launch_id = self.find_option('rp_launch_id')
29+
self.rp_launch_attributes = self.find_option('rp_launch_attributes')
30+
self.rp_launch_description = self.find_option('rp_launch_description')
31+
self.rp_log_batch_size = int(self.find_option('rp_log_batch_size'))
3632
self.rp_log_level = get_actual_log_level(self.pconfig, 'rp_log_level')
37-
self.rp_parent_item_id = (self.pconfig.option.rp_parent_item_id or
38-
self.pconfig.getini('rp_parent_item_id'))
39-
self.rp_project = (self.pconfig.option.rp_project or
40-
self.pconfig.getini('rp_project'))
41-
self.rp_rerun_of = (self.pconfig.option.rp_rerun_of or
42-
self.pconfig.getini('rp_rerun_of'))
43-
self.rp_retries = int(self.pconfig.getini('retries'))
44-
self.rp_uuid = getenv('RP_UUID') or self.pconfig.getini('rp_uuid')
45-
self.rp_verify_ssl = self.pconfig.getini('rp_verify_ssl')
33+
self.rp_parent_item_id = self.find_option('rp_parent_item_id')
34+
self.rp_project = self.find_option('rp_project')
35+
self.rp_rerun_of = self.find_option('rp_rerun_of')
36+
self.rp_retries = int(self.find_option('retries'))
37+
self.rp_uuid = getenv('RP_UUID') or self.find_option('rp_uuid')
38+
self.rp_verify_ssl = self.find_option('rp_verify_ssl')
4639

4740
@property
4841
def rp_rerun(self):
@@ -54,3 +47,22 @@ def rp_rerun(self):
5447
self._rp_rerun = (self.pconfig.option.rp_rerun or
5548
self.pconfig.getini('rp_rerun'))
5649
return self._rp_rerun
50+
51+
def find_option(self, option_name, default=None):
52+
"""
53+
Find a single configuration setting from multiple places.
54+
55+
The value is retrieved in the following places in priority order:
56+
57+
1. From `self.pconfig.option.[option_name]`.
58+
2. From `self.pconfig.getini(option_name)`.
59+
60+
:param option_name: name of the option
61+
:param default: value to be returned if not found
62+
:return: option value
63+
"""
64+
value = (
65+
getattr(self.pconfig.option, option_name) or
66+
self.pconfig.getini(option_name)
67+
)
68+
return value if value else default

pytest_reportportal/config.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from _pytest.config import Config as Config
2-
from typing import List, Optional, Text
2+
from typing import List, Optional, Text, Union
33

44
class AgentConfig:
55
_rp_rerun: Optional[bool] = ...
@@ -23,3 +23,5 @@ class AgentConfig:
2323
def __init__(self, pytest_config: Config) -> None: ...
2424
@property
2525
def rp_rerun(self) -> Optional[bool]: ...
26+
27+
def find_option(self, param: str) -> Union[str, bool]: ...

pytest_reportportal/plugin.py

Lines changed: 65 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -214,83 +214,80 @@ def pytest_addoption(parser):
214214
:param parser: Object of the Parser class
215215
"""
216216
group = parser.getgroup('reporting')
217-
group.addoption(
218-
'--rp-launch',
219-
action='store',
220-
dest='rp_launch',
221-
help='Launch name (overrides rp_launch config option)')
222-
group.addoption(
223-
'--rp-launch-id',
224-
action='store',
225-
dest='rp_launch_id',
226-
help='Use already existing launch-id. The plugin won\'t control the '
227-
'Launch status (overrides rp_launch_id config option)')
228-
group.addoption(
229-
'--rp-launch-description',
230-
action='store',
231-
dest='rp_launch_description',
232-
help='Launch description (overrides '
233-
'rp_launch_description config option)')
234-
group.addoption(
235-
'--rp-rerun',
236-
action='store_true',
237-
dest='rp_rerun',
238-
help='Marks the launch as the rerun')
239-
group.addoption(
240-
'--rp-rerun-of',
241-
action='store',
242-
dest='rp_rerun_of',
243-
help='ID of the launch to be marked as a rerun '
244-
'(use only with rp_rerun=True)')
245-
group.addoption(
246-
'--rp-parent-item-id',
247-
action='store',
248-
dest='rp_parent_item_id',
249-
help='Create all test item as child items of the given '
250-
'(already existing) item.')
251-
group.addoption(
252-
'--rp-project',
253-
action='store',
254-
dest='rp_project',
255-
help='Sets rp_project from command line'
256-
)
217+
218+
def add_shared_option(name, help, default=None, action='store'):
219+
"""
220+
Add an option to both the command line and the .ini file.
221+
222+
This function modifies `parser` and `group` from the outer scope.
223+
224+
:param name: name of the option
225+
:param help: help message
226+
:param default: default value
227+
:param action: `group.addoption` action
228+
"""
229+
parser.addini(
230+
name=name,
231+
default=default,
232+
help=help,
233+
)
234+
group.addoption(
235+
'--{0}'.format(name.replace('_', '-')),
236+
action=action,
237+
dest=name,
238+
help='{help} (overrides {name} config option)'.format(
239+
help=help,
240+
name=name,
241+
),
242+
)
243+
257244
group.addoption(
258245
'--reportportal',
259246
action='store_true',
260247
dest='rp_enabled',
261248
default=False,
262249
help='Enable ReportPortal plugin'
263250
)
264-
group.addoption(
265-
'--rp-log-level',
266-
dest='rp_log_level',
267-
default=None,
268-
help='Logging level for automated log records reporting'
251+
add_shared_option(
252+
name='rp_launch',
253+
help='Launch name',
254+
default='Pytest Launch',
269255
)
270-
271-
parser.addini(
272-
'rp_log_level',
273-
default=None,
274-
help='Logging level for automated log records reporting'
256+
add_shared_option(
257+
name='rp_launch_id',
258+
help='Use already existing launch-id. The plugin won\'t control the '
259+
'Launch status',
275260
)
276-
parser.addini(
277-
'rp_uuid',
278-
help='UUID')
279-
parser.addini(
280-
'rp_endpoint',
281-
help='Server endpoint')
282-
parser.addini(
283-
'rp_project',
284-
help='Project name')
285-
parser.addini(
286-
'rp_launch',
287-
default='Pytest Launch',
288-
help='Launch name')
289-
parser.addini(
290-
'rp_launch_id',
291-
default=None,
292-
help='Use already existing launch-id. The plugin won\'t control '
293-
'the Launch status')
261+
add_shared_option(
262+
name='rp_launch_description',
263+
help='Launch description',
264+
default='',
265+
)
266+
add_shared_option(name='rp_project', help='Project name')
267+
add_shared_option(
268+
name='rp_log_level',
269+
help='Logging level for automated log records reporting',
270+
)
271+
add_shared_option(
272+
name='rp_rerun',
273+
help='Marks the launch as a rerun',
274+
default=False,
275+
action='store_true',
276+
)
277+
add_shared_option(
278+
name='rp_rerun_of',
279+
help='ID of the launch to be marked as a rerun (use only with '
280+
'rp_rerun=True)',
281+
default='',
282+
)
283+
add_shared_option(
284+
name='rp_parent_item_id',
285+
help='Create all test item as child items of the given (already '
286+
'existing) item.',
287+
)
288+
add_shared_option(name='rp_uuid', help='UUID')
289+
add_shared_option(name='rp_endpoint', help='Server endpoint')
290+
294291
parser.addini(
295292
'rp_launch_attributes',
296293
type='args',
@@ -299,10 +296,6 @@ def pytest_addoption(parser):
299296
'rp_tests_attributes',
300297
type='args',
301298
help='Attributes for all tests items, e.g. Smoke')
302-
parser.addini(
303-
'rp_launch_description',
304-
default='',
305-
help='Launch description')
306299
parser.addini(
307300
'rp_log_batch_size',
308301
default='20',
@@ -375,21 +368,7 @@ def pytest_addoption(parser):
375368
type='bool',
376369
default=True,
377370
help='Adding tag with issue id to the test')
378-
parser.addini(
379-
'rp_parent_item_id',
380-
default=None,
381-
help="Create all test item as child items of the given "
382-
"(already existing) item.")
383371
parser.addini(
384372
'retries',
385373
default='0',
386374
help='Amount of retries for performing REST calls to RP server')
387-
parser.addini(
388-
'rp_rerun',
389-
default=False,
390-
help='Marks the launch as the rerun')
391-
parser.addini(
392-
'rp_rerun_of',
393-
default='',
394-
help='ID of the launch to be marked as a rerun '
395-
'(use only with rp_rerun=True)')

tests/conftest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,26 @@ def mocked_module(mocked_session):
4242
def mocked_config():
4343
"""Mock Pytest config for testing."""
4444
mocked_config = mock.create_autospec(Config)
45+
46+
mocked_config.getoption_side_effects = {
47+
'--collect-only': False,
48+
'--setup-plan': False,
49+
'rp_log_level': 'debug',
50+
}
51+
52+
def getoption_side_effect(name, default=None):
53+
return mocked_config.getoption_side_effects.get(
54+
name, default if default else mock.Mock()
55+
)
56+
57+
mocked_config.getoption.side_effect = getoption_side_effect
4558
mocked_config._reportportal_configured = True
4659
mocked_config.rootdir = py.path.local('/path/to')
4760
mocked_config.trace = TagTracer().get('root')
4861
mocked_config.pluginmanager = mock.Mock()
62+
mocked_config.option = mock.Mock()
63+
mocked_config.option.rp_log_batch_size = -1
64+
mocked_config.option.retries = -1
4965
return mocked_config
5066

5167

0 commit comments

Comments
 (0)