diff --git a/mcserverwrapper/test/conftest.py b/mcserverwrapper/test/conftest.py index 8908878..808585a 100644 --- a/mcserverwrapper/test/conftest.py +++ b/mcserverwrapper/test/conftest.py @@ -10,8 +10,10 @@ import sys import pathlib -from pytest import Metafunc +import pytest +from pytest import Metafunc, TestReport, Session, ExitCode +from .helpers.common_helper import get_mcserver_log from .integration_tests import test_forge # Adding source path to sys path @@ -26,7 +28,7 @@ from .fixtures import newest_server_jar from .helpers.common_helper import get_vanilla_urls -#pylint: enable=wrong-import-position +# pylint: enable=wrong-import-position def pytest_generate_tests(metafunc: Metafunc): """Pytest hook""" @@ -48,3 +50,42 @@ def pytest_generate_tests(metafunc: Metafunc): metafunc.parametrize(argnames="forge_download_url", argvalues=test_forge.FORGE_URLS.values(), ids=[f"test_version_{item}" for item in test_forge.FORGE_URLS]) + +# the arguments are needed for the pytest hooks to work correctly +# pylint: disable=unused-argument +@pytest.hookimpl(wrapper=True, tryfirst=True) +def pytest_runtest_makereport(item, call): + """Save mcserverwrapper.log to the current pytest item""" + + # execute all other hooks to obtain the report object + rep: TestReport = yield + + # we only look at actual failing test calls, not setup/teardown + if rep.when == "call" and rep.outcome == "failed": + item.mcserverlog = get_mcserver_log() + + return rep + +@pytest.hookimpl(trylast=True) +def pytest_sessionfinish(session: Session, exitstatus: ExitCode): + """Print all saved mcserverwrapper.log after all tests finished""" + + if len(session.items) == 0: + return + + print("") + print("") + print("=" * 20, end="") + print(" McServerWrapper.log ", end="") + print("=" * 20) + + for item in session.items: + # skip tests that didn't fail + if hasattr(item, "mcserverlog"): + print("") + print("-" * 20, end="") + print(f" {item.name} ", end="") + print("-" * 20, end="\n") + for line in item.mcserverlog.split("\n"): + print(line) +# pylint: enable=unused-argument diff --git a/mcserverwrapper/test/helpers/common_helper.py b/mcserverwrapper/test/helpers/common_helper.py index 4d3d4dc..c165b70 100644 --- a/mcserverwrapper/test/helpers/common_helper.py +++ b/mcserverwrapper/test/helpers/common_helper.py @@ -16,6 +16,8 @@ from javascript import require, once +from mcserverwrapper.src.util import logger + mineflayer = require('mineflayer') def setup_workspace(): @@ -122,6 +124,19 @@ def download_file(url, counter=""): file.write(req.content) return local_filename +def get_mcserver_log() -> str: + """Return the current mcserverwrapper.log as a string if it exists""" + + if logger.logfile_path is None: + print("Logger was not yet setup, cannot print logfile") + return "" + + data = f"Printing out {logger.LOGFILE_NAME}:\n" + with open(logger.logfile_path, "r", encoding="utf8") as f: + for line in f.readlines(): + data += line + return data + def get_vanilla_urls(): """Function written by @Pfefan"""