Using Test Fixtures in PHPUnit

Exclusive offer: get 50% off this eBook here
Instant Hands-on Testing with PHPUnit How-to [Instant]

Instant Hands-on Testing with PHPUnit How-to [Instant] — Save 50%

A practical guide to getting started with PHPUnit to improve code quality with this book and ebook

$14.99    $7.50
by Michael Lively | August 2013 | Open Source PHP

In this article by Micheal Lively, the author of Instant Hands-on Testing with PHPUnit How-to [Instant], we will go into detail of how these fixtures work and what types of things you can do with them.

As you begin writing tests you'll find that many of them, especially ones inside the same test case class, need to run the same code to set up the object that you are running tests against. This code is part of what is commonly called a fixture. Many test methods require the same fixture. PHPUnit allows you to support shared fixtures using the setUp()and tearDown() methods.

(For more resources related to this topic, see here.)

How to do it...

Open tests/CardTest.php and add a new setUp() method and use the $cardproperty to hold the Cardfixture.

class CardTest extends PHPUnit_Framework_TestCase
private $card;
public function setUp()
$this->card = new Card('4', 'spades');
public function testGetNumber()
$actualNumber = $this->card->getNumber();
$this->assertEquals(4, $actualNumber, 'Number should be <4>');
public function testGetSuit()
$actualSuit = $this->card->getSuit();
$this->assertEquals('spades', $actualSuit, 'Suit should be

public function testIsInMatchingSet()
$matchingCard = new Card('4', 'hearts');
'<4 of Spades> should match <4 of Hearts>');
public function testIsNotInMatchingSet()
$matchingCard = new Card('5', 'hearts');
'<4 of Spades> should not match <5 of Hearts>');

How it works...

You'll notice the biggest change in this method is the addition of the setUp() method. The setUp() method is run immediately before any test method in the test case. So when testGetNumber() is run, the PHPUnit framework will first execute setUp() on the same object. setUp() then initializes $this|card with a new Cardobject. $this|card is then used in the test to validate that the number is returned properly. Using setUp()in this way makes your tests much easier to maintain. If the signature of the Cardclass's constructor is changed, you will only have one place in this file to reflect that change as opposed to four separate places. You will save even more time as you add more and more tests to a single test case class.

It should also be noted that a new instance of CardTest is created each time a test method is executed. Only the code in this case is being shared. The objects that setUp() creates are not shared across tests. We will talk about how to share resources across tests shortly.

There is also a tearDown() method. It can be used to remove any resource you created inside your setUp() method. If you find yourself opening files, or sockets, or setting up other resources then you will need to use tearDown()to close those resources, delete file contents, or otherwise tear down your resources. This becomes very important to help keep your test suite from consuming too many resources. There is nothing quite like running out of inodes when you are running a large test suite!

There's more...

As we mentioned a moment ago, PHPUnit has the facility to share resources across execution of multiple tests. This is generally considered bad practice. One of the primary rules of creating tests is that tests should be independent from each other so that you can isolate and locate the code causing test failures more easily.

However, there are times when the physical resources required to create a fixture become large enough to outweigh the negatives of sharing this fixture across multiple tests. When such cases arise PHPUnit provides two methods that you can override: setUpBeforeClass() and tearDownAfterClass(). These are expected to be static methods. setUpBeforeClass() will be called prior to any tests or setUp() calls being made on a given class. tearDownAfterClass() will be called once all tests have been run and the final tearDown() call has been made. If you override these methods to create new objects or resources you would need to make sure that you set these values on static members of the test case class. Also, even if you are dealing only with objects, the tearDownAfterClass() is incredibly important to implement. If you do not implement it then any object created in setUpBeforeClass() and saved to static variables will remain in memory until all tests in your test suite have run.


In this way, you can use shared fixtures to reduce code duplication and to reduce the code necessary to set up new tests.

Resources for Article:

Further resources on this subject:

Instant Hands-on Testing with PHPUnit How-to [Instant] A practical guide to getting started with PHPUnit to improve code quality with this book and ebook
Published: May 2013
eBook Price: $14.99
See more
Select your format and quantity:

About the Author :

Michael Lively

Michael Lively has worked in a variety of roles in the software development industry for 12 years, from developer to architect and now a software development director. He has worked on a variety of different projects and a variety of different technologies over that time, from small home-grown sites serving a handful of regular users to large enterprise platforms serving millions of consumers. In addition to his professional work, he has been an active member of the PHP open source community. Some of his contributions include the database extension for PHPUnit and more recently an alternative mocking framework for PHP called Phake.

Books From Packt

Expert PHP 5 Tools
Expert PHP 5 Tools

JavaScript Unit Testing
JavaScript Unit Testing

Instant Eclipse Application Testing How-to [Instant]
Instant Eclipse Application Testing How-to [Instant]

Object-Oriented Programming with PHP5
Object-Oriented Programming with PHP5

Magento 1.4 Development Cookbook
Magento 1.4 Development Cookbook

Using Node.js for UI Testing
Using Node.js for UI Testing

CouchDB and PHP Web Development Beginner's Guide
CouchDB and PHP Web Development Beginner's Guide

 PHP 5 E-commerce Development
PHP 5 E-commerce Development

No votes yet

Post new comment

This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software