Skip to content

Commit cf9034b

Browse files
authored
Misc/capture logs when running examples (#1141)
* unmask all signals in App driver's subprocess * keep log cap only
1 parent 0c61520 commit cf9034b

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

testplan/testing/py_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,9 @@ def pytest_runtest_logreport(self, report):
482482

483483
if report.failed:
484484
self._current_case_report.status_override = Status.FAILED
485+
# XXX: report.skipped set to True when xfail, how to distinguish?
486+
# elif report.skipped:
487+
# self._current_case_report.status_override = Status.SKIPPED
485488
else:
486489
self._current_case_report.pass_if_empty()
487490
self._current_case_report.runtime_status = RuntimeStatus.FINISHED

tests/conftest.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import shutil
55
import sys
66
import tempfile
7-
from logging import Logger
7+
from logging import Logger, INFO
88
from logging.handlers import RotatingFileHandler
99

1010
sys.path.append(os.path.join(os.path.dirname(__file__), "helpers"))
@@ -125,3 +125,13 @@ def rotating_logger(runpath):
125125
yield logger
126126

127127
logger.handler.close()
128+
129+
130+
@pytest.fixture
131+
def captplog(caplog):
132+
from testplan.common.utils.logger import TESTPLAN_LOGGER
133+
134+
caplog.set_level(INFO)
135+
TESTPLAN_LOGGER.addHandler(caplog.handler)
136+
yield caplog
137+
TESTPLAN_LOGGER.removeHandler(caplog.handler)

tests/functional/examples/test_examples.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def _param_formatter(param):
9090
],
9191
ids=_param_formatter,
9292
)
93-
def test_example(root, filename, runpath):
93+
def test_example(root, filename, runpath, captplog):
9494
file_path = os.path.join(root, filename)
9595

9696
if ON_WINDOWS and any(
@@ -106,5 +106,9 @@ def test_example(root, filename, runpath):
106106
pytest.skip()
107107

108108
run_example_in_process(
109-
filename, root, KNOWN_EXCEPTIONS, ["--runpath", runpath]
109+
filename,
110+
root,
111+
KNOWN_EXCEPTIONS,
112+
["--runpath", runpath],
113+
captplog,
110114
)

tests/helpers/example_runner.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@
1818

1919

2020
def run_example_in_process(
21-
filename, root, known_exceptions, cmdline_args=None
21+
filename,
22+
root,
23+
known_exceptions,
24+
cmdline_args=None,
25+
caplog=None,
2226
):
27+
if caplog is not None:
28+
caplog.clear()
29+
2330
sys_path = copy.copy(sys.path)
2431
sys_argv = copy.copy(sys.argv)
2532

@@ -43,9 +50,12 @@ def run_example_in_process(
4350
except SystemExit as e:
4451
if e.code not in SUCCES_EXIT_CODES:
4552
assert (
46-
"# This plan contains tests that demonstrate failures as well."
47-
) == second_line, (
48-
'Expected "{}" example to pass, it failed'.format(file_path)
53+
second_line
54+
== "# This plan contains tests that demonstrate failures as well."
55+
), 'Expected "{}" example to pass, it failed'.format(file_path) + (
56+
"\nCaptured logs:\n{}".format(caplog.text)
57+
if caplog is not None
58+
else ""
4959
)
5060
except Exception as e:
5161
for exception in known_exceptions:

0 commit comments

Comments
 (0)