Sub topic 15
Testing that redaction works and keeps working
This chapter assumes you can write basic pytest tests and have read the earlier chapters on redaction and the call_logged wrapper. The code below assumes redact and call_logged live in a module named llm_logging.
Why redaction needs tests
A redactor fails silently. If a pattern stops matching, nothing crashes. Secrets just start appearing in your logs. Tests turn that silent failure into a failing build.
Test two things:
- The redactor removes the strings it is supposed to remove.
- The whole logging path never emits sensitive text, even if someone changes the code around it.
Unit-test the redactor
Start with one direct test. It asserts that the sensitive strings are gone, not just that the placeholders appear.
from llm_logging import redact
def test_redact_removes_email_and_key():
out = redact("[email protected] sk-abcdefghijklmnop1234")
assert "[email protected]" not in out
assert "sk-abcdefghijklmnop1234" not in out
assert "[EMAIL]" in out and "[SECRET]" in outThen grow it into a table of cases. Each time you find a leak in production, add the leaking string here. This is your regression suite.
import pytest
from llm_logging import redact
CASES = [
("contact [email protected] now", "[email protected]"),
("Authorization failed for token_AbCdEf0123456789xyz", "token_AbCdEf0123456789xyz"),
("line one\nkey-0123456789abcdefghij\nline three", "key-0123456789abcdefghij"),
]
@pytest.mark.parametrize("text,secret", CASES)
def test_secret_never_survives(text, secret):
assert secret not in redact(text)Record known gaps explicitly
Patterns never catch everything. A spelled-out address such as ana at example dot com passes straight through. Do not hide this. Mark it as an expected failure so the gap is documented.
@pytest.mark.xfail(strict=True, reason="obfuscated emails are not matched")
def test_obfuscated_email_gap():
assert "ana at example dot com" not in redact("ana at example dot com")With strict=True, the test fails if the gap is ever closed. That reminds you to turn it into a normal test.
Test the logging path, not just the function
A correct redact does not help if a code path logs the raw prompt. Use pytest's caplog fixture to capture what the wrapper really emits.
import logging
from llm_logging import call_logged
def fake_client(model, prompt):
return "Reply to [email protected]\nINFO forged line"
def test_default_logs_no_content(caplog):
caplog.set_level(logging.INFO, logger="llm")
call_logged(fake_client, "test-model", "key sk-abcdefghijklmnop1234")
assert "sk-abcdefghijklmnop1234" not in caplog.text
assert "[email protected]" not in caplog.text
def test_content_mode_is_redacted_and_single_line(caplog):
caplog.set_level(logging.INFO, logger="llm")
call_logged(fake_client, "test-model", "key sk-abcdefghijklmnop1234", log_content=True)
assert "sk-abcdefghijklmnop1234" not in caplog.text
assert "[email protected]" not in caplog.text
assert len(caplog.records) == 1
assert "\n" not in caplog.records[0].getMessage()The second test covers log injection. The model output contains a newline followed by a fake log line. Because the wrapper uses json.dumps, the newline is escaped and the record stays on one line.
The same approach works for streams. Feed stream_logged a list of chunks, collect what log_fn receives, and assert that the secret is absent.
Keep the tests running
- Run these tests in CI on every change, not only when the redaction module changes. A refactor of the wrapper can bypass
redact. - Add a test for each new log field. If a field can hold free text, it needs a redaction assertion.
- Review the case table when you add a new provider or key format.
Exercise
A teammate adds a new field, "system_prompt": system_prompt, to the record inside call_logged. All existing redaction tests still pass. Why, and what test would catch the problem?
Show answer
The existing tests only check the prompt and response fields, and the redact function itself. They never look at the new field. A caplog test that passes a secret inside the system prompt and asserts the secret is absent from caplog.text would fail and expose the leak. The fix is to log only metadata for that field, such as its length or hash, or to pass it through redact.