Skip to content

Commit 9ac4a91

Browse files
author
Dmitriy Gumeniuk
authored
Merge pull request #131 from bigbZik/logging_fix
Fix logging issue and adding support for pytest --setup-plan option
2 parents 03f8f25 + fe4dc59 commit 9ac4a91

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

pytest_reportportal/plugin.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,10 @@ def pytest_configure_node(node):
3737

3838

3939
def pytest_sessionstart(session):
40-
if session.config.getoption('--collect-only', default=False) is True:
41-
return
42-
4340
if session.config._reportportal_configured is False:
4441
# Stop now if the plugin is not properly configured
4542
return
4643

47-
if not session.config.option.rp_enabled:
48-
return
49-
5044
if is_master(session.config):
5145
session.config.py_test_service.init_service(
5246
project=session.config.getini('rp_project'),
@@ -87,9 +81,6 @@ def pytest_collection_modifyitems(session, config, items):
8781

8882

8983
def pytest_collection_finish(session):
90-
if session.config.getoption('--collect-only', default=False) is True:
91-
return
92-
9384
if session.config._reportportal_configured is False:
9485
# Stop now if the plugin is not properly configured
9586
return
@@ -106,16 +97,10 @@ def wait_launch(rp_client):
10697

10798

10899
def pytest_sessionfinish(session):
109-
if session.config.getoption('--collect-only', default=False) is True:
110-
return
111-
112100
if session.config._reportportal_configured is False:
113101
# Stop now if the plugin is not properly configured
114102
return
115103

116-
if not session.config.option.rp_enabled:
117-
return
118-
119104
# FixMe: currently method of RP api takes the string parameter
120105
# so it is hardcoded
121106
if is_master(session.config):
@@ -125,6 +110,13 @@ def pytest_sessionfinish(session):
125110

126111

127112
def pytest_configure(config):
113+
114+
if config.getoption('--collect-only', default=False) or \
115+
config.getoption('--setup-plan', default=False) or \
116+
not config.option.rp_enabled:
117+
config._reportportal_configured = False
118+
return
119+
128120
project = config.getini('rp_project')
129121
endpoint = config.getini('rp_endpoint')
130122
uuid = config.getini('rp_uuid')

pytest_reportportal/rp_logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def _log(self, level, msg, args,
4444
record = self.makeRecord(self.name, level, fn, lno, msg, args,
4545
exc_info, func, extra, sinfo)
4646

47-
if not record.attachment:
47+
if not getattr(record, 'attachment', None):
4848
record.attachment = attachment
4949
self.handle(record)
5050

tests/test_plugin.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
"""This modules includes unit tests for the plugin."""
22

3-
from requests.exceptions import RequestException
43
try:
54
from unittest.mock import create_autospec, Mock, patch
65
except ImportError:
76
from mock import create_autospec, Mock, patch
87

98
from _pytest.config import Config
109
from delayed_assert import expect, assert_expectations
10+
import pytest
11+
from requests.exceptions import RequestException
1112

1213
from pytest_reportportal.plugin import pytest_configure
14+
from pytest_reportportal import RPLogger
15+
16+
17+
@pytest.fixture
18+
def logger():
19+
return RPLogger("pytest_reportportal.test")
1320

1421

1522
@patch('pytest_reportportal.plugin.requests.get')
@@ -31,3 +38,28 @@ def test_stop_plugin_configuration_on_conn_error(mocked_get):
3138
expect(mocked_config._reportportal_configured is False,
3239
'The value of the _reportportal_configured is not False.')
3340
assert_expectations()
41+
42+
43+
@patch('pytest_reportportal.RPLogger.handle')
44+
@pytest.mark.parametrize("log_level", ("info", "debug", "warning", "error"))
45+
def test_logger_handle_attachment(mock_handler, logger, log_level):
46+
"""Test logger call for different log levels with some text attachment."""
47+
log_call = getattr(logger, log_level)
48+
attachment = "Some {} attachment".format(log_level)
49+
log_call("Some {} message".format(log_level), attachment=attachment)
50+
expect(mock_handler.call_count == 1, "logger.handle called more than 1 time")
51+
expect(getattr(mock_handler.call_args[0][0], "attachment") == attachment,
52+
"record.attachment in args doesn't match real value")
53+
assert_expectations()
54+
55+
56+
@patch('pytest_reportportal.RPLogger.handle')
57+
@pytest.mark.parametrize("log_level", ("info", "debug", "warning", "error"))
58+
def test_logger_handle_no_attachment(mock_handler, logger, log_level):
59+
"""Test logger call for different log levels without any attachment."""
60+
log_call = getattr(logger, log_level)
61+
log_call("Some {} message".format(log_level))
62+
expect(mock_handler.call_count == 1, "logger.handle called more than 1 time")
63+
expect(getattr(mock_handler.call_args[0][0], "attachment") is None,
64+
"record.attachment in args is not None")
65+
assert_expectations()

0 commit comments

Comments
 (0)