Skip to content

PYTHON-5292 - Debug logs should only print on failed tests #2296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .evergreen/scripts/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def run() -> None:
return

if os.environ.get("DEBUG_LOG"):
TEST_ARGS.extend(f"-o log_cli_level={logging.DEBUG} -o log_cli=1".split())
TEST_ARGS.extend(f"-o log_cli_level={logging.DEBUG}".split())

# Run local tests.
ret = pytest.main(TEST_ARGS + sys.argv[1:])
Expand Down
4 changes: 1 addition & 3 deletions .evergreen/scripts/setup_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,7 @@ def handle_test_env() -> None:
UV_ARGS.append(f"--group {framework}")

else:
# Use --capture=tee-sys so pytest prints test output inline:
# https://docs.pytest.org/en/stable/how-to/capture-stdout-stderr.html
TEST_ARGS = f"-v --capture=tee-sys --durations=5 {TEST_ARGS}"
TEST_ARGS = f"-v --durations=5 {TEST_ARGS}"
TEST_SUITE = TEST_SUITE_MAP.get(test_name)
if TEST_SUITE:
TEST_ARGS = f"-m {TEST_SUITE} {TEST_ARGS}"
Expand Down
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,11 @@ If you are running one of the `no-responder` tests, omit the `run-server` step.

## Enable Debug Logs

- Use `-o log_cli_level="DEBUG" -o log_cli=1` with `just test` or `pytest`.
- Add `log_cli_level = "DEBUG` and `log_cli = 1` to the `tool.pytest.ini_options` section in `pyproject.toml` for Evergreen patches or to enable debug logs by default on your machine.
- You can also set `DEBUG_LOG=1` and run either `just setup-tests` or `just-test`.
- Use `-o log_cli_level="DEBUG" -o log_cli=1` with `just test` or `pytest` to output all debug logs to the terminal. **Warning**: This will output a huge amount of logs.
- Add `log_cli=1` and `log_cli_level="DEBUG"` to the `tool.pytest.ini_options` section in `pyproject.toml` to enable debug logs in this manner by default on your machine.
- Set `DEBUG_LOG=1` and run `just setup-tests`, `just-test`, or `pytest` to enable debug logs only for failed tests.
- Finally, you can use `just setup-tests --debug-log`.
- For evergreen patch builds, you can use `evergreen patch --param DEBUG_LOG=1` to enable debug logs for the patch.
- For evergreen patch builds, you can use `evergreen patch --param DEBUG_LOG=1` to enable debug logs for failed tests in the patch.

## Adding a new test suite

Expand Down
3 changes: 2 additions & 1 deletion test/asynchronous/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import time
import uuid
from typing import Any, Iterable, Type, no_type_check
from unittest import mock
from unittest import mock, skipIf
from unittest.mock import patch

import pytest
Expand Down Expand Up @@ -629,6 +629,7 @@ def test_detected_environment_logging(self, mock_get_hosts):
logs = [record.getMessage() for record in cm.records if record.name == "pymongo.client"]
self.assertEqual(len(logs), 7)

@skipIf(os.environ.get("DEBUG_LOG"), "Enabling debug logs breaks this test")
@patch("pymongo.asynchronous.srv_resolver._SrvResolver.get_hosts")
async def test_detected_environment_warning(self, mock_get_hosts):
with self._caplog.at_level(logging.WARN):
Expand Down
3 changes: 2 additions & 1 deletion test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import time
import uuid
from typing import Any, Iterable, Type, no_type_check
from unittest import mock
from unittest import mock, skipIf
from unittest.mock import patch

import pytest
Expand Down Expand Up @@ -622,6 +622,7 @@ def test_detected_environment_logging(self, mock_get_hosts):
logs = [record.getMessage() for record in cm.records if record.name == "pymongo.client"]
self.assertEqual(len(logs), 7)

@skipIf(os.environ.get("DEBUG_LOG"), "Enabling debug logs breaks this test")
@patch("pymongo.synchronous.srv_resolver._SrvResolver.get_hosts")
def test_detected_environment_warning(self, mock_get_hosts):
with self._caplog.at_level(logging.WARN):
Expand Down
Loading