42

How to install phpunit?

I read documentation https://github.com/sebastianbergmann/phpunit, but have an error:

>pear upgrade PEAR
Nothing to upgrade

>pear config-set auto_discover 1
config-set succeeded

>pear install pear.phpunit.de/PHPUnit
No releases available for package "pear.phpunit.de/PHPUnit"
install failed

How can I fix this error?

neophyte
  • 6,540
  • 2
  • 28
  • 43
Dmitry
  • 7,457
  • 12
  • 57
  • 83

5 Answers5

70

Try the following instructions:

  1. In the command prompt, switch to the directory that you installed PHP to by running cd C:\php\
  2. Then install PEAR by running php go-pear.phar
  3. Press Enter to accept the default when it asks you “Are you installing a system-wide PEAR or a local copy?”
  4. Press Enter again to accept the file layout.
  5. Press Enter to finish.
  6. Run the following commands (they may take a while to update, be patient):
    • pear channel-update pear.php.net
    • pear upgrade-all
    • pear channel-discover pear.phpunit.de
    • pear channel-discover components.ez.no
    • pear channel-discover pear.symfony-project.com
    • pear update-channels
  7. Clear your pear cache pear clear-cache
  8. To install PHPUnit, run pear install --alldeps --force phpunit/PHPUnit
  9. To test that PHPUnit was successfully installed, run phpunit -v
Bira
  • 4,531
  • 2
  • 27
  • 42
Satya
  • 8,693
  • 5
  • 34
  • 55
  • 1
    It fails on line `pear install --alldeps --force phpunit/PHPUnit`: `No releases available for package "pear.phpunit.de/PHPUnit" install failed` – Dmitry Sep 29 '12 at 18:10
  • 4
    It's very important to say that you run command prompt as administrator! – Dragos Rizescu Jan 08 '14 at 19:06
  • 7
    If you are missing `go-pear` as was I, it can be downloaded from the pear website: http://pear.php.net/go-pear.phar. – nexus-bytes Apr 12 '14 at 05:55
  • 4
    Bad news: running `phpunit -v` you can see that `pear.phpunit.de` will be deactivated `no later than December, 31 2014`. Glad that i could grab it before that! :) – Rafael Barros May 12 '14 at 20:55
  • 1
    IMPORTANT: This method of installing php-unit is no longer supported. Check my answer for a link to an alternative solution... – Wilt Oct 24 '15 at 17:49
49

Old answer (2014): It's said that phpunit will not be available via PEAR since December 2014.
So it's easy to install it using composer:

composer global require "phpunit/phpunit=4.1.*"

Update 2019: it should be installed as a local (for your project) development package:

 composer require --dev phpunit/phpunit ^8

Update 2020: it should be installed as a local (for your project) development package: composer require --dev phpunit/phpunit ^9.3

nhlchrs
  • 3
  • 3
Dmitry
  • 7,457
  • 12
  • 57
  • 83
  • 1
    Installation is not a problem, how to *launch* it under Windows after that? – hijarian Apr 03 '17 at 10:26
  • @hijarian if you follow Wilt's answer, do as the documentation says: download _phpunit.phar_ and create a bash script that executes _php.exe phpunit.phar_. Don't forget to add the directories of the PHP executable and the PHAR file to the Path environment variable. – gabrielmdu Sep 18 '17 at 01:49
  • 8
    `composer global require phpunit/phpunit` for the latest – Lucas Bustamante Sep 21 '18 at 23:44
  • 2
    `composer global require phpunit/phpunit` is the best answer. – HartleySan May 07 '19 at 12:51
12

As said by @Wilt Installation via pear doesn't works any longer. Follow below steps instead

Step I: Create a directory named bin in C drive.

Step II: Now add the path C:\bin to your environment.

  • To do this click on Windows icon and right click on Computer and then select Properties.
  • Then click on Advanced system settings -> Advanced -> Environment Variables.
  • In the System variables section scroll down and select the line where the Variable column value is Path. Click on Edit.
  • Now add (append at the end) ;C:\bin at the end.

Step III: Download phpunit phar file to C:\bin folder.

  • If you are PHP 7 then download the phar file from https:// phar.phpunit.de/phpunit-6.2.phar. Else if you are using PHP 5.6 then download the phar file from https:// phar.phpunit.de/phpunit-5.7.phar.
  • Once downloaded rename the file to phpunit.phar and move it to C:\bin folder.

Step IV: Create a batch script phpunit.cmd

  • Open command prompt. Type cd C:\bin and hit enter.

  • Then type echo @php "%~dp0phpunit.phar" %* > phpunit.cmd and hit enter.

To verify PHPUnit has been installed type phpunit --version in command prompt. You should get something like PHPUnit x.y.z by Sebastian Bergmann and contributors.

Reference: https://perials.com/installing-phpunit-windows/

Suraj
  • 121
  • 1
  • 2
1

I use Windows XP.

I wasted a lot of time trying to use pear (the proscribed method to get MakeGood working with Eclipse IDE), only to discover the repository for phpunit is no longer available. This information should be nearer the top of this page so people do not waste their time too.

I installed phpunit via the download at https://phpunit.de/ and following the accompanying instructions. By adding the path to the command file to PATH I can now run phpunit from the command line. I have not however got phpunit running in MakeGood/Eclipse. Much of the information on that is out-of-date as it requires pear.

jpkrc
  • 71
  • 7
1

Try this in Windows Subsystem for Linux (wsl):

sudo apt install phpunit
mustaccio
  • 18,234
  • 16
  • 48
  • 57
Luis Riego
  • 11
  • 1
  • Welcome to Stack Overflow! Please note you are answering a four years old and already answered question. Here is a guide on [How to Answer](http://stackoverflow.com/help/how-to-answer). – help-info.de Aug 27 '18 at 17:49