DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Fluent Assertions Reloaded
  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • Solid Testing Strategies for Salesforce Releases

Trending

  • Artificial Intelligence, Real Consequences: Balancing Good vs Evil AI [Infographic]
  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • From Zero to Production: Best Practices for Scaling LLMs in the Enterprise
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  1. DZone
  2. Coding
  3. Languages
  4. Practical PHP Testing Patterns: Assertion Message

Practical PHP Testing Patterns: Assertion Message

By 
Giorgio Sironi user avatar
Giorgio Sironi
·
Dec. 06, 10 · Interview
Likes (0)
Comment
Save
Tweet
Share
6.6K Views

Join the DZone community and get the full member experience.

Join For Free

In the previous article of this series, we saw how assertions are the key to self-validating tests. Even if an assertion's result is only a pass/fail value, programmers and testers find very handy to associate to the fail value some kind of additional information about the failure, to allow easier debugging without insertion of var_dump() and similar statements into the code.

As I showed you in the previous article's code sample, assertions are useful the most when they fail, and in case they do, they should explain what happened so that a correction can be made quickly. An assertion that passes does not do anything; an assertion that fails shows an error.

Whata happens when tests are failing

The typical scenario is that of a large test suite, with hundreds or thousands of tests. At the time of a commit to the source code repository (or of a push if we're talking about git), the whole test suite must be green. Even if only one tests is red, the test suite is regarded as red.

Sometimes, a regression is introduced by a programming error or an unforeseen scenario, and the output of the test suite becomes something like this:

[12:30:02][giorgio@Desmond:~/txt/articles]$ phpunit assertionmessage.php 
PHPUnit 3.5.5 by Sebastian Bergmann.

FFFF

Time: 0 seconds, Memory: 3.75Mb

There were 4 failures:

1) AssertionMessagesTest::testAsserTrue
The object is not an Iterator.
Failed asserting that <boolean:false> is true.

/home/giorgio/Dropbox/txt/articles/assertionmessage.php:9

2) AssertionMessagesTest::testAssertEquals
The square root of 17 is not 4 but 4.1231056256177.
Failed asserting that <double:4.1231056256177> matches expected <integer:4>.

/home/giorgio/Dropbox/txt/articles/assertionmessage.php:17

3) AssertionMessagesTest::testAssertContainsFailsWithCustomMessage
The array does not contain 4.
Failed asserting that an array contains <integer:4>.

/home/giorgio/Dropbox/txt/articles/assertionmessage.php:24

4) AssertionMessagesTest::testAssertContains
Failed asserting that an array contains <integer:4>.

/home/giorgio/Dropbox/txt/articles/assertionmessage.php:36

FAILURES!
Tests: 4, Assertions: 4, Failures: 4.

The green tests are not noisy (which would be a smell), since they are not interesting at the moment. The red tests, which our focus is on, are shown with all the associated information. Along with the test name, the line of interest, and the kind of problem (failure/error), the assertion message is shown.

How to write a meaningful message

An assertion message can be passed as an argument of the assertion method, to be used in case of failure. Writing good assertion messages is an art, but before diving into some code to show you some messages I wrote, we can see how assertion messages are categorized in literature:

  • Assertion-Identifying Message: includes information on which of the assertions in the same method caused the failure. This information can be for example the name of the checked variables. It's good to understand at a glance which assertion failed, but PHPUnit shows you the line of the test method that caused the failure, so it's not strictly necessary to include this kind of messages.
  • Expectation-Describing Message: tells us what should have happened, but did not according to the data passed to the assertion method.
  • Argument-Describing Message: when using assertions which are not very smart, like assertFalse() or assertTrue(), a failure would tell us by default only something like False was passed to assertTrue(). Adding an Argument-Describing Message will tell us instead The predicate on the set containing all red cars was false instead of true.

Watch the test fail

I can never say it enoug: when you write your tests, watch them fail (this mean also that each test should be written in advance with regard to the code it exercises, following the discipline of Test-Driven Development).

This practice is not only useful for avoiding false positives, which are rare anyway, but to show at least for once the assertion message. You can check that when the test fails, the error message is meaningful and the test does not cause a Fatal Error. Watch your test fail to ensure that when they'll fail due to a regression in the future, you will be pleased by how fast you'll learn what's wrong.

Example

The code sample shows how to use PHPUnit's assertion methods with the $message optional argument. Any time you use basic methods like assertTrue(), a message is mandatory. The need for $message is inversely proportional to the specificity of the assertion: for example when assertContains() fails, PHPUnit has already valuable information in the the method itself to produce a readable error message.

Note that the usage of "" (double quotes) enables fast composition of error messages via variable substitution.

<?php

class AssertionMessagesTest extends PHPUnit_Framework_TestCase
{
public function testAsserTrue()
{
$object = null;
// back in PHPUnit 3.4 where we did not have assertInstanceOf()
$this->assertTrue($object instanceof Iterator, 'The object is not an Iterator.');
}

public function testAssertEquals()
{
$expected = 4;
$square = 17;
$result = sqrt($square);
$this->assertEquals($expected, $result, "The square root of $square is not $expected but $result.");
}

public function testAssertContainsFailsWithCustomMessage()
{
$array = array(1, 2, 3);
$testElement = 4;
$this->assertContains($testElement, $array, "The array does not contain $testElement.");
}

/**
* This Assertion Message would be enough. It will even be better in some cases,
* since PHPUnit would display nicely $testElement even if it was not "castable"
* to a string (an object or array).
*/
public function testAssertContains()
{
$array = array(1, 2, 3);
$testElement = 4;
$this->assertContains($testElement, $array);
}
}
Assertion (software development) PHP Testing

Opinions expressed by DZone contributors are their own.

Related

  • Fluent Assertions Reloaded
  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • Solid Testing Strategies for Salesforce Releases

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!