Skip to content

Commit 0ee8e58

Browse files
authored
PYTHON-5292 - Debug logs should only print on failed tests (#2296)
1 parent 6ed3533 commit 0ee8e58

File tree

5 files changed

+10
-10
lines changed

5 files changed

+10
-10
lines changed

.evergreen/scripts/run_tests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def run() -> None:
181181
return
182182

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

186186
# Run local tests.
187187
ret = pytest.main(TEST_ARGS + sys.argv[1:])

.evergreen/scripts/setup_tests.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,7 @@ def handle_test_env() -> None:
468468
UV_ARGS.append(f"--group {framework}")
469469

470470
else:
471-
# Use --capture=tee-sys so pytest prints test output inline:
472-
# https://docs.pytest.org/en/stable/how-to/capture-stdout-stderr.html
473-
TEST_ARGS = f"-v --capture=tee-sys --durations=5 {TEST_ARGS}"
471+
TEST_ARGS = f"-v --durations=5 {TEST_ARGS}"
474472
TEST_SUITE = TEST_SUITE_MAP.get(test_name)
475473
if TEST_SUITE:
476474
TEST_ARGS = f"-m {TEST_SUITE} {TEST_ARGS}"

CONTRIBUTING.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,11 @@ If you are running one of the `no-responder` tests, omit the `run-server` step.
352352

353353
## Enable Debug Logs
354354

355-
- Use `-o log_cli_level="DEBUG" -o log_cli=1` with `just test` or `pytest`.
356-
- 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.
357-
- You can also set `DEBUG_LOG=1` and run either `just setup-tests` or `just-test`.
355+
- 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.
356+
- 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.
357+
- Set `DEBUG_LOG=1` and run `just setup-tests`, `just-test`, or `pytest` to enable debug logs only for failed tests.
358358
- Finally, you can use `just setup-tests --debug-log`.
359-
- For evergreen patch builds, you can use `evergreen patch --param DEBUG_LOG=1` to enable debug logs for the patch.
359+
- For evergreen patch builds, you can use `evergreen patch --param DEBUG_LOG=1` to enable debug logs for failed tests in the patch.
360360

361361
## Adding a new test suite
362362

test/asynchronous/test_client.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import time
3535
import uuid
3636
from typing import Any, Iterable, Type, no_type_check
37-
from unittest import mock
37+
from unittest import mock, skipIf
3838
from unittest.mock import patch
3939

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

632+
@skipIf(os.environ.get("DEBUG_LOG"), "Enabling debug logs breaks this test")
632633
@patch("pymongo.asynchronous.srv_resolver._SrvResolver.get_hosts")
633634
async def test_detected_environment_warning(self, mock_get_hosts):
634635
with self._caplog.at_level(logging.WARN):

test/test_client.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import time
3535
import uuid
3636
from typing import Any, Iterable, Type, no_type_check
37-
from unittest import mock
37+
from unittest import mock, skipIf
3838
from unittest.mock import patch
3939

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

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

0 commit comments

Comments
 (0)