Skip to content

Commit 21b4c3d

Browse files
author
Ableytner
committed
print server logs after test fails
1 parent 27e1634 commit 21b4c3d

2 files changed

Lines changed: 58 additions & 2 deletions

File tree

mcserverwrapper/test/conftest.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
import sys
1111
import pathlib
1212

13-
from pytest import Metafunc
13+
import pytest
14+
from pytest import Metafunc, TestReport, Session, ExitCode
1415

16+
from .helpers.common_helper import get_mcserver_log
1517
from .integration_tests import test_forge
1618

1719
# Adding source path to sys path
@@ -26,7 +28,7 @@
2628
from .fixtures import newest_server_jar
2729

2830
from .helpers.common_helper import get_vanilla_urls
29-
#pylint: enable=wrong-import-position
31+
# pylint: enable=wrong-import-position
3032

3133
def pytest_generate_tests(metafunc: Metafunc):
3234
"""Pytest hook"""
@@ -48,3 +50,42 @@ def pytest_generate_tests(metafunc: Metafunc):
4850
metafunc.parametrize(argnames="forge_download_url",
4951
argvalues=test_forge.FORGE_URLS.values(),
5052
ids=[f"test_version_{item}" for item in test_forge.FORGE_URLS])
53+
54+
# the arguments are needed for the pytest hooks to work correctly
55+
# pylint: disable=unused-argument
56+
@pytest.hookimpl(wrapper=True, tryfirst=True)
57+
def pytest_runtest_makereport(item, call):
58+
"""Save mcserverwrapper.log to the current pytest item"""
59+
60+
# execute all other hooks to obtain the report object
61+
rep: TestReport = yield
62+
63+
# we only look at actual failing test calls, not setup/teardown
64+
if rep.when == "call" and rep.outcome == "failed":
65+
item.mcserverlog = get_mcserver_log()
66+
67+
return rep
68+
69+
@pytest.hookimpl(trylast=True)
70+
def pytest_sessionfinish(session: Session, exitstatus: ExitCode):
71+
"""Print all saved mcserverwrapper.log after all tests finished"""
72+
73+
if len(session.items) == 0:
74+
return
75+
76+
print("")
77+
print("")
78+
print("=" * 20, end="")
79+
print(" McServerWrapper.log ", end="")
80+
print("=" * 20)
81+
82+
for item in session.items:
83+
# skip tests that didn't fail
84+
if hasattr(item, "mcserverlog"):
85+
print("")
86+
print("-" * 20, end="")
87+
print(f" {item.name} ", end="")
88+
print("-" * 20, end="\n")
89+
for line in item.mcserverlog.split("\n"):
90+
print(line)
91+
# pylint: enable=unused-argument

mcserverwrapper/test/helpers/common_helper.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
from javascript import require, once
1818

19+
from mcserverwrapper.src.util import logger
20+
1921
mineflayer = require('mineflayer')
2022

2123
def setup_workspace():
@@ -122,6 +124,19 @@ def download_file(url, counter=""):
122124
file.write(req.content)
123125
return local_filename
124126

127+
def get_mcserver_log() -> str:
128+
"""Return the current mcserverwrapper.log as a string if it exists"""
129+
130+
if logger.logfile_path is None:
131+
print("Logger was not yet setup, cannot print logfile")
132+
return ""
133+
134+
data = f"Printing out {logger.LOGFILE_NAME}:\n"
135+
with open(logger.logfile_path, "r", encoding="utf8") as f:
136+
for line in f.readlines():
137+
data += line
138+
return data
139+
125140
def get_vanilla_urls():
126141
"""Function written by @Pfefan"""
127142

0 commit comments

Comments
 (0)