Refactor debug and trace log tests (#506)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Florimond Manca · committed by GitHub · parent 07586f9 · Browse files · 2 files·+36 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+36 added−18 removed
- tests:+36 added−18 removed
Provenance
- Commit
2fcf23b- Tree
f535f53- Author
Florimond Manca <florimond.manca@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
07586f9
2 files changed+36 added−18 removed
modified:tests/test_utils.py+16 added-18 removed
1 import asyncio2-import logging2 import os3 4 import pytest5 6 import httpx8-from httpx import utils7 from httpx.utils import (8 ElapsedTimer,9 get_ca_bundle_from_env,10 get_environment_proxies,2 unmodified lines13 obfuscate_sensitive_headers,14 parse_header_links,15 should_not_be_proxied,16 )17+from tests.utils import override_log_level18 19 20 @pytest.mark.parametrize(21 "encoding",79 unmodified lines101 assert parse_header_links(value) == expected102 103 104 @pytest.mark.asyncio106-@pytest.mark.parametrize("httpx_log_level", ["trace", "debug"])107-async def test_httpx_log_level_enabled_stderr_logging(server, capsys, httpx_log_level):108- os.environ["HTTPX_LOG_LEVEL"] = httpx_log_level105+async def test_logs_debug(server, capsys):106+ with override_log_level("debug"):107+ async with httpx.AsyncClient() as client:108+ response = await client.get(server.url)109+ assert response.status_code == 200110+ stderr = capsys.readouterr().err111+ assert "httpx.dispatch.connection_pool" not in stderr112 110- # Force a reload on the logging handlers111- utils._LOGGER_INITIALIZED = False112- utils.get_logger("httpx")113 114+@pytest.mark.asyncio115+async def test_logs_trace(server, capsys):116+ with override_log_level("trace"):114- async with httpx.AsyncClient() as client:115- await client.get(server.url)117+ async with httpx.AsyncClient() as client:118+ response = await client.get(server.url)119+ assert response.status_code == 200120+ stderr = capsys.readouterr().err121+ assert "httpx.dispatch.connection_pool" in stderr122 117- if httpx_log_level == "trace":118- assert "httpx.dispatch.connection_pool" in capsys.readouterr().err119- else:120- assert "httpx.dispatch.connection_pool" not in capsys.readouterr().err123 122- # Reset the logger so we don't have verbose output in all unit tests123- logging.getLogger("httpx").handlers = []124-125-124 def test_get_ssl_cert_file():125 # Two environments is not set.126 assert get_ca_bundle_from_env() is None127 151 unmodified linesadded:tests/utils.py+20 added
1+import contextlib2+import logging3+import os4+5+from httpx import utils6+7+8+@contextlib.contextmanager9+def override_log_level(log_level: str):10+ os.environ["HTTPX_LOG_LEVEL"] = log_level11+12+ # Force a reload on the logging handlers13+ utils._LOGGER_INITIALIZED = False14+ utils.get_logger("httpx")15+16+ try:17+ yield18+ finally:19+ # Reset the logger so we don't have verbose output in all unit tests20+ logging.getLogger("httpx").handlers = []