Install and use PHPunit

 Install and use PHPunit

    Install PHPunit under Linux

    PHP Archive (PHAR) 
    The easiest way to get PHPUnit is to download PHPUnit's PHP Archive (PHAR), which bundles all the necessary components (and some optional components) required by PHPUnit in a single file:

    To use the PHP Archive (PHAR) requires the phar extension.

    The openssl extension is required to use the --self-update feature of PHAR.

    If the Suhosin extension is enabled, PHAR execution needs to be allowed in php.ini:

    suhosin.executor.include.whitelist = phar 
    

If you want to install PHAR globally:

$ wget https://phar.phpunit.de/phpunit.phar
$ chmod +x phpunit.phar
$ chmod +x phpunit.phar
$ sudo mv phpunit.phar /usr/local/bin/phpunit
$ phpunit --version

    PHPUnit xyz by Sebastian Bergmann and contributors. 
    You can also use the downloaded PHAR file directly:

$ wget https://phar.phpunit.de/phpunit.phar
$ php phpunit.phar –version

    PHPUnit xyz by Sebastian Bergmann and contributors. (The author's version is PHPUnit 5.7.4 by Sebastian Bergmann and contributors.) 

    Note: PHPunit has the latest version that supports php7.* The official recommendation is that we install the latest version of php, of course, it is not the same to install the latest version, but if your version is php6.*+ it is best to download the latest PHPunit

 

Install PHPunit on Windows
  1. Create a directory for the PHP binary executable, for example D:\Server\bin

  2. Add D:\Server\bin to the PATH environment variable (so that PHPunit takes effect globally)

  3. Download https://phar.phpunit.de/phpunit.phar and save the file to C:\bin\phpunit.phar (note that the download is usually phpunitx.y.phar, with the version number, the name should be the same as below The file executed by the command has always been executed. Otherwise, the command will not find the file so that it prompts could not open file ....)

  4. Open a command line (for example, press Windows+R » cmd » ENTER)

     Create an overwrite batch script (finally get D:\Server\bin\phpunit.cmd):

C:\Users\username> cd D:Server\bin
C:\bin> echo @php "%~dp0phpunit.phar" %* > phpunit.cmd
C:\bin> exit

      Open a new command line window and confirm that PHPUnit can be executed in any path: 

C:\Users\username> phpunit --version

      PHPUnit 5.7.4 by Sebastian Bergmann and contributors. 
      Note: If it doesn't work globally, try running it in the previously generated directory, such as: (If it doesn't work, there is an error in the above steps, check it carefully)

C:\Users\username> cd D:Server\bin
 D:\Server\bin phpunit --version

 

write tests

        Note:这个文件创建上面生成批处理脚本的文件夹下 
        Create the file StackTest.php

<?php
use PHPUnit\Framework\TestCase;
    class StackTest extends TestCase
    {
        public function testPushAndPop()
        {
            $stack = [];
            $this->assertEquals(0, count($stack));

            array_push($stack, 'foo');
            $this->assertEquals('foo', $stack[count($stack)-1]);
            $this->assertEquals(1, count($stack));

            $this->assertEquals('foo', array_pop($stack));
            $this->assertEquals(0, count($stack));
        }
    }
    ?>

 carry out testing

D:\Server\bin  phpunit StackTest.php
D:\Server\bin>phpunit login_test.php
    PHPUnit 5.7.4 by Sebastian Bergmann and contributors.

    .                                                                   1 / 1 (100%)

    Time: 134 ms, Memory: 8.00MB

    OK (1 test, 5 assertions)

This article is reproduced from: https://www.cnblogs.com/IT--Loding/p/6222147.html

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324974038&siteId=291194637