Skip to content

Commit

Permalink
logging: order hookimpl's in chronological order
Browse files Browse the repository at this point in the history
Makes it easier to understand what's going on.
  • Loading branch information
bluetech committed May 17, 2020
1 parent 3f82006 commit f71ec8c
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions src/_pytest/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,14 @@ def _log_cli_enabled(self):

return True

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_sessionstart(self):
self.log_cli_handler.set_when("sessionstart")

with catching_logs(self.log_cli_handler, level=self.log_cli_level):
with catching_logs(self.log_file_handler, level=self.log_file_level):
yield

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_collection(self) -> Generator[None, None, None]:
self.log_cli_handler.set_when("collection")
Expand All @@ -604,6 +612,31 @@ def pytest_collection(self) -> Generator[None, None, None]:
with catching_logs(self.log_file_handler, level=self.log_file_level):
yield

@pytest.hookimpl(hookwrapper=True)
def pytest_runtestloop(self, session):
"""Runs all collected test items."""

if session.config.option.collectonly:
yield
return

if self._log_cli_enabled() and self._config.getoption("verbose") < 1:
# setting verbose flag is needed to avoid messy test progress output
self._config.option.verbose = 1

with catching_logs(self.log_cli_handler, level=self.log_cli_level):
with catching_logs(self.log_file_handler, level=self.log_file_level):
yield # run all the tests

@pytest.hookimpl
def pytest_runtest_logstart(self):
self.log_cli_handler.reset()
self.log_cli_handler.set_when("start")

@pytest.hookimpl
def pytest_runtest_logreport(self):
self.log_cli_handler.set_when("logreport")

def _runtest_for(self, item: nodes.Item, when: str) -> Generator[None, None, None]:
"""Implements the internals of pytest_runtest_xxx() hook."""
log_handler = LogCaptureHandler()
Expand Down Expand Up @@ -639,19 +672,10 @@ def pytest_runtest_teardown(self, item):
del item._store[catch_log_handlers_key]
del item._store[catch_log_handler_key]

@pytest.hookimpl
def pytest_runtest_logstart(self):
self.log_cli_handler.reset()
self.log_cli_handler.set_when("start")

@pytest.hookimpl
def pytest_runtest_logfinish(self):
self.log_cli_handler.set_when("finish")

@pytest.hookimpl
def pytest_runtest_logreport(self):
self.log_cli_handler.set_when("logreport")

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_sessionfinish(self):
self.log_cli_handler.set_when("sessionfinish")
Expand All @@ -660,30 +684,6 @@ def pytest_sessionfinish(self):
with catching_logs(self.log_file_handler, level=self.log_file_level):
yield

@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_sessionstart(self):
self.log_cli_handler.set_when("sessionstart")

with catching_logs(self.log_cli_handler, level=self.log_cli_level):
with catching_logs(self.log_file_handler, level=self.log_file_level):
yield

@pytest.hookimpl(hookwrapper=True)
def pytest_runtestloop(self, session):
"""Runs all collected test items."""

if session.config.option.collectonly:
yield
return

if self._log_cli_enabled() and self._config.getoption("verbose") < 1:
# setting verbose flag is needed to avoid messy test progress output
self._config.option.verbose = 1

with catching_logs(self.log_cli_handler, level=self.log_cli_level):
with catching_logs(self.log_file_handler, level=self.log_file_level):
yield # run all the tests

@pytest.hookimpl
def pytest_unconfigure(self):
# Close the FileHandler explicitly.
Expand Down

0 comments on commit f71ec8c

Please sign in to comment.