I've recently installed laravel and have written some tests in /tests directory but when I use phpunit
at cmd in the same folder that phpunit.xml
exists, it says 'phpunit' is not recognized as an internal or external command,operable program or batch file.
. I'm using windows 7. what should I do?

- 3,673
- 8
- 34
- 60
-
Yes I mean installed it – Ramin Omrani Jun 21 '14 at 19:48
-
Did you set the environmental variable? – Rahil Wazir Jun 21 '14 at 19:49
-
Environment variables for what? – Ramin Omrani Jun 21 '14 at 19:50
-
To be able to run commands with `phpunit`. It was working before? – Rahil Wazir Jun 21 '14 at 19:51
-
no it wasn't , so you mean I should add the phpunit executable to PATH yeah? – Ramin Omrani Jun 21 '14 at 19:53
-
Big yes! If it isn't already. Try `echo %PATH%` in cmd and see the output contain `phpunit` – Rahil Wazir Jun 21 '14 at 19:54
11 Answers
The solution for me:
php vendor/phpunit/phpunit/phpunit
This, of course, assumes you've set up a php environment variable in Windows
As Unnawut said, it doesn't work because vendor/phpunit/phpunit/phpunit is not a native Windows executable. You need a .bat or .cmd file that will basically call 'php phpunit'. There should be one in vendor/bin, but to make life easy, try this - create a file phpunit.bat (or .cmd) at the root of your site, containing this:
@ECHO OFF
SET BIN_TARGET=%~dp0/vendor/phpunit/phpunit/phpunit
php "%BIN_TARGET%" %*
Now you can call phpunit from the command line at the root of the site.

- 383
- 2
- 9
If you are a window user and you are having this issue, do this:
You need to tell Window where to find PHPUnit command, you can first of all verify that this file exists in your Laravel project under /vendor/bin
Finally you need to append the full path to /vendor/bin
in your window PATH variable,
To do this: 1. Right-click on 'Computer' then click properties
- On the second window click
Advanced system settings
- On the next window under
Advanced
clickEnvironmental Variables
- On the next window double-click
PATH
then set PATH variable by appending
the full path to your laravel-project/vendor/bin;
Notice the ;
at the end.
NB: Other variables might already exists in the PATH, so ensure you don't overwrite them by appending your own at the very end
- Finally click
Ok
on all the dialog boxes

- 16,745
- 10
- 77
- 96
alias phpunit="vendor/bin/phpunit"

- 165
- 1
- 11
-
5While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Jul 30 '16 at 19:58
I added this command in command line instead of just "phpunit"
vendor\bin\phpunit
That worked for me.

- 5,064
- 33
- 79
- 116
-
1
-
1Or you can add `./vendor/bin` to the PATH environment variable. This way you can just call `phpunit` from any Laravel root folder. – Arno van Oordt Feb 23 '20 at 10:13
-
@ArnovanOordtPlease submit your comment as an answer to this question. It easy to implement, is permanent and has global scope. It is the best answer here and deserves it's own post. – Crazy World May 27 '20 at 02:03
Install phpunit
globally:
composer global require phpunit/phpunit
Afterwards you will be able to run phpunit
( even on Windows ):
phpunit

- 8,226
- 20
- 73
- 164
The phpunit executable is not in your project root folder, that's why it can't find it.
Now I assume that you already have phpunit in your composer.json
file, something like this:
"require-dev": {
"phpunit/phpunit": "3.7.*"
}
When installed by composer, the package will be installed to vendor/vendor_name/package_name
. So to run it at your project root, type this command:
vendor/phpunit/phpunit/phpunit

- 7,500
- 1
- 26
- 33
-
`composer update` and after that do `vendor/phpunit/phpunit/phpunit` from the folder you have `phpunit.xml` – Unnawut Jun 21 '14 at 20:04
-
This working for me
In double quotes this command in console windows
"vendor/bin/phpunit"

- 419
- 4
- 6
Borrowing from @Chris' excellent answer:
Even better, you can make vendor/phpunit/phpunit/phpunit
an environment variable, say "phpunit" and whenever you want to run the test in any laravel project you just call php %phpunit%
.
Demonstration

- 1
- 1

- 269
- 2
- 13
If it says the following:
$ phpunit tests/Feature/ExampleTest.php PHPUnit 3.7.21 by Sebastian Bergmann.
Class 'tests/Feature/ExampleTest' could not be found in 'C:\xampp\htdocs\blog1\tests\Feature\ExampleTest.php'.
Instead of typing tests/Feature/ExampleTest.php you say tests " \\Feature\\Example.test" because you're using windows, not mac. :) GL & HF
Using just \ or / will give errors :)

- 11
With Laravel phpunit is set up right out of the box. The easiest way to run it on Windows is to add an entry to scripts in your package.json file...
"scripts": {
...
"tests": "php vendor/phpunit/phpunit/phpunit"
},
Now you simply run unit tests with
npm run tests

- 4,691
- 2
- 32
- 51