6

I want to run a phpunit test on my local windows machine..(using wamp)..

I have globally installed composer..And phpunit is installed on my local directory..

Folder Structure..

_________tests
|          |
|          |______PagesTest.php
|
|________vendor
           |
           |______bin
                   |
                   |_____phpunit  

On my localroot(www)..I have created a phpunit.xml file..

<?xml version="1.0" encoding="UTF-8"?>

<phpunit convertWarningsToExceptions="true"
        convertNoticesToExceptions="true"
        convertErrorsToExceptions="true"
        backupStaticAttributes="false"
        processIsolation="false"
        stopOnFailure="false"
        backupGlobals="false"
        syntaxCheck="false"
        colors="true">
    <testsuites>
        <testsuite name="Punta Test Suite">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>
</phpunit>        

On Cmd I tried:

C:\wamp\www\vendor\bin>phpunit PagesTest

The output is

Cannot open file PagesTest.php

And I have no idea how can i run this PagesTest.php file using composer

2 Answers2

15

Ok, PHPUnit is running, but doesn't find the Test file, because the Composer Autoloader isn't loaded.

You can solve this, by adding a configuration file for PHPUnit, which defines the attribute bootstrap and set the Composer Autoloader file.

Example for phpunit.xml.dist

<?xml version="1.0" encoding="utf-8" ?>
<phpunit bootstrap="./vendor/autoload.php">

    <testsuites>
        <testsuite name="My Project - TestSuite">
            <directory>./tests</directory>
        </testsuite>
    </testsuites>

</phpunit>

Then simply run the PHPUnit command:

$ ./vendor/bin/phpunit

On Windows just flip the slashes: C:\> .\vendor\bin\phpunit

PHPUnit will automatically detect the configuration file (phpunit.xml.dist) and load the Composer Autoloader during its bootstrap phase.

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
  • Thanks i have a xml file totally forgot about..And missed the bootstrap..But i'm on windows (wamp)(cmd)..vendor/bin/phpunit command doesnt work..which maybe a composer bug on windows.. –  Jan 03 '16 at 18:29
  • 1
    Glad i could help. (On Windows just flip the slashes on the command `.\vendor\bin\phpunit.bat`. I've updated my answer.) – Jens A. Koch Jan 03 '16 at 21:04
  • 1
    .\vendor\bin\phpunit works i accepted your answer.. –  Jan 03 '16 at 22:36
3

If the test class is located in a subdirectory, you need to pass the complete path to it like this:

phpunit ./tests/PagesTest.php

You can run phpunit without any argument which will execute all tests in the testsuite as it was configured in the phpunit.xml.dist or phpunit.xml file.

To use the phpunit binary install via Composer in your project, you need to use the path to it:

./vendor/bin/phpunit ./tests/PagesTest.php

I do not have a Windows installation to test this, but maybe you have to prefix the call with php (like php ./vendor/bin/phpunit).

xabbuh
  • 5,801
  • 16
  • 18
  • 1
    interestingly php vendor/phpunit/phpunit/phpunit solved my problem.. –  Jan 03 '16 at 19:36