We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
Make `capture_log` assertions test-specific
almirsarajcic
Do not assert on generic text in logs captured by async tests.
log = capture_log(fn -> Billing.sync(invoice.id) end)
assert log =~ "failed"
refute log =~ "unexpected response"
ExUnit.CaptureLog warns that a test using async: true can capture messages from other tests. That makes both assertions timing-dependent: the positive assertion can pass because an unrelated test logged "failed", while the negative assertion can fail because another process logged "unexpected response".
Put a stable, operation-specific marker in the message and match that identifying fragment:
Logger.error(
"billing_sync invoice=#{invoice.id} failed: #{inspect(reason)}"
)
assert log =~ "billing_sync invoice=#{invoice.id} failed:"
The partial match still ignores timestamps and formatter details, but it no longer matches every failure in the concurrent suite. Use a resource identifier that is unique to the test when concurrent tests can exercise the same operation.
If correctness requires proving that no matching log occurred anywhere, or the message cannot be made test-specific, move that test module out of the async pool instead of treating capture_log/2 as process isolation.
copied to clipboard