安裝並使用PHPunit


  安裝並使用PHPunit

    Linux 下安裝PHPunit

    PHP 檔案包 (PHAR) 
    要獲取 PHPUnit,最簡單的方法是下載 PHPUnit 的 PHP 檔案包 (PHAR),它將 PHPUnit 所需要的所有必要組件(以及某些可選組件)捆綁在單個文件中:

    要使用 PHP檔案包(PHAR)需要有 phar 擴展。

    要使用 PHAR 的 –self-update 功能需要有 openssl 擴展。

    如果啟用了 Suhosin 擴展,需要在 php.ini 中允許執行 PHAR:

    suhosin.executor.include.whitelist = phar 
    如果要全局安裝 PHAR:

$ 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 x.y.z by Sebastian Bergmann and contributors. 
    也可以直接使用下載的 PHAR 文件:

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

    PHPUnit x.y.z by Sebastian Bergmann and contributors.(筆者的版本是PHPUnit 5.7.4 by Sebastian Bergmann and contributors.) 
    注意:PHPunit是有對應版本的最新的版的支持php7.* 官方建議我們安裝最新版php,當然不一樣要安裝最新的只是如果你的版本是php6.*+最好下載最新的PHPunit

  Windows下安裝PHPunit

    1.   為 PHP 的二進制可執行文件建立一個目錄,例如 D:\Server\bin

    2.   將 D:\Server\bin 添加加到 PATH 環境變量中(這樣PHPunit全局生效)

    3.   下載 https://phar.phpunit.de/phpunit.phar 並將文件保存到 C:\bin\phpunit.phar(注意下載下來一般是phpunitx.y.phar,帶版本號的,名字要和下面命令執行的文件一直不然執行命令會找不到文件以至於提示could not open file ….)

    4.   打開命令行(例如,按 Windows+R » 輸入 cmd » ENTER)

      建立外包覆批處理腳本(最后得到 D:\Server\bin\phpunit.cmd):

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

      新開一個命令行窗口,確認一下可以在任意路徑下執行 PHPUnit: 

C:\Users\username> phpunit --version 

      PHPUnit 5.7.4 by Sebastian Bergmann and contributors. 
      注:如果全局下不能運行,那就到之前生成的目錄下運行試試,如:(還不行就是上述步驟出錯了,仔細檢查下)

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

  編寫測試

        注:這個文件創建上面生成批處理腳本的文件夾下 
        創建文件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));
        }
    }
    ?>

 

 

    進行測試

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)

 

 

    PHPunit的安裝和編寫測試已經完成了。具體的操作請查看官方手冊。 
    官網手冊

    歡迎指正交流 QQ:407461375


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM