Use precise, informative assertions in tests to provide clear feedback when tests fail and verify the correct behavior rather than implementation details.
Use precise, informative assertions in tests to provide clear feedback when tests fail and verify the correct behavior rather than implementation details.
Key practices:
assert
statements with proper test framework assertions that provide clear error messages.
// Instead of this:
assert valueCount == 1;
// Do this:
assertEquals(1, valueCount, "Multi-values make chunking more complex, and it's not a real case yet");
// Instead of this:
try {
createComponents("my_analyzer", analyzerSettings, testAnalysis.tokenizer, testAnalysis.charFilter, testAnalysis.tokenFilter);
fail("expected failure");
} catch (IllegalArgumentException e) {
// assertions
}
// Do this:
IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> createComponents("my_analyzer", analyzerSettings, testAnalysis.tokenizer, testAnalysis.charFilter, testAnalysis.tokenFilter)
);
// assertions on exception
// Instead of verifying exact implementation details:
verify(coordinatingIndexingPressure).increment(1, bytesUsed(doc0Source));
verify(coordinatingIndexingPressure).increment(1, bytesUsed(doc1Source));
// Verify the essential behavior:
verify(coordinatingIndexingPressure, times(6)).increment(eq(1), longThat(l -> l > 0));
Verify all relevant fields: Assert on all important aspects of the test output, not just a subset.
// Instead of hardcoded values:
String apiKeyId = "test-id";
// Use randomization:
String apiKeyId = randomAlphaOfLength(20);
greaterThanOrEqualTo()
instead of strict equality.Enter the URL of a public GitHub repository