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

This commit is contained in:
Noah Stapp 2025-04-21 09:43:58 -04:00 committed by GitHub
parent 6ed3533b73
commit 0ee8e585c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 10 additions and 10 deletions

View File

@ -181,7 +181,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:])

View File

@ -468,9 +468,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}"

View File

@ -352,11 +352,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

View File

@ -34,7 +34,7 @@ import threading
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
@ -629,6 +629,7 @@ class AsyncClientUnitTest(AsyncUnitTest):
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):

View File

@ -34,7 +34,7 @@ import threading
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
@ -622,6 +622,7 @@ class ClientUnitTest(UnitTest):
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):