Skip to content

Commit

Permalink
Misc/capture logs when running examples (#1141)
Browse files Browse the repository at this point in the history
* unmask all signals in App driver's subprocess

* keep log cap only
  • Loading branch information
zhenyu-ms authored Oct 25, 2024
1 parent 0c61520 commit cf9034b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
3 changes: 3 additions & 0 deletions testplan/testing/py_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,9 @@ def pytest_runtest_logreport(self, report):

if report.failed:
self._current_case_report.status_override = Status.FAILED
# XXX: report.skipped set to True when xfail, how to distinguish?
# elif report.skipped:
# self._current_case_report.status_override = Status.SKIPPED
else:
self._current_case_report.pass_if_empty()
self._current_case_report.runtime_status = RuntimeStatus.FINISHED
Expand Down
12 changes: 11 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import shutil
import sys
import tempfile
from logging import Logger
from logging import Logger, INFO
from logging.handlers import RotatingFileHandler

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

logger.handler.close()


@pytest.fixture
def captplog(caplog):
from testplan.common.utils.logger import TESTPLAN_LOGGER

caplog.set_level(INFO)
TESTPLAN_LOGGER.addHandler(caplog.handler)
yield caplog
TESTPLAN_LOGGER.removeHandler(caplog.handler)
8 changes: 6 additions & 2 deletions tests/functional/examples/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def _param_formatter(param):
],
ids=_param_formatter,
)
def test_example(root, filename, runpath):
def test_example(root, filename, runpath, captplog):
file_path = os.path.join(root, filename)

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

run_example_in_process(
filename, root, KNOWN_EXCEPTIONS, ["--runpath", runpath]
filename,
root,
KNOWN_EXCEPTIONS,
["--runpath", runpath],
captplog,
)
18 changes: 14 additions & 4 deletions tests/helpers/example_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@


def run_example_in_process(
filename, root, known_exceptions, cmdline_args=None
filename,
root,
known_exceptions,
cmdline_args=None,
caplog=None,
):
if caplog is not None:
caplog.clear()

sys_path = copy.copy(sys.path)
sys_argv = copy.copy(sys.argv)

Expand All @@ -43,9 +50,12 @@ def run_example_in_process(
except SystemExit as e:
if e.code not in SUCCES_EXIT_CODES:
assert (
"# This plan contains tests that demonstrate failures as well."
) == second_line, (
'Expected "{}" example to pass, it failed'.format(file_path)
second_line
== "# This plan contains tests that demonstrate failures as well."
), 'Expected "{}" example to pass, it failed'.format(file_path) + (
"\nCaptured logs:\n{}".format(caplog.text)
if caplog is not None
else ""
)
except Exception as e:
for exception in known_exceptions:
Expand Down

0 comments on commit cf9034b

Please sign in to comment.