<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>code-diesel &#187; phpunit</title>
	<atom:link href="http://www.codediesel.com/tag/phpunit/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Test dependencies in PHPUnit 3.4</title>
		<link>http://www.codediesel.com/testing/test-dependencies-in-phpunit/</link>
		<comments>http://www.codediesel.com/testing/test-dependencies-in-phpunit/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 08:54:33 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[testing]]></category>
		<category><![CDATA[phpunit]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/sql/test-dependencies-in-phpunit/</guid>
		<description><![CDATA[writing dependent tests in phpunit 3.4]]></description>
			<content:encoded><![CDATA[<p><a target="_blank" href="http://www.phpunit.de/">PHPUnit</a> has become the de-facto standard for unit testing PHP code. Now in version 3.4, it has added many new and interesting features to its repertoire.</p>
<h4>Dependencies in PHPUnit tests</h4>
<p>PHPUnit 3.4 now supports dependencies between different test methods. It allows you to execute a particular test <em>ONLY IF</em> the test that it depends on executes successfully. Take the following example (Listing 1.) where we test a <a href="http://www.codediesel.com/php/linked-list-in-php/">linked-list class</a> I developed earlier.<br />
<span id="more-2217"></span></p>
<h4>A simple unit-test with PHPUnit</h4>
<p>The test basically creates a link-list with 100 nodes, and tests a few class methods &#8211; reverseList, deleteFirstNode, deleteLastNode. As you can see the linklist creation part and method tests are combined in the single test method <em>testLinkList()</em>. </p>
<p>The primary focus here is on testing the three class functions. There is nothing wrong with this. But what if the linklist creation part itself fails before the other tests are conducted? Of course we can add another test to check that the links are being created correctly, but that mixes up the two parts.</p>
<p><b>Listing 1.</b></p>

<div class="wp_codebox"><table><tr id="p22171"><td class="code" id="p2217code1"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'linklist.class.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'PHPUnit/Framework.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> LinkListTest <span style="color: #000000; font-weight: bold;">extends</span> PHPUnit_Framework_TestCase
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/* How many nodes to create */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_nNodes</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testLinkList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #009933; font-style: italic;">/* Linklist creation block */</span>
        <span style="color: #000088;">$theList</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes<span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>  
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insertLast</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
        <span style="color: #009933; font-style: italic;">/* 
            Linklist methods testing block :
&nbsp;
            $theList-&gt;totalNodes() is a public function 
            in the 'linklist' class that returns the total
            number of nodes in the link-list.
        */</span>
&nbsp;
        <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reverseList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes<span style="color: #339933;">,</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">totalNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteFirstNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">totalNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteLastNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">totalNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Splitting up tests</h4>
<p>A better way is to split the linklist creation part and class method testing part into two separate test functions as below (Listing 2). Here the linklist is created in the &#8216;testCreateList()&#8217; method and the testing of the class methods is done in the &#8216;testLinkList()&#8217; method. This allows us to keep the two testing blocks separate.</p>
<p><b>Listing 2.</b></p>

<div class="wp_codebox"><table><tr id="p22172"><td class="code" id="p2217code2"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'linklist.class.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'PHPUnit/Framework.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> LinkListTest <span style="color: #000000; font-weight: bold;">extends</span> PHPUnit_Framework_TestCase
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/* How many nodes to create */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_nNodes</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testCreateList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$theList</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes<span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insertLast</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertTrue</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theList</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @depends testCreateList
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testLinkList<span style="color: #009900;">&#40;</span><span style="color: #000088;">$theList</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reverseList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes<span style="color: #339933;">,</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">totalNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteFirstNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">totalNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteLastNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">totalNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The important point to note here is that we have specified that the &#8216;<em>testLinkList</em>&#8216; method depends on the &#8216;<em>testCreateList</em>&#8216; method, with a @depends annotation. This tells PHPUnit that if the &#8216;<em>testCreateList</em>&#8216;  test fails then the test dependent on it, &#8216;<em>testLinkList</em>&#8216;, will have to be skipped, which is a logical way to go.</p>

<div class="wp_codebox"><table><tr id="p22173"><td class="code" id="p2217code3"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @depends testCreateList
 */</span></pre></td></tr></table></div>

<h4>Producers &#038; Consumers in a Test</h4>
<p>First some definitions:</p>
<blockquote><p>
a. A producer is a test method that yields its unit under test as return value.<br />
b. A consumer is a test method that depends on one or more producers and their return values.
</p></blockquote>
<p>What it means is that a &#8216;Producer&#8217; is a test method that returns some data that will be used by a &#8216;Consumer&#8217; test method. In our example the &#8216;<em>testCreateList</em>&#8216; is a Producer and the &#8216;<em>testLinkList</em>&#8216; is the Consumer.  The &#8216;<em>testCreateList</em>&#8216; method returns a linklist object which is then received by the dependent method &#8216;<em>testLinkList</em>&#8216;. So if for some reason the &#8216;<em>testCreateList</em>&#8216; fails to return some data then the test that depends on it,  &#8216;<em>testLinkList</em>&#8216;, will have to be skipped, as it has nothing to work with.</p>
<p>Bear in mind that it does not mean that every test method that depends on some other method has to accept some arguments from the method it is dependent on. Lets breakup the above example further (Listing 3). Here we have added one more test method, &#8216;<em>testNodesDefined</em>&#8216;, which makes sure that the &#8216;$_nNodes&#8217; variable is not null. The &#8216;<em>testCreateList</em>&#8216; method is dependent on this method, so that if the &#8216;$_nNodes&#8217; is null then the &#8216;<em>testCreateList</em>&#8216; is skipped, which causes the test &#8216;<em>testLinkList</em>&#8216; to be skipped.</p>
<p><b>Listing 3.</b></p>

<div class="wp_codebox"><table><tr id="p22174"><td class="code" id="p2217code4"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'linklist.class.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'PHPUnit/Framework.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> LinkListTest <span style="color: #000000; font-weight: bold;">extends</span> PHPUnit_Framework_TestCase
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/* How many nodes to create */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_nNodes</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testNodesDefined<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertNotNull</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @depends testNodesDefined
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testCreateList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$theList</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes<span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insertLast</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertTrue</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theList</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @depends testCreateList
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testLinkList<span style="color: #009900;">&#40;</span><span style="color: #000088;">$theList</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reverseList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes<span style="color: #339933;">,</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">totalNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteFirstNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">totalNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteLastNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">totalNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p> A output from a failed test, when we set  &#8216;$_nNodes&#8217; to null, is shown below.<br />
<b>Listing 4.</b></p>

<div class="wp_codebox"><table><tr id="p22175"><td class="code" id="p2217code5"><pre class="text" style="font-family:monospace;">D:\localhost\datastructures&gt;phpunit --verbose LinkListTest
PHPUnit 3.4.2 by Sebastian Bergmann.
&nbsp;
LinkListTest
FSS
&nbsp;
Time: 0 seconds
&nbsp;
There was 1 failure:
&nbsp;
1) LinkListTest::testNodesDefined
Failed asserting that &lt;null&gt; is not null.
&nbsp;
D:\localhost\test\datastructures\LinkListTest.php:13
&nbsp;
There were 2 skipped tests:
&nbsp;
1) LinkListTest::testCreateList
This test depends on &quot;LinkListTest::testNodesDefined&quot; to pass.
&nbsp;
&nbsp;
2) LinkListTest::testLinkList
This test depends on &quot;LinkListTest::testCreateList&quot; to pass.
&nbsp;
&nbsp;
FAILURES!
Tests: 1, Assertions: 2, Failures: 1, Skipped: 2.
&nbsp;
D:\localhost\test\datastructures&gt;</pre></td></tr></table></div>

<blockquote><p>
Note: The tests are executed in the sequence in which you wrote them. So take care while writing dependent tests. For example, sequencing test methods as below will not work.
</p></blockquote>

<div class="wp_codebox"><table><tr id="p22176"><td class="code" id="p2217code6"><pre class="php" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testNodesDefined<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertNotNull</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @depends testCreateList
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testLinkList<span style="color: #009900;">&#40;</span><span style="color: #000088;">$theList</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #339933;">..</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @depends testNodesDefined
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testCreateList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #339933;">..</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h4>What all of this has brought us</h4>
<p>Testing a complex class in a single humongous test method is not wrong, but can create a confusing mess of &#8216;asserts&#8217;. Splitting a single test method into multiple dependent tests can help you segregate your test methods by functionality which in turn will get you to quickly localize defects in your code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/testing/test-dependencies-in-phpunit/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

