Search code examples
phplaravelphpunit

Difference between running tests with commands phpunit and artisan


I have a laravel project which has some unit tests written in them.

When i run those tests with php vendor/bin/phpunit the results look like this: php unit test result

Now when i run the tests with php artisan test the results look like this:

artisan test result

Is it possible to run the phpunit command with the styling of the artisan test command?

I like the extra information about the duration of my tests but I need to run the phpunit command for some extra logging etc.

EDIT:

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
  <testsuites>
    <testsuite name="Unit">
      <directory suffix="Test.php">./tests/Unit</directory>
      <directory suffix="Test.php">./Modules/**/Tests/Unit</directory>
    </testsuite>
    <testsuite name="Feature">
      <directory suffix="Test.php">./tests/Feature</directory>
      <directory suffix="Test.php">./Modules/**/Tests/Feature</directory>
    </testsuite>
  </testsuites>
  <coverage/>
  <php>
    <server name="APP_ENV" value="testing"/>
    <server name="BCRYPT_ROUNDS" value="4"/>
    <server name="CACHE_DRIVER" value="array"/>
    <server name="MAIL_MAILER" value="array"/>
    <server name="QUEUE_CONNECTION" value="sync"/>
    <server name="SESSION_DRIVER" value="array"/>
    <server name="TELESCOPE_ENABLED" value="false"/>
  </php>
  <source>
    <include>
      <directory suffix=".php">./app</directory>
    </include>
  </source>
</phpunit>

Solution

  • In the package nunomaduro/collision is the logic to print the results as given.

    In the composer.json file of the package is a autoload file registered. This file is: vendor/nunomaduro/collision/src/Adapters/Phpunit/Autoload.php

    It will check if the $_SERVER['COLLISION_PRINTER'] is set. If so, it register a PHPUnit event subscriber that will modify the output. This is seen in the file vendor/nunomaduro/collision/src/Adapters/Phpunit/Subscribers/EnsurePrinterIsRegisteredSubscriber.php

    To run PHPUnit with the pretty output run:

    COLLISION_PRINTER=DefaultPrinter php vendor/bin/phpunit --color=always
    

    This requires that the nunomaduro/collision package is installed.

    When running the php artisan test command it will set the COLLISION_PRINTER environment variable when running PHPUnit. This can be seen in the file api/vendor/nunomaduro/collision/src/Adapters/Laravel/Commands/TestCommand.php