Automated tests
Assertion messages
The default assertion message provided by PHPUnit is sufficient for most situations. The page explains when a custom assertion message can be added and the format of the message.
When to add a custom assertion message
- When the assertion in inside a loop.
- In a custom assertion method in a trait or base test class. A custom assertion method begins with
public function assert*();
. - In a helper method.
Format of a custom assertion message
%subject% should %verb% %payload%
Definitions
%subject%
The additional context information
%verb%
The action.
%payload%
The value the%subject% should be.
Example
foreach ($expected_steps as $element_id => $step_value) {
foreach (['s', 'i', 'h', 'd', 'm', 'y'] as $sub_field) {
$name = $element_id . '[' . $sub_field . ']';
$expected_step = $step_value[$sub_field] ?? 1;
$input = $this->xpath("//form//input[@name='$name']");
$this->assertCount(1, $input, "Duration input $name should appear exactly once.");
$actual_step = (integer) $input[0]->attributes()->{'step'};
$this->assertEquals($expected_step, $actual_step, "Duration input $name should have the correct step value.");
}
}
where
{subject} == "Duration input $name"
should == 'should' ;)
{verb} == 'have'
{payload} == 'the correct step value'