fix(test): use capsys instead of caplog for structlog output
TestLogGpuMemory tests used pytest's caplog fixture to capture log output, but log_gpu_memory() uses structlog which writes to stdout, not Python's standard logging module. caplog only captures standard logging messages, so assertions always failed on empty text. Switch to capsys which captures stdout directly.
This commit is contained in:
parent
4b057bc84e
commit
c8c371610b
1 changed files with 12 additions and 20 deletions
|
|
@ -285,7 +285,7 @@ class TestLogGpuMemory:
|
|||
def test_does_not_raise(self):
|
||||
log_gpu_memory("test")
|
||||
|
||||
def test_logs_gpu_info_when_available(self, caplog):
|
||||
def test_logs_gpu_info_when_available(self, capsys):
|
||||
fake_info = {
|
||||
"available": True,
|
||||
"backend": "cuda",
|
||||
|
|
@ -295,35 +295,27 @@ class TestLogGpuMemory:
|
|||
"utilization_pct": 12.5,
|
||||
"free_gb": 14.0,
|
||||
}
|
||||
import structlog
|
||||
from loggers import get_logger
|
||||
|
||||
with (
|
||||
patch(
|
||||
"utils.hardware.hardware.get_gpu_memory_info", return_value = fake_info
|
||||
),
|
||||
caplog.at_level(logging.INFO, logger = "utils.hardware.hardware"),
|
||||
with patch(
|
||||
"utils.hardware.hardware.get_gpu_memory_info", return_value = fake_info
|
||||
):
|
||||
log_gpu_memory("unit-test")
|
||||
|
||||
assert "unit-test" in caplog.text
|
||||
assert "CUDA" in caplog.text
|
||||
assert "FakeGPU" in caplog.text
|
||||
captured = capsys.readouterr().out
|
||||
assert "unit-test" in captured
|
||||
assert "CUDA" in captured
|
||||
assert "FakeGPU" in captured
|
||||
|
||||
def test_logs_cpu_fallback_when_no_gpu(self, caplog):
|
||||
def test_logs_cpu_fallback_when_no_gpu(self, capsys):
|
||||
fake_info = {"available": False, "backend": "cpu"}
|
||||
import structlog
|
||||
from loggers import get_logger
|
||||
|
||||
with (
|
||||
patch(
|
||||
"utils.hardware.hardware.get_gpu_memory_info", return_value = fake_info
|
||||
),
|
||||
caplog.at_level(logging.INFO, logger = "utils.hardware.hardware"),
|
||||
with patch(
|
||||
"utils.hardware.hardware.get_gpu_memory_info", return_value = fake_info
|
||||
):
|
||||
log_gpu_memory("cpu-test")
|
||||
|
||||
assert "No GPU available" in caplog.text
|
||||
captured = capsys.readouterr().out
|
||||
assert "No GPU available" in captured
|
||||
|
||||
|
||||
# ========== format_error_message() ==========
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue