phpunit 單元測試

用了這麼久的php一直都是用xdebug在調試,今天想來試試php單元測試。php

安裝phpunitpython

  • 爲 PHP 的二進制可執行文件創建一個目錄,例如 D:\phpunitlinux

  • 將 ;D:\phpunit 添加加到 PATH 環境變量中
    windows

  • 下載 http://phar.phpunit.cn/phpunit-6.2.phar 並將文件保存到 D:\phpunit\phpunit.phar //linux使用wget下載單元測試

  • 打開命令行測試

  • 創建外包覆批處理腳本(最後獲得 C:\bin\phpunit.cmdthis

    C:\Users\Administrator>D:
    D:\>cd phpunit
    D:\phpUnit> echo @php "%~dp0phpunit.phar" %* > phpunit.cmd

 

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

  

2.編寫測試類spa

  新建一個Demo.php文件命令行

<?php
class
Demo{ public function foo(){ return "foo"; } public function bar(){ return "bar"; } }

   在寫一個test.php文件debug

<?php
include
"Demo.php"; use PHPUnit\Framework\TestCase; class TestDemo extends TestCase{ public function testFoo(){ $demo = new Demo(); $this->assertEquals('foo', $demo->foo());//返回foo斷言成功 $this->assertEquals('bar',$demo->foo());//斷言失敗 } }

  在命令行輸入phpunit test.php運行

  

 

 從圖中能夠看到,斷言了2個,失敗了一個

相關文章
相關標籤/搜索