1010import sys
1111import 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
1517from .integration_tests import test_forge
1618
1719# Adding source path to sys path
2628from .fixtures import newest_server_jar
2729
2830from .helpers .common_helper import get_vanilla_urls
29- #pylint: enable=wrong-import-position
31+ # pylint: enable=wrong-import-position
3032
3133def 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
0 commit comments