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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Practical PHP Testing Patterns: Unfinished Test Assertion

Practical PHP Testing Patterns: Unfinished Test Assertion

Giorgio Sironi user avatar by
Giorgio Sironi
·
Feb. 14, 11 · Interview
Like (0)
Save
Tweet
Share
767 Views

Join the DZone community and get the full member experience.

Join For Free

Sometimes we are in the middle of writing a test and another possible, unrelated test comes to mind. Now we are in a tough situation: Test-Driven Development tells us to go one step at a time for good reasons. But if we just wait before inserting that test, we can forget it even if it is a nasty corner case.

We need an approach for storing these tests somewhere, hoping that in the future they could be implemented (or discarded upon deeper analysis).

What we want

We don't want to write the test on a Post-it note, as it will be probably forgotten just like in our own brain RAM. A nice thing would be to keep this new test in source control, even if it will fail for now.

If we are going to introduce it in a Testcase Class, we don't want side effects on already existing tests (no explosion of the suite or compilation errors).
A first approximation of the solution can be an empty Test Method:

public function testThatXDoesY() {}

or a slightly more sophisticated:

public function testThatXDoesY()
{
//call method Z and check W is equal to K
}

However, we want to be told that this test is not green. An empty test is a false positive: we see a dot in the output, and we could forget to go over that test again to actually write some code in it.

Thus another approximation may be just throwing an exception to make the test fail:

public function testThatXDoesY()
{
throw new Exception('Write me!');
}

But often we want to distinguish between tests that we still have to write and regressions. In that case, we want a clear message that tells us that this test is still incomplete (not failing: just incomplete) and what we can do to fix it.

In all these scenarios, we can introduce an Unfinished Test Assertion, which is an assertion that always fails and provide a meaningful message for the failure. Some of these assertions fail in special ways (see the Implementation in PHPUnit section.)

It's also common sense to include Unfinished Test Assertions when tests are generated (una tantum) by some other process, and implemented one at the time by developers. Even when these tests are spreaded in your suite, you can always track them by running it as a whole.

Implementation in PHPUnit

Basically, inside a PHPUnit_Framework_TestCase you have three methods to execute an Unfinished Test Assertion:

  • $this->fail('message') displays a F instead of a . in the results, and implies a red bar. It's just a semantic way to execute $this->assertTrue(false).
  • $this->markTestSkipped('message') displays an S in the results, and implies a yellow bar instead of a green one.
  • $this->markTestIncomplete('message') displays an I in the results, and also implies a yellow bar.

Of course you can wrap these basic methods with yours.

The difference between skipped and incomplete tests is only semantic, not functional: incomplete tests lack code, skipped are complete but cannot be run in the current environment or due to some other test already failing.

The difference between $this->fail() and the other methods is instead clear: even one failed test results in a red bar, while skipped and incomplete tests make the bar yellow, which is a less serious issue.

Example

I have included a sample Testcase Class which shows usage of the various predefined methods, plus a custom one.

If you want to actually see the messages of skipped and incomplete tests, you must run phpunit --verbose instead of simply phpunit.

The code is also available on the Github repository for this series: https://github.com/giorgiosironi/practical-php-testing-patterns.

<?php
class UnfinishedTestAssertions extends PHPUnit_Framework_TestCase
{
public function testThatFails()
{
$this->fail('Write this test, you should check that X does Y.');
}

public function testThatIsSkipped()
{
$this->markTestSkipped('To execute this test, you need an active Internet connection.');
}

public function testThatIsIncomplete()
{
$fixture = new ArrayObject();
$fixture['key'] = 'value';

$this->markTestIncomplete('This test needs to be finished: the act and assert parts are missing.');
}

public function testThatIsDeclaredIncompleteWithCustomUnifinishedTestAssertion()
{
$this->assertionWillBeNeeded();
}

private function assertionWillBeNeeded()
{
$this->markTestIncomplete('The test lacks an assert phase. Write it.');
}
}
Test assertion PHP

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DZone's Article Submission Guidelines
  • 11 Observability Tools You Should Know
  • Spring Boot vs Eclipse Micro Profile: Resident Set Size (RSS) and Time to First Request (TFR) Comparative
  • Unlocking the Power of Elasticsearch: A Comprehensive Guide to Complex Search Use Cases

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: