Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'll start of by linking to the manual and then going into what I've seen and heard in the field.</p> <p><a href="http://phpunit.de/manual/current/en/organizing-tests.html">Organizing phpunit test suites</a></p> <h3>Module / Test folder organization in the file system</h3> <p>My recommended approach is combining the file system with an xml config.</p> <pre><code>tests/ \ unit/ | - module1 | - module2 - integration/ - functional/ </code></pre> <p>with a <code>phpunit.xml</code> with a simple:</p> <pre><code>&lt;testsuites&gt; &lt;testsuite name="My whole project"&gt; &lt;directory&gt;tests&lt;/directory&gt; &lt;/testsuite&gt; &lt;/testsuites&gt; </code></pre> <p>you can split the testsuites if you want to but thats a project to project choice.</p> <p>Running <code>phpunit</code> will then execute ALL tests and running <code>phpunit tests/unit/module1</code> will run all tests of module1.</p> <h3>Organization of the "unit" folder</h3> <p>The most common approach here is to mirror your <code>source/</code> directory structure in your <code>tests/unit/</code> folder structure.</p> <p>You have one TestClass per ProductionClass anyways so it's a good approach in my book.</p> <h3>In file organization</h3> <ul> <li>One class per file.</li> </ul> <p>It's not going to work anyways if you have more than one test class in one file so avoid that pitfall.</p> <ul> <li>Don't have a test namespace</li> </ul> <p>It just makes writing the test more verbose as you need an additional use statement so I'd say the testClass should go in the same namespace as the production class but that is nothing PHPUnit forces you to do. I've just found it to be easier with no drawbacks.</p> <h3>Executing only a few tests</h3> <p>For example <code>phpunit --filter Factory</code> executes all FactoryTests while <code>phpunit tests/unit/logger/</code> executes everything logging related.</p> <p>You can use <code>@group</code> tags for something like issue numbers, stories or something but for "modules" I'd use the folder layout.</p> <h3>Multiple xml files</h3> <p>It can be useful to create multiple xml files if you want to have:</p> <ul> <li>one without code coverage</li> <li>one just for the unit tests (but not for the functional or integration or long running tests) </li> <li>other common "filter" cases</li> <li>PHPBB3 for example does that for <a href="https://github.com/phpbb/phpbb3"><strong><code>their phpunit.xmls</code></strong></a></li> </ul> <h3>Code coverage for your tests</h3> <p>As it is related to starting a new project with tests:</p> <ul> <li>My suggestion is to use <code>@covers</code> tags <a href="http://edorian.github.io/2011-11-04-An-introduction-to-phpunits-covers-annotation/">like described in my blog</a> (Only for unit tests, always cover all non public functions, always use covers tags.</li> <li>Don't generate coverage for your integration tests. It gives you a false sense of security.</li> <li>Always use whitelisting to include all of your production code so the numbers don't lie to you!</li> </ul> <h3>Autoloading and bootstrapping your tests</h3> <p>You don't need any sort of auto loading for your tests. PHPUnit will take care of that.</p> <p>Use the <code>&lt;phpunit bootstrap="file"&gt;</code> attribute to specify your test bootstrap. <code>tests/bootstrap.php</code> is a nice place to put it. There you can set up your applications autoloader and so on (or call your applications bootstrap for that matter).</p> <h1>Summary</h1> <ul> <li>Use the xml configuration for pretty much everything</li> <li>Seperate unit and integration tests</li> <li>Your unit test folders should mirror your applications folder structure</li> <li>To only execute specif tests use <code>phpunit --filter</code> or <code>phpunit tests/unit/module1</code></li> <li>Use the <strong><code>strict</code></strong> mode from the get go and never turn it off. </li> </ul> <h3>Sample projects to look at</h3> <ul> <li><a href="https://github.com/beberlei/bankaccount">Sebastian Bergmanns "Bank Account" example project</a></li> <li><a href="https://github.com/phpbb/phpbb3">phpBB3</a> Even so they have to fight some with their legacy ;)</li> <li><a href="https://github.com/symfony/symfony">Symfony2</a></li> <li><a href="https://github.com/doctrine/doctrine2">Doctrine2</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload