18

I just installed a fresh Laravel 5 project, my first one on this version. PHPUnit is supposed to be out of the box with the framework and every tutorials I saw just say to type phpunit within the project folder to launch the Unit Tests.

I checked and PHPUnit is in the composer.json, I also did a composer install and composer update just in case it wouldn't be here

website(master)$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Removing phpunit/phpunit (4.6.1)
  - Installing phpunit/phpunit (4.6.2)
    Downloading: 100%

But it just doesn't work phpunit isn't recognized at all

 website(master)$ phpunit
-bash: phpunit: command not found

Seems like nobody got this problem before as I Googled it. I hope I'm not doing any stupid mistake. Any idea or suggestion ? Thanks guys ;)

Laurent
  • 2,284
  • 2
  • 21
  • 41

8 Answers8

52

I didn't install PHPUnit globally and didn't define the path. So for anyone who would have same problem :

composer global require phpunit/phpunit
composer global require phpunit/dbunit

Then you add this to you ~/.bash_profile or ~/.profile

export PATH=~/.composer/vendor/bin:$PATH
Laurent
  • 2,284
  • 2
  • 21
  • 41
  • 9
    For anyone using phpunit globally: might this approach have the downside of always testing with the same version of phpunit VS testing with the one defined by the project composer.json file (as the answer of atul_systematix states)? Or isn't this really an issue to worry about? – Bert H Nov 06 '17 at 10:19
17

This occurs when you don't have phpunit installed globally.

Run this command to use the local version (installed with composer):

vendor/bin/phpunit
Bert H
  • 1,087
  • 1
  • 15
  • 29
atul_systematix
  • 253
  • 3
  • 12
  • 6
    While this code snippet may solve the question, [including an explanation](http://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. – DimaSan Dec 14 '16 at 07:50
11

in windows machine the command is different please use this command

php vendor/phpunit/phpunit/phpunit

orignal source

Sunil Kumar
  • 759
  • 7
  • 17
3

You can run this command in cmd before running phpunit command:

doskey phpunit="vendor/bin/phpunit"

And if you are lazy as I am, you can run this one:

doskey pu="vendor/bin/phpunit"
Saeid
  • 1,269
  • 1
  • 9
  • 12
2

for people who have WINDOWS 7, use the .\vendor\bin\phpunit command instead of ./vendor/bin/phpunit

0

Run the command

composer config --list --global | grep -w home

You can find the find the [home] with composer path, similar to this one.

[home] /home/example_username/.config/composer

The path ~/.config/composer is where composer global packages are installed. Next run the command...

export PATH=~/.config/composer/vendor/bin:$PATH
Madan Sapkota
  • 25,047
  • 11
  • 113
  • 117
0

I made a permanent link to my phpunit like this

echo 'alias phpunit=vendor/bin/phpunit' >> ~/.bash_aliases

now phpunit is working by itself and stays even after I restart the terminal

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
-1

Include this line on your composer.json

"phpunit/phpunit": "4.0.*",

Run composer update. You should be able to run the following command on your Laravel directory.

vendor/bin/phpunit 
Kent Aguilar
  • 5,048
  • 1
  • 33
  • 20