Using PHPUnit directly on Laravel3

タグ: Laravel3   PHPUnit  

You can use Artisan command line tool for your unit tests.

php artisan test

By this command, you can test all files with suffix test.php under a folder application/tests.

But this is for unit test only. And limited. PHPUnit is more. So it is better use of phpunit command directly on Laravel 3.

If you use any frameworks, you must test on the framework environment. So you can specify bootstrap code to work the framework. Like artisan test do so.

phpunit try to find phpunit.xml or phpunit.xml.dist file. If found, PHPUnit use it. You can put it on favoret directory, but it is easy to put it on Laravel top folder. Or you can give parameter to folder that have the xml file with option -c.

Here is example phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
    <!-- set Laravel environmet -->
        <env name="LARAVEL_ENV" value="test" />
        <testsuite name="Sample test suite">
            <!-- specified suffix of test cases -->
            <directory suffix="Test.php">application/tests</directory>
    <!-- there are logs for coverage reports. -->
    <!-- if you don't use coverage, no needed specified -->
        <log type="coverage-html" target="build/coverage" title="Base32"
             charset="UTF-8" yui="true" highlight="true"
             lowUpperBound="35" highLowerBound="70"/>
        <log type="coverage-clover" target="build/logs/clover.xml"/>
        <!-- this is JUnit test report of Jenkins -->
        <log type="junit" target="build/logs/junit.xml" logIncompleteSkipped="false"/>
    <!-- coverage targets. no needed coverage? so no needed. -->
        <whitelist addUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">application/models</directory>
            <directory suffix=".php">application/controllers</directory>

This included coverage stuff. When you use IDE, normally it support code coverage report. Or Jenkins also supported coverage. It is not so hard.

If you don't need coverage, and section is no needed. Remove them. It became more simple.

Once set the xml file, you can just type phpunit. Or if you put it somewhere else, phpunit -c your-directory-where-put-the-xml.