Friday, October 16, 2015

PHPUnit Testing in Composer Project

install PHPUnit

wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit

now phpunit is accessible globally

download some project from git that contains "Tests" folder and composer.json

git clone --depth=1 https://github.com/domnikl/DesignPatternsPHP.git
composer install

see if there is phpunit.xml.dist file in project root

cat phpunit.xml.dist 
phpunit bootstrap="./vendor/autoload.php" colors="true">
    <testsuites>
        <testsuite name="Design Patterns">
            <directory suffix="Test.php">Behavioral/*/Tests</directory>
            <directory suffix="Test.php">Creational/*/Tests</directory>
            <directory suffix="Test.php">More/*/Tests</directory>
            <directory suffix="Test.php">Structural/*/Tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <blacklist>
            <directory>./vendor</directory>
        </blacklist>
    </filter>
</phpunit>

From project root run this

phpunit -vvv
PHPUnit 5.6.1 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.0.8-0ubuntu0.16.04.2
Configuration: /var/www/html/DesignPatternsPHP/phpunit.xml.dist

................................................................. 65 / 68 ( 95%)
...                                                               68 / 68 (100%)

Time: 839 ms, Memory: 10.00MB

OK (68 tests, 208 assertions)

or you can run one test on single file also from project root

phpunit Structural/Composite/Tests/CompositeTest.php
PHPUnit 5.6.1 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 103 ms, Memory: 8.00MB

OK (1 test, 1 assertion)

detailed output for each test

phpunit --testdox
PHPUnit 5.6.1 by Sebastian Bergmann and contributors.

DesignPatterns\Behavioral\ChainOfResponsibilities\Tests\Chain
 [x] Can request key in fast storage
 [x] Can request key in slow storage

DesignPatterns\Behavioral\Command\Tests\Command
 [x] Invocation
...

Inside DesignPatternsPHP/Structural/Composite/TestsCompositeTest.php you can put method like this

public function testMyMethod(){
   print __METHOD__;
   $this->assertEquals("abcd", "abc");
}

and run phpunit --verbose you'll get

1) DesignPatterns\Structural\Composite\Tests\CompositeTest::testMyMethod
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'abcd'
+'abc'

Also you'll get name of method in output in 65th test.

No comments:

Post a Comment