<?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>Bill Shupp &#187; Unit Testing</title>
	<atom:link href="http://blog.shupp.org/category/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.shupp.org</link>
	<description>Software engineer, musician, space enthusiast</description>
	<lastBuildDate>Fri, 03 Feb 2012 22:00:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>More on step debugging in PHP</title>
		<link>http://blog.shupp.org/2011/04/18/more-on-step-debugging-in-php/</link>
		<comments>http://blog.shupp.org/2011/04/18/more-on-step-debugging-in-php/#comments</comments>
		<pubDate>Tue, 19 Apr 2011 06:43:27 +0000</pubDate>
		<dc:creator>shupp</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHPUnit]]></category>
		<category><![CDATA[Unit Testing]]></category>
		<category><![CDATA[VIM]]></category>
		<category><![CDATA[Xdebug]]></category>

		<guid isPermaLink="false">http://blog.shupp.org/?p=8170</guid>
		<description><![CDATA[Having recently wrote about PHP step debugging in VIM, I thought I'd share a couple of things I've come across since writing it. First, I find that I often need step debugging when writing unit tests, as that's when I'm really scrutinizing logic.  It turns out it's pretty easy to do.  All you need is [...]]]></description>
			<content:encoded><![CDATA[<p>Having recently wrote about <a href="http://blog.shupp.org/2011/02/12/php-step-debugging-in-vim/">PHP step debugging in VIM</a>, I thought I'd share a couple of things I've come across since writing it.</p>
<p>First, I find that I often need step debugging when writing unit tests, as that's when I'm really scrutinizing logic.  It turns out it's pretty easy to do.  All you need is to set the XDEBUG_CONFIG environment variable like so:</p>
<p><code>XDEBUG_CONFIG="idekey=session_name" php bin/phpunit.php tests/FooTest.php</code></p>
<p>When debugging from VIM, you'll want to have two shells open.  One running the debugger, and another executing the tests.  Also, note that in a shared environment, you can still pass in xdebug ini settings like so:</p>
<p><code>XDEBUG_CONFIG="idekey=session_name" \<br />
    php -d xdebug.remote_host=10.0.0.1 bin/phpunit.php tests/FooTest.php</code></p>
<p>Second, for step debugging in VIM, I've settled on <a title="VIM Plugin" href="http://www.vim.org/scripts/script.php?script_id=2508">this plugin</a> (there are many versions out there).  It seems to solve problems with tab handling that I was seeing with older versions.</p>
<p>Cheers,</p>
<p>Bill</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.shupp.org/2011/04/18/more-on-step-debugging-in-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Better code quality through easy access to tools</title>
		<link>http://blog.shupp.org/2010/09/20/better-code-quality-through-easy-access-to-tools/</link>
		<comments>http://blog.shupp.org/2010/09/20/better-code-quality-through-easy-access-to-tools/#comments</comments>
		<pubDate>Tue, 21 Sep 2010 03:39:43 +0000</pubDate>
		<dc:creator>shupp</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHPUnit]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://blog.shupp.org/?p=7891</guid>
		<description><![CDATA[(Reposted from the Empower Campaigns blog) When developing an application, one of the keys to maintaining high productivity and quality is to make sure that you not only have the right tool set, but that you have easy access to it.   With web applications, the usual needs are things like running Selenium tests, running unit [...]]]></description>
			<content:encoded><![CDATA[<p><em>(Reposted from the Empower Campaigns <a title="Original Empower Campaigns Blog" href="http://blog.empowercampaigns.com/post/1156820644/better-code-quality-through-easy-access-to-tools">blog</a>)</em></p>
<p>When developing an application, one of the keys to maintaining high  productivity and quality is to make sure that you not only have the  right tool set, but that you have easy access to it.   With web  applications, the usual needs are things like running <a title="Selenium" href="http://seleniumhq.org/">Selenium</a> tests, running unit tests and checking code coverage, profiling, and having links to documentation.  Though I've <a title="stated previously" href="http://blog.shupp.org/2010/02/21/pragmatic-tips-for-unit-testing-in-php/">talked previously</a> about the cost of maintaining 100% code coverage, I'm convinced that  it's worth it if you not only get proficient at the tools, but make them  very easy to use.  So when I started as the first developer here at <a title="Empower Campaigns" href="http://empowercampaigns.com">Empower Campaigns</a> a couple of months ago, I wanted to make sure these needs were addressed early on, before we had a large development team.<span id="more-7891"></span></p>
<p><img src="http://assets.tumblr.com/javascript/tiny_mce_3_3_3/plugins/pagebreak/img/trans.gif" alt="" />Inspired by the work that had been done at my previous job by <a title="Ian Eure" href="http://atomized.org">Ian Eure</a> and <a title="Matt Erkkila" href="http://matterkkila.com">Matt Erkkila</a>, I decided to start with the following:</p>
<ul>
<li>Use the tried and true <a title="Gnu Make" href="http://www.gnu.org/software/make/">GNU Make</a> for building environments and executing tools</li>
<li>Have a "build" virtual host in every development environment for accessing the tools' web resources</li>
</ul>
<p>Using make, getting our project "enterprise" (we use space shuttle themed project names) up and running is as simple as this:</p>
<pre># clone into a directory named with your vhost name:
git clone review:enterprise.git enterprise.bshupp.dev.em.power
cd enterprise.bshupp.dev.em.power
# install into apache
make clean install</pre>
<p>&nbsp;</p>
<p>This not only provides a working application (or "shuttle" as we call them) at <em>enterprise.bshupp.dev.em.power</em>, but also a "build" virtual host for access to web resources at <em>build.enterprise.bshupp.dev.em.power</em>.  Here's a screen shot of the initial build "dashboard", which provides  navigation on the left, and some environment details and instructions on  the main page.</p>
<p><a title="dashboard by empowercampaigns, on Flickr" href="http://www.flickr.com/photos/empowercampaigns/4999009123/"><img src="http://farm5.static.flickr.com/4152/4999009123_ed3c36fae3.jpg" alt="dashboard" width="500" height="248" /></a></p>
<p>Since our web tier is in PHP, the most common tool we need to run is <a title="PHPUnit" href="http://www.phpunit.de/">PHPUnit</a>, which you run via <em>make test</em>.   You'll see the standard output in your terminal like so (with some nice  colors indicating the status - green for success and high coverage,  yellow for success and medium coverage, and red for failing tests).</p>
<p><a title="unit_tests by empowercampaigns, on Flickr" href="http://www.flickr.com/photos/empowercampaigns/4999244672/"><img src="http://farm5.static.flickr.com/4091/4999244672_d93c859ae3.jpg" alt="unit_tests" width="500" height="279" /></a></p>
<p>In addition, to view coverage details, you go to the "code coverage" link your build dashboard.</p>
<p><a title="code_coverage by empowercampaigns, on Flickr" href="http://www.flickr.com/photos/empowercampaigns/4999637302/"><img src="http://farm5.static.flickr.com/4087/4999637302_6acc299784.jpg" alt="code_coverage" width="500" height="186" /></a></p>
<p><em>make test</em> also generates <a title="TestDox" href="http://agiledox.sourceforge.net/index.html">TestDox</a> output to see which tests are failing.</p>
<p><a title="testdox by empowercampaigns, on Flickr" href="http://www.flickr.com/photos/empowercampaigns/4999637424/"><img src="http://farm5.static.flickr.com/4089/4999637424_6db759ff4c.jpg" alt="testdox" width="492" height="500" /></a></p>
<p>We keep a Selenium Remote Control (RC) server running on our  development system for easy access by developers.  Want to see if you've  broken the selenium tests?  Just run <em>make selenium-smoke</em> or <em>make selenium-full</em>, and Selenium RC will talk to your development environment.</p>
<p>And since developers need to run <em>make test selenium-smoke phpcs</em> to validate before any push to avoid breaking a build, I've wrapped them up into <em>make validate</em> for convenience.  We actually use these shortcuts with <a title="Hudson" href="http://hudson-ci.org/">Hudson</a>, which allows us to direct Selenium RC to different staging environments, or modify build steps without re-configuring Hudson.</p>
<p>Want to read the inline documentation in a web browser instead of your text editor?  Just run <em>make phpdoc</em>, then click on the "PHPDocs" link in the build dashboard.</p>
<p><a title="phpdocs by empowercampaigns, on Flickr" href="http://www.flickr.com/photos/empowercampaigns/4999216461/"><img src="http://farm5.static.flickr.com/4112/4999216461_85707fb59b.jpg" alt="phpdocs" width="500" height="271" /></a></p>
<p>One thing that's particularly useful is easy profiling with Facebook's <a title="XHProf" href="http://mirror.facebook.net/facebook/xhprof/doc.html">XHProf</a>.  Just enable profiling in your environment via <em>make enable-profiling</em>,  perform the action in the application you want to profile, then open up  the "XHProf Profiler" link in your build dashboard.  There you'll see a  list of "runs" which you can drill down into and see exactly how each  is performing.  When you're done, just run <em>make disable-profiling</em>.</p>
<p><a title="xhprof by empowercampaigns, on Flickr" href="http://www.flickr.com/photos/empowercampaigns/4999050781/"><img src="http://farm5.static.flickr.com/4126/4999050781_b561d65a53.jpg" alt="xhprof" width="500" height="271" /></a></p>
<p>To manage XHProf profile runs, I save them by environment, and wrote a  helper that allows you to browse runs by date, and then "clean" them  out when you're done.  This works easily in a shared <em>xhprof.output_dir</em> by namespacing the stored runs according to environment name.  Here is  the example snippet for starting profiling and saving as runs:</p>
<p><script src="https://gist.github.com/584952.js?file=init_profile.php"></script></p>
<p>And you can checkout the full code over on <a title="EC XHProf on Github" href="http://github.com/empower/ec_xhprof">github</a>.</p>
<p>If you want to get statistics on or content from APC, that's available too through the <a title="APC Inspector" href="http://svn.php.net/viewvc/pecl/apc/trunk/apc.php?revision=301867&amp;view=markup">inspector</a> that ships with the extension:</p>
<p><a title="apc by empowercampaigns, on Flickr" href="http://www.flickr.com/photos/empowercampaigns/4999033447/"><img src="http://farm5.static.flickr.com/4103/4999033447_71cb3df16d.jpg" alt="apc" width="500" height="282" /></a></p>
<p>Lastly, I've also included links in the dashboard to all the tools we use: our Confluence wiki, <a title="Gerrit" href="http://code.google.com/p/gerrit/">Gerrit</a>, gitweb, and Hudson.</p>
<p>This system makes it really convenient to deploy your development  environment, access documentation, profile your code, run test tools and  analyze results.  It's my hope that as our development team grows,  it'll be relatively painless to maintain high quality, well tested code.</p>
<p>Cheers,</p>
<p>Bill Shupp, Lead Developer (twitter: http://twitter.com/shupp)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.shupp.org/2010/09/20/better-code-quality-through-easy-access-to-tools/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pragmatic tips for unit testing in PHP</title>
		<link>http://blog.shupp.org/2010/02/21/pragmatic-tips-for-unit-testing-in-php/</link>
		<comments>http://blog.shupp.org/2010/02/21/pragmatic-tips-for-unit-testing-in-php/#comments</comments>
		<pubDate>Sun, 21 Feb 2010 23:38:32 +0000</pubDate>
		<dc:creator>shupp</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Digg]]></category>
		<category><![CDATA[PEAR]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHPUnit]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://blog.shupp.org/?p=51</guid>
		<description><![CDATA[Recently I've been hearing a lot of differing opinions about unit testing.  Some people just don't believe in it at all, others think the value it provides is only part of the picture, and that its cost should be weighed accordingly.  Some think that 100% test coverage should be the goal for high quality software.  [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I've been hearing a lot of differing opinions about unit testing.  Some people just don't believe in it at all, others think the value it provides is only part of the picture, and that its cost should be weighed accordingly.  Some think that 100% test coverage should be the goal for high quality software.  I personally think the cost of unit testing 100% of your code is worth it, but <strong>only</strong> if you can do it efficiently.  And that's the catch; there's a learning curve to become proficient at unit testing, especially with <a title="PHPUnit" href="http://phpunit.de">PHPUnit</a>, the most prominent unit testing framework for PHP.  Unit testing should never take more than 20-30% of your overall coding time, if you do it right.<span id="more-51"></span></p>
<p>Below are some things I keep in mind when coding and testing.  I thought I'd share them here in case anyone else finds them useful.</p>
<p><strong>Think about testability when you write code, not when you write the test<br />
</strong></p>
<p>This may not be new to you, especially if you're familiar with <a title="dependency injection" href="http://en.wikipedia.org/wiki/Dependency_injection">dependency injection</a> or <a title="TDD" href="http://en.wikipedia.org/wiki/Test-driven_development">test driven development</a>.  Your unit tests should be able to run in a vacuum:  No database access, no memcache access, no headers can be sent, etc.  (Sure those things can be tested separately, but should be a small subset of your code, not the overall application logic.)  To do this, make sure you abstract those dependencies.  For example, instead of instantiating a cache driver in the middle of your method, make a getter for it which you can mock:</p>
<p><script src="http://gist.github.com/310433.js?file=gistfile1.php"></script></p>
<p>If you do this abstraction when you code, there's less refactoring to be done when writing your tests.  Also, if your dependency doesn't make a network call upon instantiation, you can still test the getter using <a title="assertType() documentation" href="http://www.phpunit.de/manual/3.4/en/api.html#api.assert.assertType">assertType()</a>.  <a title="Memcached::addServer() documentation" href="http://www.php.net/manual/en/memcached.addserver.php">Memcached's addServer()</a> is an example of such a dependency - no connection is made until you do an operation.  If this is not the case, and you can't properly test the getter, you probably want wrap the getter in <code>@codeCoverageIgnore</code> annotations like so:</p>
<p><script src="http://gist.github.com/310488.js?file=gistfile1.php"></script></p>
<p>It doesn't make sense for code you never intend to test to count against your coverage percentage.</p>
<p><strong>Keep documentation handy</strong></p>
<p>PHPUnit is a big framework.  There are a lot of things to remember, including all the assertion methods available, data providers, and mocking objects, among other things.  Rather than always copying some other example, or bugging your co-worker, spend an hour or two, and <a title="PHPUnit Documentation" href="http://www.phpunit.de/wiki/Documentation">RTFM</a>.  You'll be better off for it.  A co-worker of mine kept a printout of the <a title="TestCase docs" href="http://www.phpunit.de/manual/3.4/en/api.html#api.testcase.tables.api">PHPUnit_Framework_TestCase::getMock()</a> documentation (probably the hardest to remember) taped to his monitor.  I prefer <a title="Delicious" href="http://delicious.com/">delicious</a> bookmarking, but to each his own.  Though I've been using PHPUnit for a couple of years now, I still refer to the documentation regularly.</p>
<p><strong>Don't worry about test code re-usability</strong></p>
<p>As good OOP programmers, we are often pre-occupied with writing elegant, re-usable code, even in tests.  I have recently given up on this in my unit tests, mostly due to scope issues in PHPUnit which I'll describe later.  Rather than try consolidate re-usable code, I'm more likely to copy and paste chunks throughout my test and <strong>know</strong> that they will work.  I think the goal of tests is to write useful assertions, not necessarily elegant, re-usable components.  Why do I care how many lines are in my test code?  You might be thinking "Doesn't that make your tests harder to maintain?"  Maybe so, but in the big picture, I don't think it adds much time, and once you've fixed something in one test, then you can quickly find it elsewhere.  It might not be pretty, but it is faster, and more pragmatic, at least until the tools are better.</p>
<p><strong>Mocking scope issues</strong></p>
<p>As I mentioned above, I've found (and reported) some strange behavior I've seen with scope and mocked objects.  One pattern I see people use is a <code>getMocks()</code> helper in a test, which returns an array of commonly used objects, pre-mocked with the most used defaults already set.  Sounds like a reasonable approach to testing a large class with lots of dependency needs, right?  <strong>Wrong</strong>.  What I've found is that many times (and my test results have been inconsistent, unfortunately), you cannot modify the return value of a mocked method after its expectation has already been set.  No warning or error is thrown when you do this, but the original mocked value is returned instead of the second value you assigned.  Re-mocking return values <strong>does</strong> seem to work when it's used for throwing exceptions, though.  It appears to be a scope issue, but again, my tests have not always been consistent.  The usual result is that you spent an hour pulling your hear out trying to figure out why your test is failing.  I eventually said <em>screw it</em>, and just never set default behaviour for my mocked methods.</p>
<p>I now use a pattern where I have getters in my test for each mocked dependency, which mock each method I may need, but don't set any initial expectations yet.  I'll then pass them all into the getter for the main class I'm trying to test, which attaches them for me.  This way, on a per test basis, I set the expectations of each dependency as needed.  This seems to work much more reliably:</p>
<p><script src="http://gist.github.com/310459.js?file=gistfile1.php"></script></p>
<p><strong>Meaningful assertions</strong></p>
<p>Another complaint I hear is that, especially when enforcing code coverage rules in your development shop, developers focus more on the required coverage than on the quality of the tests.  It's important to think about what it is you're testing.  If you just call a method to make sure you get your code coverage, you're doing it wrong.  There are lots of assertions you can do:  assert the type of the response, assert the content of the response, assert the message of the exception  thrown, the exception code, the type of exception, how many times a mocked method was called, etc.  But you want to be smart about it as well.  We had one case where the original test for a Form framework was testing the exact template output.  Since there was markup and JS in the ouput, every time a designer or JS developer modified it, the test would break.  After a few breaks, I modified the test to use <a title="DomDocument documentation" href="http://www.php.net/manual/en/class.domdocument.php">DomDocument</a> parsing, and make sure the relevant form ids and attributes were present, which is really what we wanted to assert.  <strong>If you don't write meaningful tests, don't bother writing them at all.</strong></p>
<p><strong>Useful tools</strong></p>
<p>Make sure it's easy to write and run your tests.  PHPUnit has options for generating skeletons, for example, to get you started quickly.  You can use --skeleton-class to generate a class from your test (if you're practicing TDD), or the reverse, --skeleton-test, to generate a test from your class.</p>
<p>Running tests should be easy too.  One thing to keep in mind is that your include path must be correct.  For example, when I'm developing a <a title="PEAR" href="http://pear.php.net">PEAR</a> package, I may have a previous version already installed, but am developing in my repository checkout.  I need to make sure my local files are not only in my include path, but that they are at the beginning.  A handy wrapper like this should allow you to always run your development code easily:      <script src="http://gist.github.com/310504.js?file=runTests.php"></script></p>
<p>If I put this in the root of my repository, I can then call it like so:</p>
<p><code>php runTests.php --coverage-html coverage OpenID_AllTests</code></p>
<p>(If this is a PEAR package, you'll want to ignore <code>runTests.php</code> when generating your package.xml file.)</p>
<p>At Digg, our development environment tools are based on <a href="http://en.wikipedia.org/wiki/Make_%28software%29">make</a>, and it's super easy to run not only your PHPUnit tests and coverage, but also chain together running <a title="PHP_CodeSniffer" href="http://pear.php.net/package/PHP_CodeSniffer">PHP_CodeSniffer</a> for coding standards compliance:</p>
<p><code>make coverage phpcs</code></p>
<p>Facebook has a good <a title="Facebook Post on Hiring and Tools" href="http://www.facebook.com/note.php?note_id=158285241831">post</a> about hiring in general, which includes a section on <a title="Writing Tools" href="http://www.facebook.com/note.php?note_id=171746501831">writing good tools</a> for your team.  This idea certainly applies to your testing tools.  The easier you make it for your team to write good unit tests, the better.</p>
<p><strong>Conclusion</strong></p>
<p>Unit testing in PHP is not that easy, in my opinion (compared to python, anyway).  Until you get good at it, that is, and learn to work around quirks in the tools.  Then it's really not that hard.  But everyone  values unit testing differently.  If you choose to invest in testing your code, perhaps you'll find some of the above useful.   I used to not think much about unit testing, until I had a few experiences where unit tests caught problems in my code <strong>before</strong> I had run into them myself.  At this point, I think unit testing my PHP code 100% is worth it, especially now that I can do it quickly and efficiently.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.shupp.org/2010/02/21/pragmatic-tips-for-unit-testing-in-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

