Skip to content

Commit f0b6ace

Browse files
authored
Debug mode imlplementation
1 parent ae33c32 commit f0b6ace

File tree

15 files changed

+198
-13
lines changed

15 files changed

+198
-13
lines changed

examples/test_simple.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""Simple example test."""
2+
3+
4+
def test_simple():
5+
"""Simple example test."""
6+
assert True

pytest_reportportal/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def __init__(self, pytest_config):
3030
self.rp_launch_description = self.find_option('rp_launch_description')
3131
self.rp_log_batch_size = int(self.find_option('rp_log_batch_size'))
3232
self.rp_log_level = get_actual_log_level(self.pconfig, 'rp_log_level')
33+
self.rp_mode = self.find_option('rp_mode')
3334
self.rp_parent_item_id = self.find_option('rp_parent_item_id')
3435
self.rp_project = self.find_option('rp_project')
3536
self.rp_rerun_of = self.find_option('rp_rerun_of')

pytest_reportportal/config.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ class AgentConfig:
2020
rp_retries: int = ...
2121
rp_uuid: Text = ...
2222
rp_verify_ssl: bool = ...
23+
rp_mode: str = ...
2324
def __init__(self, pytest_config: Config) -> None: ...
2425
@property
2526
def rp_rerun(self) -> Optional[bool]: ...
2627

27-
def find_option(self, param: str) -> Union[str, bool]: ...
28+
def find_option(self, param: str) -> Union[str, bool, list]: ...

pytest_reportportal/plugin.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,13 @@ def pytest_sessionstart(session):
109109
log.debug(str(response_error))
110110
config.py_test_service.rp = None
111111
return
112-
113-
attributes = gen_attributes(
114-
config._reporter_config.rp_launch_attributes)
112+
rp_launch_attributes = config._reporter_config.rp_launch_attributes
113+
attributes = gen_attributes(rp_launch_attributes) \
114+
if rp_launch_attributes else None
115115
if not config._reporter_config.rp_launch_id:
116116
config.py_test_service.start_launch(
117117
config._reporter_config.rp_launch,
118+
mode=config._reporter_config.rp_mode,
118119
attributes=attributes,
119120
description=config._reporter_config.rp_launch_description,
120121
rerun=config._reporter_config.rp_rerun,
@@ -287,6 +288,11 @@ def add_shared_option(name, help, default=None, action='store'):
287288
)
288289
add_shared_option(name='rp_uuid', help='UUID')
289290
add_shared_option(name='rp_endpoint', help='Server endpoint')
291+
add_shared_option(
292+
name='rp_mode',
293+
help='Visibility of current launch [DEFAULT, DEBUG]',
294+
default='DEFAULT'
295+
)
290296

291297
parser.addini(
292298
'rp_launch_attributes',

pytest_reportportal/service.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
"""This module includes Service functions for work with pytest agent."""
22

33
import logging
4-
from os import getenv
54
import sys
65
import traceback
6+
from os import getenv
77
from time import time
88

99
import pkg_resources
1010
import pytest
1111
from _pytest.doctest import DoctestItem
1212
from _pytest.main import Session
1313
from _pytest.nodes import File, Item
14-
from _pytest.warning_types import PytestWarning
1514
from _pytest.python import Class, Function, Instance, Module
1615
from _pytest.unittest import TestCaseFunction, UnitTestCase
17-
16+
from _pytest.warning_types import PytestWarning
1817
from reportportal_client import ReportPortalService
1918
from reportportal_client.external.google_analytics import send_event
2019
from reportportal_client.helpers import (
@@ -26,7 +25,6 @@
2625
from six import with_metaclass
2726
from six.moves import queue
2827

29-
3028
log = logging.getLogger(__name__)
3129

3230

@@ -137,11 +135,11 @@ def init_service(self,
137135
self._errors = queue.Queue()
138136
if self.rp is None:
139137
self.ignore_errors = ignore_errors
140-
self.ignored_attributes = ignored_attributes
138+
self.ignored_attributes = ignored_attributes or []
141139
self.parent_item_id = parent_item_id
142140
if self.rp_supports_parameters:
143141
self.ignored_attributes = list(
144-
set(ignored_attributes).union({'parametrize'}))
142+
set(self.ignored_attributes).union({'parametrize'}))
145143
self.log_batch_size = log_batch_size
146144
log.debug('ReportPortal - Init service: endpoint=%s, '
147145
'project=%s, uuid=%s', endpoint, project, uuid)
@@ -392,6 +390,7 @@ def finish_launch(self, status=None, **kwargs):
392390
}
393391
log.debug('ReportPortal - Finish launch: request_body=%s', fl_rq)
394392
self.rp.finish_launch(**fl_rq)
393+
self.rp = None
395394

396395
def post_log(self, message, loglevel='INFO', attachment=None):
397396
"""

tests/__init__.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1-
"""This package contains unit tests for the project."""
1+
"""This package contains tests for the project.
2+
3+
Copyright (c) 2021 http://reportportal.io .
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License
15+
"""
216

317
from six import add_move, MovedModule
418
add_move(MovedModule('mock', 'mock', 'unittest.mock'))
19+
20+
REPORT_PORTAL_SERVICE = (
21+
'pytest_reportportal.service.ReportPortalService')

tests/helpers/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""This package contains integration tests for the project.
2+
3+
Copyright (c) 2021 http://reportportal.io .
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License
15+
"""

tests/helpers/utils.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""This module contains utility code for unit tests.
2+
3+
Copyright (c) 2021 http://reportportal.io .
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License
15+
"""
16+
import pytest
17+
18+
DEFAULT_VARIABLES = {
19+
'rp_launch': 'Pytest',
20+
'rp_endpoint': "http://localhost:8080",
21+
'rp_project': "default_personal",
22+
'rp_uuid': "test_uuid"
23+
}
24+
25+
26+
def run_pytest_tests(tests=None, variables=None):
27+
"""Run specific pytest tests.
28+
29+
:param tests: a list of tests to run
30+
:param variables: parameter variables which will be passed to pytest
31+
:return: exit code
32+
"""
33+
if variables is None:
34+
variables = DEFAULT_VARIABLES
35+
36+
arguments = []
37+
arguments.append('--reportportal')
38+
for k, v in variables.items():
39+
arguments.append('-o')
40+
arguments.append('{0}={1}'.format(k, str(v)))
41+
42+
if tests is not None:
43+
for t in tests:
44+
arguments.append(t)
45+
46+
return pytest.main(arguments)

tests/integration/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""This package contains integration tests for the project.
2+
3+
Copyright (c) 2021 http://reportportal.io .
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License
15+
"""
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""This module includes integration tests for the debug mode switch."""
2+
3+
# Copyright (c) 2021 http://reportportal.io .
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License
15+
16+
import pytest
17+
from six.moves import mock
18+
19+
from tests import REPORT_PORTAL_SERVICE
20+
from tests.helpers import utils
21+
22+
23+
@mock.patch(REPORT_PORTAL_SERVICE)
24+
@pytest.mark.parametrize(['mode', 'expected_mode'], [('DEFAULT', 'DEFAULT'),
25+
('DEBUG', 'DEBUG'),
26+
(None, 'DEFAULT')])
27+
def test_launch_mode(mock_client_init, mode, expected_mode):
28+
"""Verify different launch modes are passed to `start_launch` method.
29+
30+
:param mock_client_init: Pytest fixture
31+
:param mode: a variable to be passed to pytest
32+
:param expected_mode: a value which should be passed to
33+
ReportPortalService
34+
:return: exit code
35+
"""
36+
variables = dict()
37+
if mode is not None:
38+
variables['rp_mode'] = mode
39+
variables.update(utils.DEFAULT_VARIABLES.items())
40+
result = utils.run_pytest_tests(tests=['examples/test_simple.py'],
41+
variables=variables)
42+
assert int(result) == 0, 'Exit code should be 0 (no errors)'
43+
44+
mock_client = mock_client_init.return_value
45+
assert mock_client.start_launch.call_count == 1, \
46+
'"start_launch" method was not called'
47+
48+
call_args = mock_client.start_launch.call_args_list
49+
start_launch_kwargs = call_args[0][1]
50+
assert start_launch_kwargs['mode'] == expected_mode

0 commit comments

Comments
 (0)