Skip to main content

Code reviews rule: Missing comment in test assertions

Written by David Martin
Updated this week

Missing comment in test assertions

Why is this an issue?

Assertions without messages make test failures harder to diagnose. When a test fails, developers must:

  • Find the failing assertion in the code

  • Understand what was being tested

  • Determine what went wrong

A descriptive message immediately explains what the assertion was checking.

Examples

Example of incorrect code:

Assert.areEqual(expected, actual);
Assert.isTrue(result.isSuccess());

Example of correct code:

Assert.areEqual(expected, actual, 'Account name should match input');
Assert.isTrue(result.isSuccess(), 'DML operation should succeed without errors');

How can I fix violations?

An autofix exists for this violation.

Alternatively you can add a message parameter to all assertions which:

  • Describe what the assertion is checking

  • Include relevant context (e.g., the scenario being tested)

  • Be specific about expected vs actual values if not obvious

When should I disable this rule?

You may dismiss specific violations for simple assertions where the context is obvious from surrounding code.

Resources

Did this answer your question?