Skip to content

Commit bb9fc8a

Browse files
author
Dmitriy Gumeniuk
authored
Merge pull request #152 from iivanou/handle_exitfirst_correctly
Terminate service correctly with the maxfail option
2 parents f3d41e2 + fdd7c87 commit bb9fc8a

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

pytest_reportportal/plugin.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,14 @@ def pytest_sessionfinish(session):
101101
# Stop now if the plugin is not properly configured
102102
return
103103

104-
# FixMe: currently method of RP api takes the string parameter
105-
# so it is hardcoded
104+
shouldfail = getattr(session, 'shouldfail', False)
105+
nowait = True if shouldfail else False
106+
106107
if is_master(session.config):
107-
session.config.py_test_service.finish_launch(status='RP_Launch')
108+
session.config.py_test_service.finish_launch(
109+
status='RP_Launch', force=nowait)
108110

109-
session.config.py_test_service.terminate_service()
111+
session.config.py_test_service.terminate_service(nowait=nowait)
110112

111113

112114
def pytest_configure(config):

pytest_reportportal/service.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def finish_pytest_item(self, test_item, status, issue=None):
295295
log.debug('ReportPortal - End TestSuite: request_body=%s', payload)
296296
self.RP.finish_test_item(**payload)
297297

298-
def finish_launch(self, launch=None, status='rp_launch'):
298+
def finish_launch(self, status='rp_launch', force=False):
299299
self._stop_if_necessary()
300300
if self.RP is None:
301301
return
@@ -306,7 +306,10 @@ def finish_launch(self, launch=None, status='rp_launch'):
306306
'status': status
307307
}
308308
log.debug('ReportPortal - Finish launch: request_body=%s', fl_rq)
309-
self.RP.finish_launch(**fl_rq)
309+
if not force:
310+
self.RP.finish_launch(**fl_rq)
311+
else:
312+
self.RP.stop_launch(**fl_rq)
310313

311314
def post_log(self, message, loglevel='INFO', attachment=None):
312315
self._stop_if_necessary()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def read_file(fname):
88
return f.read()
99

1010

11-
version = '1.0.8'
11+
version = '1.0.9'
1212

1313

1414
requirements = [

tests/test_plugin.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
from mock import create_autospec, Mock, patch
77

88
from _pytest.config import Config
9-
from delayed_assert import expect, assert_expectations
9+
from delayed_assert import expect, assert_expectations
1010
import pytest
1111
from requests.exceptions import RequestException
1212

1313
from pytest_reportportal.listener import RPReportListener
14-
from pytest_reportportal.plugin import pytest_configure
14+
from pytest_reportportal.plugin import pytest_configure, pytest_sessionfinish
1515
from pytest_reportportal.service import PyTestServiceClass
1616
from pytest_reportportal import RPLogger
1717

@@ -169,3 +169,27 @@ def iter_markers(name=None):
169169
expect(test_item.add_marker.call_args[0][0] == "issue:456823",
170170
"item.add_marker called with incorrect parameters")
171171
assert_expectations()
172+
173+
174+
@patch('pytest_reportportal.plugin.is_master', Mock(return_value=True))
175+
@pytest.mark.parametrize('shouldfail, outcome', [
176+
(False, False), ('stopping after 1 failures', True)
177+
])
178+
def test_sessionfinish_with_maxfail(shouldfail, outcome):
179+
"""Test session_finish logic when the maxfail Pytest argument is in use.
180+
181+
:param shouldfail: shouldfail attribute value for the Session object
182+
:param outcome: nowait argument value passed to the terminate_service()
183+
"""
184+
mocked_session = Mock()
185+
mocked_session.shouldfail = shouldfail
186+
mocked_session.config = Mock()
187+
mocked_session.config._reportportal_configured = True
188+
mocked_session.config.py_test_service.terminate_service = Mock()
189+
mocked_session.config.py_test_service.finish_launch = Mock()
190+
pytest_sessionfinish(mocked_session)
191+
expect(lambda: mocked_session.config.py_test_service.
192+
finish_launch.assert_called_with(force=outcome, status='RP_Launch'))
193+
expect(lambda: mocked_session.config.py_test_service.
194+
terminate_service.assert_called_with(nowait=outcome))
195+
assert_expectations()

0 commit comments

Comments
 (0)