Skip to content

Commit f71ec8c

Browse files
committed
logging: order hookimpl's in chronological order
Makes it easier to understand what's going on.
1 parent 3f82006 commit f71ec8c

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

src/_pytest/logging.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,14 @@ def _log_cli_enabled(self):
596596

597597
return True
598598

599+
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
600+
def pytest_sessionstart(self):
601+
self.log_cli_handler.set_when("sessionstart")
602+
603+
with catching_logs(self.log_cli_handler, level=self.log_cli_level):
604+
with catching_logs(self.log_file_handler, level=self.log_file_level):
605+
yield
606+
599607
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
600608
def pytest_collection(self) -> Generator[None, None, None]:
601609
self.log_cli_handler.set_when("collection")
@@ -604,6 +612,31 @@ def pytest_collection(self) -> Generator[None, None, None]:
604612
with catching_logs(self.log_file_handler, level=self.log_file_level):
605613
yield
606614

615+
@pytest.hookimpl(hookwrapper=True)
616+
def pytest_runtestloop(self, session):
617+
"""Runs all collected test items."""
618+
619+
if session.config.option.collectonly:
620+
yield
621+
return
622+
623+
if self._log_cli_enabled() and self._config.getoption("verbose") < 1:
624+
# setting verbose flag is needed to avoid messy test progress output
625+
self._config.option.verbose = 1
626+
627+
with catching_logs(self.log_cli_handler, level=self.log_cli_level):
628+
with catching_logs(self.log_file_handler, level=self.log_file_level):
629+
yield # run all the tests
630+
631+
@pytest.hookimpl
632+
def pytest_runtest_logstart(self):
633+
self.log_cli_handler.reset()
634+
self.log_cli_handler.set_when("start")
635+
636+
@pytest.hookimpl
637+
def pytest_runtest_logreport(self):
638+
self.log_cli_handler.set_when("logreport")
639+
607640
def _runtest_for(self, item: nodes.Item, when: str) -> Generator[None, None, None]:
608641
"""Implements the internals of pytest_runtest_xxx() hook."""
609642
log_handler = LogCaptureHandler()
@@ -639,19 +672,10 @@ def pytest_runtest_teardown(self, item):
639672
del item._store[catch_log_handlers_key]
640673
del item._store[catch_log_handler_key]
641674

642-
@pytest.hookimpl
643-
def pytest_runtest_logstart(self):
644-
self.log_cli_handler.reset()
645-
self.log_cli_handler.set_when("start")
646-
647675
@pytest.hookimpl
648676
def pytest_runtest_logfinish(self):
649677
self.log_cli_handler.set_when("finish")
650678

651-
@pytest.hookimpl
652-
def pytest_runtest_logreport(self):
653-
self.log_cli_handler.set_when("logreport")
654-
655679
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
656680
def pytest_sessionfinish(self):
657681
self.log_cli_handler.set_when("sessionfinish")
@@ -660,30 +684,6 @@ def pytest_sessionfinish(self):
660684
with catching_logs(self.log_file_handler, level=self.log_file_level):
661685
yield
662686

663-
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
664-
def pytest_sessionstart(self):
665-
self.log_cli_handler.set_when("sessionstart")
666-
667-
with catching_logs(self.log_cli_handler, level=self.log_cli_level):
668-
with catching_logs(self.log_file_handler, level=self.log_file_level):
669-
yield
670-
671-
@pytest.hookimpl(hookwrapper=True)
672-
def pytest_runtestloop(self, session):
673-
"""Runs all collected test items."""
674-
675-
if session.config.option.collectonly:
676-
yield
677-
return
678-
679-
if self._log_cli_enabled() and self._config.getoption("verbose") < 1:
680-
# setting verbose flag is needed to avoid messy test progress output
681-
self._config.option.verbose = 1
682-
683-
with catching_logs(self.log_cli_handler, level=self.log_cli_level):
684-
with catching_logs(self.log_file_handler, level=self.log_file_level):
685-
yield # run all the tests
686-
687687
@pytest.hookimpl
688688
def pytest_unconfigure(self):
689689
# Close the FileHandler explicitly.

0 commit comments

Comments
 (0)