]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Refactor debug and trace log tests (#506)
authorFlorimond Manca <florimond.manca@gmail.com>
Wed, 6 Nov 2019 11:04:20 +0000 (12:04 +0100)
committerGitHub <noreply@github.com>
Wed, 6 Nov 2019 11:04:20 +0000 (12:04 +0100)
tests/test_utils.py
tests/utils.py [new file with mode: 0644]

index 7546df077a73166d2d0e3cee3d02b7e0d1518489..959fbf8f663c84c3e3cd5ebd2695b74723b9e45a 100644 (file)
@@ -1,11 +1,9 @@
 import asyncio
-import logging
 import os
 
 import pytest
 
 import httpx
-from httpx import utils
 from httpx.utils import (
     ElapsedTimer,
     get_ca_bundle_from_env,
@@ -16,6 +14,7 @@ from httpx.utils import (
     parse_header_links,
     should_not_be_proxied,
 )
+from tests.utils import override_log_level
 
 
 @pytest.mark.parametrize(
@@ -103,24 +102,23 @@ def test_parse_header_links(value, expected):
 
 
 @pytest.mark.asyncio
-@pytest.mark.parametrize("httpx_log_level", ["trace", "debug"])
-async def test_httpx_log_level_enabled_stderr_logging(server, capsys, httpx_log_level):
-    os.environ["HTTPX_LOG_LEVEL"] = httpx_log_level
+async def test_logs_debug(server, capsys):
+    with override_log_level("debug"):
+        async with httpx.AsyncClient() as client:
+            response = await client.get(server.url)
+            assert response.status_code == 200
+    stderr = capsys.readouterr().err
+    assert "httpx.dispatch.connection_pool" not in stderr
 
-    # Force a reload on the logging handlers
-    utils._LOGGER_INITIALIZED = False
-    utils.get_logger("httpx")
 
-    async with httpx.AsyncClient() as client:
-        await client.get(server.url)
-
-    if httpx_log_level == "trace":
-        assert "httpx.dispatch.connection_pool" in capsys.readouterr().err
-    else:
-        assert "httpx.dispatch.connection_pool" not in capsys.readouterr().err
-
-    # Reset the logger so we don't have verbose output in all unit tests
-    logging.getLogger("httpx").handlers = []
+@pytest.mark.asyncio
+async def test_logs_trace(server, capsys):
+    with override_log_level("trace"):
+        async with httpx.AsyncClient() as client:
+            response = await client.get(server.url)
+            assert response.status_code == 200
+    stderr = capsys.readouterr().err
+    assert "httpx.dispatch.connection_pool" in stderr
 
 
 def test_get_ssl_cert_file():
diff --git a/tests/utils.py b/tests/utils.py
new file mode 100644 (file)
index 0000000..5084022
--- /dev/null
@@ -0,0 +1,20 @@
+import contextlib
+import logging
+import os
+
+from httpx import utils
+
+
+@contextlib.contextmanager
+def override_log_level(log_level: str):
+    os.environ["HTTPX_LOG_LEVEL"] = log_level
+
+    # Force a reload on the logging handlers
+    utils._LOGGER_INITIALIZED = False
+    utils.get_logger("httpx")
+
+    try:
+        yield
+    finally:
+        # Reset the logger so we don't have verbose output in all unit tests
+        logging.getLogger("httpx").handlers = []