Skip to content

Commit cd9c6fe

Browse files
authored
Log buffer flushing
1 parent d6f50d2 commit cd9c6fe

File tree

5 files changed

+90
-33
lines changed

5 files changed

+90
-33
lines changed

examples/test_rp_logging.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright (c) 2022 https://reportportal.io .
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# https://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License
13+
14+
import logging
15+
16+
logging.basicConfig(level=logging.INFO)
17+
18+
logger = logging.getLogger(__name__)
19+
20+
LOG_MESSAGE = "Standard logger logs to Report Portal"
21+
22+
23+
def test_report_portal_logging():
24+
logger.info(LOG_MESSAGE)

pytest_reportportal/listener.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
"""RPReportListener implements Pytest hooks required for item reporting."""
22

3-
import pytest
43
import logging
4+
5+
import pytest
6+
57
try:
68
from html import escape # python3
79
except ImportError:
810
from cgi import escape # python2
911

10-
11-
try:
12-
# This try/except can go away once we support pytest >= 3.3
13-
import _pytest.logging
14-
15-
PYTEST_HAS_LOGGING_PLUGIN = True
16-
from .rp_logging import RPLogHandler, patching_logger_class
17-
except ImportError:
18-
PYTEST_HAS_LOGGING_PLUGIN = False
12+
import _pytest.logging
13+
from .rp_logging import RPLogHandler, patching_logger_class
1914

2015

2116
class RPReportListener(object):
@@ -36,12 +31,11 @@ def __init__(self, py_test_service,
3631
self.result = None
3732
self.issue = {}
3833
self._log_level = log_level
39-
if PYTEST_HAS_LOGGING_PLUGIN:
40-
self._log_handler = \
41-
RPLogHandler(py_test_service=py_test_service,
42-
level=log_level,
43-
filter_client_logs=True,
44-
endpoint=endpoint)
34+
self._log_handler = \
35+
RPLogHandler(py_test_service=py_test_service,
36+
level=log_level,
37+
filter_client_logs=True,
38+
endpoint=endpoint)
4539

4640
@pytest.hookimpl(hookwrapper=True)
4741
def pytest_runtest_protocol(self, item):
@@ -53,17 +47,15 @@ def pytest_runtest_protocol(self, item):
5347
"""
5448
self._add_issue_id_marks(item)
5549
item_id = self.py_test_service.start_pytest_item(item)
56-
if PYTEST_HAS_LOGGING_PLUGIN:
57-
# This check can go away once we support pytest >= 3.3
58-
with patching_logger_class():
59-
with _pytest.logging.catching_logs(self._log_handler,
60-
level=self._log_level):
61-
yield
62-
else:
63-
yield
50+
with patching_logger_class():
51+
with _pytest.logging.catching_logs(self._log_handler,
52+
level=self._log_level):
53+
yield
6454
# Finishing item in RP
6555
self.py_test_service.finish_pytest_item(
6656
item, item_id, self.result or 'SKIPPED', self.issue or None)
57+
# Flush log buffer
58+
self.py_test_service.rp.terminate()
6759

6860
@pytest.hookimpl(hookwrapper=True)
6961
def pytest_runtest_makereport(self, item):

tests/helpers/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License
1515
"""
16+
import random
17+
import time
18+
1619
import pytest
1720

1821
DEFAULT_VARIABLES = {
@@ -44,3 +47,8 @@ def run_pytest_tests(tests=None, variables=None):
4447
arguments.append(t)
4548

4649
return pytest.main(arguments)
50+
51+
52+
def item_id_gen(**kwargs):
53+
return "{}-{}-{}".format(kwargs['name'], str(round(time.time() * 1000)),
54+
random.randint(0, 9999))
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""This module includes integration test for the log flushing."""
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+
from six.moves import mock
17+
18+
from tests import REPORT_PORTAL_SERVICE
19+
from tests.helpers import utils
20+
from multiprocessing.pool import ThreadPool
21+
22+
23+
@mock.patch(REPORT_PORTAL_SERVICE)
24+
def test_logging_flushing(mock_client_init):
25+
"""Verify log buffer flushes after test finish.
26+
27+
:param mock_client_init: Pytest fixture
28+
"""
29+
def run_test():
30+
return utils.run_pytest_tests(['examples/test_rp_logging.py'])
31+
32+
pool = ThreadPool(processes=1)
33+
async_result = pool.apply_async(run_test)
34+
result = async_result.get()
35+
pool.terminate()
36+
37+
assert int(result) == 0, 'Exit code should be 0 (no errors)'
38+
39+
mock_client = mock_client_init.return_value
40+
assert mock_client.terminate.call_count == 1, \
41+
'"terminate" method was not called at the end of the test'

tests/integration/test_suite_hierarchy.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License
1515

16-
import random
17-
import time
18-
1916
import pytest
2017
from delayed_assert import expect, assert_expectations
2118
from six.moves import mock
@@ -25,11 +22,6 @@
2522
from tests.helpers import utils
2623

2724

28-
def item_id_gen(**kwargs):
29-
return "{}-{}-{}".format(kwargs['name'], str(round(time.time() * 1000)),
30-
random.randint(0, 9999))
31-
32-
3325
def verify_start_item_parameters(mock_client, expected_items):
3426
assert mock_client.start_test_item.call_count == len(expected_items), \
3527
'"start_test_item" method was called incorrect number of times'
@@ -54,7 +46,7 @@ def test_rp_hierarchy_parameters(mock_client_init, test, variables,
5446
:param mock_client_init: Pytest fixture
5547
"""
5648
mock_client = mock_client_init.return_value
57-
mock_client.start_test_item.side_effect = item_id_gen
49+
mock_client.start_test_item.side_effect = utils.item_id_gen
5850

5951
result = utils.run_pytest_tests(tests=test, variables=variables)
6052
assert int(result) == 0, 'Exit code should be 0 (no errors)'

0 commit comments

Comments
 (0)