Skip to content

Commit 8397d3b

Browse files
committed
rp_launch_uuid_print and rp_launch_uuid_print_output configuration parameters
1 parent 3b5d023 commit 8397d3b

File tree

6 files changed

+163
-47
lines changed

6 files changed

+163
-47
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22

33
## [Unreleased]
4+
### Added
5+
- `rp_launch_uuid_print` and `rp_launch_uuid_print_output` configuration parameters, by @HardNorth
6+
### Removed
7+
- Python 2.7, 3.6 support, by @HardNorth
48

59
## [5.1.9]
610
### Added

README.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,18 @@ The following parameters are optional:
8484
- :code:`rp_launch = AnyLaunchName` - launch name (could be overridden by pytest --rp-launch option, default value is 'Pytest Launch').
8585
- :code:`rp_launch_id = xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx` - id of the existing launch (the session will not handle the lifecycle of the given launch).
8686
- :code:`rp_launch_attributes = 'PyTest' 'Smoke' 'Env:Python3'` - list of attributes for launch.
87+
- :code:`rp_launch_description = 'Smoke test'` - launch description (could be overridden by pytest --rp-launch-description option, default value is '').
88+
- :code:`rp_launch_timeout = 86400` - Maximum time to wait for child processes finish, default value: 86400 seconds (1 day).
89+
- :code:`rp_launch_uuid_print = True` - Enables printing Launch UUID on test run start. Default `False`.
90+
- :code:`rp_launch_uuid_print_output = stderr` - Launch UUID print output. Default `stdout`. Possible values: [stderr, stdout].
8791
- :code:`rp_parent_item_id = xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx` - id of the existing test item for session to use as parent item for the tests (the session will not handle the lifecycle of the given test item).
8892
- :code:`rp_tests_attributes = 'PyTest' 'Smoke'` - list of attributes that will be added for each item in the launch.
89-
- :code:`rp_launch_description = 'Smoke test'` - launch description (could be overridden by pytest --rp-launch-description option, default value is '').
9093
- :code:`rp_log_batch_size = 20` - size of batch log request.
9194
- :code:`rp_log_batch_payload_size = 65000000` - maximum payload size in bytes of async batch log requests.
9295
- :code:`rp_log_level = INFO` - The log level that will be reported.
9396
- :code:`rp_log_format = [%(levelname)7s] (%(name)s) %(message)s (%(filename)s:%(lineno)s)` - Format string to be used for logs sent to the service.
9497
- :code:`rp_ignore_attributes = 'xfail' 'usefixture'` - Ignore specified pytest markers.
95-
- :code:`rp_is_skipped_an_issue = False` - Treat skipped tests as required investigation. Default is True.
98+
- :code:`rp_is_skipped_an_issue = False` - Treat skipped tests as required investigation. Default `True`.
9699
- :code:`rp_hierarchy_dirs_level = 0` - Directory starting hierarchy level (from pytest.ini level) (default `0`).
97100
- :code:`rp_hierarchy_dirs = True` - Enables hierarchy for tests directories, default `False`. Doesn't support 'xdist' plugin.
98101
- :code:`rp_hierarchy_dir_path_separator` - Path separator to display directories in test hierarchy. In case of empty value current system path separator will be used (os.path.sep).
@@ -102,7 +105,6 @@ The following parameters are optional:
102105
- :code:`rp_verify_ssl = True` - Verify SSL when connecting to the server.
103106
- :code:`rp_mode = DEFAULT` - DEBUG or DEFAULT launch mode. DEBUG launches are displayed in a separate tab and not visible to anyone except owner.
104107
- :code:`rp_thread_logging` - EXPERIMENTAL - Enables support for reporting logs from threads by patching the builtin Thread class. Use with caution.
105-
- :code:`rp_launch_timeout = 86400` - Maximum time to wait for child processes finish, default value: 86400 seconds (1 day).
106108
- :code:`rp_api_retries = 0` - Amount of retries for performing REST calls to RP server.
107109

108110

pytest_reportportal/config.py

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
"""This module contains class that stores RP agent configuration data."""
2+
import sys
23
import warnings
34

45
from distutils.util import strtobool
56
from os import getenv
7+
from typing import Optional, Union, Any, TextIO, Dict
68

9+
from _pytest.config import Config
710
from reportportal_client.logs.log_manager import MAX_LOG_BATCH_PAYLOAD_SIZE
811

912
try:
@@ -14,10 +17,49 @@
1417
get_actual_log_level
1518

1619

20+
OUTPUT_TYPES: Dict[str, TextIO] = {
21+
'stdout': sys.stdout,
22+
'stderr': sys.stderr
23+
}
24+
25+
1726
class AgentConfig(object):
1827
"""Storage for the RP agent initialization attributes."""
1928

20-
def __init__(self, pytest_config):
29+
rp_rerun: Optional[bool]
30+
pconfig: Config
31+
rp_endpoint: str
32+
rp_hierarchy_code: bool
33+
rp_dir_level: int
34+
rp_hierarchy_dirs: bool
35+
rp_dir_path_separator: str
36+
rp_ignore_attributes: set
37+
rp_is_skipped_an_issue: bool
38+
rp_issue_id_marks: bool
39+
rp_issue_system_url: str
40+
rp_bts_project: str
41+
rp_bts_url: str
42+
rp_launch: str
43+
rp_launch_id: Optional[str]
44+
rp_launch_attributes: Optional[list]
45+
rp_launch_description: str
46+
rp_log_batch_size: int
47+
rp_log_batch_payload_size: int
48+
rp_log_level: Optional[int]
49+
rp_log_format: Optional[str]
50+
rp_mode: str
51+
rp_parent_item_id: Optional[str]
52+
rp_project: str
53+
rp_rerun_of: Optional[str]
54+
rp_api_retries: int
55+
rp_skip_connection_test: bool
56+
rp_api_key: str
57+
rp_verify_ssl: Union[bool, str]
58+
rp_launch_timeout: int
59+
rp_launch_uuid_print: bool
60+
rp_launch_uuid_print_output: TextIO
61+
62+
def __init__(self, pytest_config: Config) -> None:
2163
"""Initialize required attributes."""
2264
self.rp_rerun = (pytest_config.option.rp_rerun or
2365
pytest_config.getini('rp_rerun'))
@@ -32,8 +74,7 @@ def __init__(self, pytest_config):
3274
self.find_option(pytest_config, 'rp_hierarchy_dir_path_separator')
3375
ignore_attributes = self.find_option(pytest_config,
3476
'rp_ignore_attributes')
35-
self.rp_ignore_attributes = set(ignore_attributes) \
36-
if ignore_attributes else set()
77+
self.rp_ignore_attributes = ignore_attributes and set(ignore_attributes)
3778
self.rp_is_skipped_an_issue = self.find_option(
3879
pytest_config,
3980
'rp_is_skipped_an_issue'
@@ -127,8 +168,15 @@ def __init__(self, pytest_config):
127168
self.rp_launch_timeout = int(
128169
self.find_option(pytest_config, 'rp_launch_timeout'))
129170

171+
self.rp_launch_uuid_print = bool(strtobool(self.find_option(
172+
pytest_config, 'rp_launch_uuid_print'
173+
)))
174+
self.rp_launch_uuid_print_output = OUTPUT_TYPES.get((self.find_option(
175+
pytest_config, 'rp_launch_uuid_print_output'
176+
) or 'stdout').lower(), OUTPUT_TYPES['stdout'])
177+
130178
# noinspection PyMethodMayBeStatic
131-
def find_option(self, pytest_config, option_name, default=None):
179+
def find_option(self, pytest_config: Config, option_name: str, default: Any = None):
132180
"""
133181
Find a single configuration setting from multiple places.
134182
@@ -148,4 +196,4 @@ def find_option(self, pytest_config, option_name, default=None):
148196
)
149197
if isinstance(value, bool):
150198
return value
151-
return value if value else default
199+
return value or default

pytest_reportportal/config.pyi

Lines changed: 0 additions & 39 deletions
This file was deleted.

pytest_reportportal/plugin.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ def pytest_sessionstart(session):
104104

105105
if is_control(config) and not config._reporter_config.rp_launch_id:
106106
config.py_test_service.start_launch()
107+
if config._reporter_config.rp_launch_uuid_print:
108+
launch_id = config.py_test_service.rp.launch_id
109+
print(f'Report Portal Launch UUID: {launch_id}', file=config._reporter_config.rp_launch_uuid_print_output)
107110
if config.pluginmanager.hasplugin('xdist') \
108111
or config.pluginmanager.hasplugin('pytest-parallel'):
109112
if not wait_launch(session.config.py_test_service.rp):
@@ -400,6 +403,16 @@ def add_shared_option(name, help_str, default=None, action='store'):
400403
default=False,
401404
action='store_true'
402405
)
406+
add_shared_option(
407+
name='rp_launch_uuid_print',
408+
help_str='Enables printing Launch UUID on test run start. Possible values: [True, False]',
409+
default='False'
410+
)
411+
add_shared_option(
412+
name='rp_launch_uuid_print_output',
413+
help_str='Launch UUID print output. Default `stdout`. Possible values: [stderr, stdout]',
414+
default='stdout'
415+
)
403416

404417
parser.addini(
405418
'rp_launch_attributes',

tests/integration/test_config_handling.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414

1515
import sys
1616
import warnings
17+
from io import StringIO
1718

1819
from delayed_assert import expect, assert_expectations
1920
from unittest import mock
2021

2122
from examples.test_rp_logging import LOG_MESSAGE
23+
from pytest_reportportal.config import OUTPUT_TYPES
2224
from tests import REPORT_PORTAL_SERVICE
2325
from tests.helpers import utils
2426

@@ -283,3 +285,89 @@ def test_retries(mock_client_init):
283285
expect(constructor_args['retries'] == retries)
284286
expect(len(filter_agent_calls(w)) == 1)
285287
assert_expectations()
288+
289+
290+
@mock.patch(REPORT_PORTAL_SERVICE)
291+
def test_launch_uuid_print(mock_client_init):
292+
print_uuid = True
293+
variables = utils.DEFAULT_VARIABLES.copy()
294+
variables.update({'rp_launch_uuid_print': str(print_uuid)}.items())
295+
296+
str_io = StringIO()
297+
stdout = sys.stdout
298+
try:
299+
OUTPUT_TYPES['stdout'] = str_io
300+
result = utils.run_pytest_tests(['examples/test_rp_logging.py'],
301+
variables=variables)
302+
finally:
303+
OUTPUT_TYPES['stdout'] = stdout
304+
305+
assert int(result) == 0, 'Exit code should be 0 (no errors)'
306+
expect(mock_client_init.call_count == 1)
307+
308+
expect('Report Portal Launch UUID:' in str_io.getvalue())
309+
assert_expectations()
310+
311+
312+
@mock.patch(REPORT_PORTAL_SERVICE)
313+
def test_launch_uuid_print_stderr(mock_client_init):
314+
print_uuid = True
315+
variables = utils.DEFAULT_VARIABLES.copy()
316+
variables.update({'rp_launch_uuid_print': str(print_uuid), 'rp_launch_uuid_print_output': 'stderr'}.items())
317+
318+
str_io = StringIO()
319+
stderr = sys.stderr
320+
try:
321+
OUTPUT_TYPES['stderr'] = str_io
322+
result = utils.run_pytest_tests(['examples/test_rp_logging.py'],
323+
variables=variables)
324+
finally:
325+
OUTPUT_TYPES['stderr'] = stderr
326+
327+
assert int(result) == 0, 'Exit code should be 0 (no errors)'
328+
expect(mock_client_init.call_count == 1)
329+
330+
expect('Report Portal Launch UUID:' in str_io.getvalue())
331+
assert_expectations()
332+
333+
334+
@mock.patch(REPORT_PORTAL_SERVICE)
335+
def test_launch_uuid_print_invalid_output(mock_client_init):
336+
print_uuid = True
337+
variables = utils.DEFAULT_VARIABLES.copy()
338+
variables.update({'rp_launch_uuid_print': str(print_uuid), 'rp_launch_uuid_print_output': 'something'}.items())
339+
340+
str_io = StringIO()
341+
stdout = sys.stdout
342+
try:
343+
OUTPUT_TYPES['stdout'] = str_io
344+
result = utils.run_pytest_tests(['examples/test_rp_logging.py'],
345+
variables=variables)
346+
finally:
347+
OUTPUT_TYPES['stdout'] = stdout
348+
349+
assert int(result) == 0, 'Exit code should be 0 (no errors)'
350+
expect(mock_client_init.call_count == 1)
351+
352+
expect('Report Portal Launch UUID:' in str_io.getvalue())
353+
assert_expectations()
354+
355+
356+
@mock.patch(REPORT_PORTAL_SERVICE)
357+
def test_no_launch_uuid_print(mock_client_init):
358+
variables = utils.DEFAULT_VARIABLES.copy()
359+
360+
str_io = StringIO()
361+
stdout = sys.stdout
362+
try:
363+
OUTPUT_TYPES['stdout'] = str_io
364+
result = utils.run_pytest_tests(['examples/test_rp_logging.py'],
365+
variables=variables)
366+
finally:
367+
OUTPUT_TYPES['stdout'] = stdout
368+
369+
assert int(result) == 0, 'Exit code should be 0 (no errors)'
370+
expect(mock_client_init.call_count == 1)
371+
372+
expect('Report Portal Launch UUID:' not in str_io.getvalue())
373+
assert_expectations()

0 commit comments

Comments
 (0)