Over a million developers have joined DZone.

Practical PHP Testing Patterns: Assertion Method

· Web Dev Zone

Easily build powerful user management, authentication, and authorization into your web and mobile applications. Download this Forrester report on the new landscape of Customer Identity and Access Management, brought to you in partnership with Stormpath.

A key requirement in writing fully automated tests is that they should be self-validating.

The self-validating term means that the output should be a red light or a green light for each test: they should not require any human intervention to evaluate the result.

Self-validation allows the composition of large test suite from many simple test cases, and it "permits" an automated testing system to reach the scale of thousand of test. Without self-validating test, you're not really automating your tests into something that Continuous Integration could run.
Assertions are atomic units that:

  • when satisfied, leave the test in a green state.
  • When not satisfied, interrupt the test and mark it as failed (red bar).
Each assertion is commonly implemented as method, which takes as parameters the values that decide its outcome. The method:
  • when satisfied, does nothing apart incrementing an assertion counter.
  • When not satisfied, throws an exception that bubbles up to the xUnit test runner that marks the test as failed.

This behavior means that once an assertion fails, no subsequent assertion in the same test method will be checked. In a sense, this is different from expectations mechanisms like Selenium's one, which when failing only take note of the problem and let the test method go on. Selenium supports both assertions and expectations.

If you want different assertions to be executed regardless of their failure state, put them in different tests. Different test methods are isolated from each other, while different assertions in the same test can be thought of as a "ascending scale", where making each assertion requires all the previous ones to be worth trying:

$this->assertTrue($result instanceof Iterator);
$this->assertEquals($result->key(), 0);

Assertions can be divided into standard ones, provided by the PHPUnit_Framework_TestCase superclass and custom ones. We will see only standard ones in this article, since customization is covered by another pattern.

Examples of PHPUnit's assertions comprehends assertTrue(), assertEquals(), or assertContains(). There are many more assertions natively available, and you can take a look at my PHPUnit Refcard to discover the most important ones.

Categories

These are the categories into which assertions can be divided, according to the xUnit patterns book, which is the authority literature on the subject.

  • Single-Outcome Assertions are methods with no arguments, which behave always the same way when called. $this->fail() for example, which is the only assertions which does not start with assert*() in PHPUnit's Api.
  • Stated Outcome Assertions take a single argument, which is checked for a condition stated in the method name. For example, assertTrue() and assertFalse() fall in this category.
  • Expected Exception Assertions evaluate a block of code and check that it raises and exception. There are no methods related to this category in PHPUnit standard set, but the exception checking is provided via annotations.
  • Equality Assertions compare two parameters: assertEquals() and assertSame() are common examples.
  • Fuzzy Equality Assertions compare two parameters with a tolerance that must be higher than the difference between them. assertEquals(), when used with doubles, accepts a tolerance fourth parameter (the third is an error message.)

Example

The code sample is a test with many failing assertions. Assertions do nothing when they pass (leaving the test, which was green when they were called, still green) so it make sense to explain them while they're failing.

<?php

class AssertionsTest extends PHPUnit_Framework_TestCase
{
/**
* Single-Outcome Assertion: for example, always fails
*/
public function testNeverPasses()
{
$this->fail();
}

/**
* Stated Outcome Assertion: fail if the argument is not true
*/
public function testAssertTrue()
{
$this->assertTrue(false);
}

/**
* Equality Assertion: fail if the arguments does not satisfy some equivalence relation
*/
public function testAssertEquals()
{
$this->assertEquals(3, 42);
}

/**
* Equality Assertion: fail if the arguments are not identical
*/
public function testAssertSame()
{
$this->assertSame(3, "3");
}

/**
* Expected Exception Assertions are implemented via an annotation
* instead of methods due to the difficulty of defining a code block in PHP.
* @expectedException InvalidArgumentException
*/
public function testDoesNotThrowAnExceptionAsWeWant()
{
$positiveValues = array();
$positiveValues[] = -1;
}

public function testFuzzyEqualityAssertion()
{
$this->assertEquals(5.01, 5.00, '', 0.001);
}
}

The Web Dev Zone is brought to you by Stormpath—offering a complete, pre-built User Management API for building web and mobile applications, and APIs. Download our new whitepaper: "Build Versus Buy: Customer Identity Management for Web and Mobile Applications".

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}