SlideShare une entreprise Scribd logo
1  sur  35
PHPUnit
      入門 v1.0
軟體開發流程

  分析


  設計


  實作


  測試


  維護
軟體由多種物件組成

汽車由多種零件組成




  似曾相識
    又
  環環相扣
IEEE ( 電機電子工程師學會 ) - SWEBOK (Software Engineering Body of Knowledge)

          依軟體測試 (Software testing) 層級區分六種測試
1.   單元測試 (Unit testing)
2.   整合測試 (Integration testing)
3.   系統測試 (System testing)
4.   系統整合測試 (System integration testing)
5.   由下而上測試 (Bottom Up testing)
6.   由上而下測試 (Top Down testing)
1. 單元測試 (Unit testing)
程式語言裡 , 單元測試是由專案程式碼各個區塊受測試是
否可使用的方法 , 它也是軟體測試最小單位

In computer programming, unit testing is a method by
which individual units of source code are tested to
determine if they are fit for use. A unit is smallest testable
part of an application. - Wiki
1.   單元測試 (Unit testing)
2.   整合測試 (Integration testing)
3.   系統測試 (System testing)
4.   系統整合測試 (System integration testing)
5.   由下而上測試 (Bottom Up testing)
6.   由上而下測試 (Top Down testing)
2. 整合測試 (Integration testing)
1.   單元測試 (Unit testing)
2.   整合測試 (Integration testing)
3.   系統測試 (System testing)
4.   系統整合測試 (System integration testing)
5.   由下而上測試 (Bottom Up testing)
6.   由上而下測試 (Top Down testing)
3. 系統測試 (System testing)
1.   單元測試 (Unit testing)
2.   整合測試 (Integration testing)
3.   系統測試 (System testing)
4.   系統整合測試 (System integration testing)
5.   由下而上測試 (Bottom Up testing)
6.   由上而下測試 (Top Down testing)
4. 系統整合測試 (System integration
testing)
1.   單元測試 (Unit testing)
2.   整合測試 (Integration testing)
3.   系統測試 (System testing)
4.   系統整合測試 (System integration testing)
5.   由下而上測試 (Bottom Up testing)
6.   由上而下測試 (Top Down testing)
5. 由下而上測試 (Bottom Up
testing)
6. 由上而下測試 (Top Down testing)
Google API 架構示意圖
是 否 曾 經 遇 過 如 下 情 況 :


1. 接過複雜的大型專案

2. 對自己維護的專案,極度沒信心

3. 在專案完成時,常做噩夢,怕隔天出現任何 差錯

4. 時常加班或利用周末休息時間,找程式蟲蟲
撰寫測試程式是件不容易的事

但寫出良好的測試程式需更用心
但學習撰寫測試是非常值得投資 :

1 、微軟及 IBM 研究報告顯示,可能會增加開發時間
   15% ~ 35 % ,但是可減少蟲蟲數 40% ~ 90% ( 註 1) 。

2 、寫程式時,應考慮易於寫測試程式原則。

3 、單元測試程式可以開發程式時順利而不會痛苦。

4 、我們不想在美好的假日或優閒的下班時間 , 被蟲蟲給吸光 ,
  明明只是簡單改了一行程式碼,為什麼程式卻壞了。



( 註 1)
http://research.microsoft.com/en-us/groups/ese/nagappan_tdd.pdf
接下來就歡迎今天的主角

  Mr. PHPUnit
Mr. PHPUnit 自我介紹

即使是一個優秀的程式設計師都會犯錯,但是優秀的程式員會利用測試盡
可能找出錯誤,花多點時間寫出測試程式,就越容易找出蟲蟲,程式錯誤
無法全部找出,但是藉由測試程式,盡可能找出有問題的程式。

使用 PHPUnit 可能和你一般使用的測試方式有些部分類似 , 不同的是會
測試是否和你預期的一樣,有效率自動執行程式你想測試的程式片段。
PHPUnit 身家背景
1. PHPUnit 是 PHP 的測試框架 , 作者是 Sebastian Bergman

2. 它是 xUnit 測試框架家族

3. PHPUnit 已經成為測試業界標準

4. 被知名的 PHP 框架所使用 , 例如 :

  Zend Framework
  Symphony
  CakePHP
  Doctrine
xUnit 簡介
1. 單元測試

2. 自動化測試

3. 源於 Smalltalk 的 SUnit, 由 Kent Beck 移植架構 , 使用 Java
寫成 JUnit, 承襲此架構其他語言相繼出現 , 例如 :

(1) C++ 的 CppUnit
(2) PHP 的 PHPUnit
(3) JavaScript 的 JsUnit, YUI Test, Qunit

JsUnit (2001 ~ 2009) 目前已停止開發維護 , 它是 JS 語言第一個
出現的測試套件

Qunit 是 jQuery 作者開發 , 後由社群維護 , 遵循
CommonJS Unit Testing 規範
測試導向開發 (Test-driven development)
如何安裝 PHPUnit
安裝需求
PHPUnit 3.6
- PHP 5.2.7+ 最低
- PHP 5.3.9+ 建議

配件 : PHP_CodeCoverage
- Xdebug 2.0.5+ 最低
- Xdebug 2.1.3+ 建議


PHPUnit 3.7
- PHP 5.3.3+ 最低
- PHP 5.4.0+ 建議

配件 : PHP_CodeCoverage
- Xdebug 2.0.5+ 最低
- Xdebug 2.2.0+ 建議
命令列執行

pear config-set auto_discover 1
pear install pear.phpunit.de/PHPUnit
撰寫基本的測試程式

檔案 StackTest.php
1 require_once PHPUnit/Autoload.php
2
3 class StackTest extends PHPUnit_Framework_TestCase
4 {
5       public function testEmpty()
6       {
7               $stack = array();
8               $this->assertEmpty($stack);
9        }
10 }
11
12 1. 檔案名稱結尾加 Test
13 2. 類別名稱結尾加 Test
14 3. 類別繼承 PHPUnit_Framework_TestCase
15 4. 類別方法名稱開頭加 test, 存取範圍設定成 public
16 5. 類別裡至少要有一個 test 開頭的方法 , 否則測試會顯示失敗
17 6. 類別方法裡至少要有一個 assert 方法被呼叫 , 否則會顯示未完成或省略
18
測試資料準備 (Test Fixtures)

檔案 StackTest.php
1 require_once PHPUnit/Autoload.php
2
3 class StackTest extends PHPUnit_Framework_TestCase
4 {
5       protected $stack;
6
7       protected function setUp()
8       {
9           $this->stack = array();
10      }
11
12      public function testEmpty()
13      {
14           $this->assertTrue(empty($this->stack));
15      }
16 }

1.   經由 setUp 或 tearDown 方法 , 可在測試前設置所需資料
2.   setUp 在執行測試前呼叫 , tearDown 則在之後被呼叫
3.   藉由這兩種方法 , 可以從是檔案或者網路相關測試
4.   另外 setUpBeforeClass 和 tearDownAfterClass 個別在執行 test 方法前後被呼叫
5.   執行順序 setUp => setUpBeforeClass => test* => tearDownAfterClass => tearDown
Mock 物件
PHPUnit 一項重大功能
什麼是 Mock 物件 ?!

1. 它是測試類別的依賴類別的替代品

2. 取代依賴呼叫方法 , 讓執行測試時暢行無阻

3. 正確使用它 , 可以正確模擬原有依賴類別特性

4. 隔離依賴類別 , 可以幫助你判斷是否目前測試類別錯誤

5. 使用 mock 物件方法稱為 Dependency Injection (DI)
Mock 物件實做
class SomeClass
{
   public function doSomething()
   {
     // Do something.
   }
}

class StubTest extends PHPUnit_Framework_TestCase
{
   public function testStub()
   {
     $stub = $this->getMock('SomeClass');

        $stub->expects($this->any())
             ->method('doSomething')
             ->will($this->returnValue('foo'));

        $this->assertEquals('foo', $stub->doSomething());
    }
}
Mock 物件其它使用

1. 可使用 getMockForAbstractClass() 方法測試抽象類別

2. 可使用 getMockFromWsdl() 方法模擬 SOAP 網路服務

3. PHPUnit 支援檔案系統模擬 , 需安裝 vfsStream 套件 , 目前仍為測試版
命令列執行測試

1. 直接輸入 PHPUnit 不加任何參數則返回說明訊息

2. 當測試某個檔案時 , 參數輸入檔案名稱 , 即可執行測試

3. 當測試整個目錄時 , 參數輸入目錄路徑 , PHPUnit 會依
提供目錄路徑遞迴方式一層一層搜尋檔案執行

4. PHPUnit 執行測試後 , 成功時會印出『 . 』 ,
失敗時會印出 F, 未完成時會印出 I, 省略跳過則會印出 S

5. 還會顯示執行秒數 , 使用記憶體大小 , 測試了多少方法

Contenu connexe

Tendances

使用 Pytest 進行單元測試 (PyCon TW 2021)
使用 Pytest 進行單元測試 (PyCon TW 2021)使用 Pytest 進行單元測試 (PyCon TW 2021)
使用 Pytest 進行單元測試 (PyCon TW 2021)Max Lai
 
Efficient JavaScript Unit Testing (Chinese Version), JavaOne China 2013
Efficient JavaScript Unit Testing (Chinese Version), JavaOne China 2013Efficient JavaScript Unit Testing (Chinese Version), JavaOne China 2013
Efficient JavaScript Unit Testing (Chinese Version), JavaOne China 2013Hazem Saleh
 
相關軟體、套件安裝與除錯工具
相關軟體、套件安裝與除錯工具相關軟體、套件安裝與除錯工具
相關軟體、套件安裝與除錯工具數真 蔡
 
Java单元测试
Java单元测试Java单元测试
Java单元测试darlingshan
 
How to avoid check style errors
How to avoid check style errorsHow to avoid check style errors
How to avoid check style errorsGuo Albert
 
Maven初级培训
Maven初级培训Maven初级培训
Maven初级培训ytsolar
 
Top100summit 陈辉-游戏测试平台 策划资源文件自动化测试体系
Top100summit 陈辉-游戏测试平台 策划资源文件自动化测试体系Top100summit 陈辉-游戏测试平台 策划资源文件自动化测试体系
Top100summit 陈辉-游戏测试平台 策划资源文件自动化测试体系drewz lin
 
Foundation of software development 1
Foundation of software development 1Foundation of software development 1
Foundation of software development 1netdbncku
 
Selenium私房菜(新手入门教程)
Selenium私房菜(新手入门教程)Selenium私房菜(新手入门教程)
Selenium私房菜(新手入门教程)liqiang xu
 
嵌入式測試驅動開發
嵌入式測試驅動開發嵌入式測試驅動開發
嵌入式測試驅動開發hugo lu
 
单元测试(H2等)和持续集成(Hudson)实战简介
单元测试(H2等)和持续集成(Hudson)实战简介单元测试(H2等)和持续集成(Hudson)实战简介
单元测试(H2等)和持续集成(Hudson)实战简介isxylands
 
2014/02: 嵌入式測試驅動開發
2014/02: 嵌入式測試驅動開發2014/02: 嵌入式測試驅動開發
2014/02: 嵌入式測試驅動開發AgileCommunity
 
20170217 julia小程式到專案發布之旅
20170217 julia小程式到專案發布之旅20170217 julia小程式到專案發布之旅
20170217 julia小程式到專案發布之旅岳華 杜
 
Selenium介绍
Selenium介绍Selenium介绍
Selenium介绍lory hou
 
测试快照
测试快照测试快照
测试快照jacquesqj
 
Clipper@datacon.2019.tw
Clipper@datacon.2019.twClipper@datacon.2019.tw
Clipper@datacon.2019.twWei-Yu Chen
 

Tendances (20)

jasmine入门指南
jasmine入门指南jasmine入门指南
jasmine入门指南
 
使用 Pytest 進行單元測試 (PyCon TW 2021)
使用 Pytest 進行單元測試 (PyCon TW 2021)使用 Pytest 進行單元測試 (PyCon TW 2021)
使用 Pytest 進行單元測試 (PyCon TW 2021)
 
Efficient JavaScript Unit Testing (Chinese Version), JavaOne China 2013
Efficient JavaScript Unit Testing (Chinese Version), JavaOne China 2013Efficient JavaScript Unit Testing (Chinese Version), JavaOne China 2013
Efficient JavaScript Unit Testing (Chinese Version), JavaOne China 2013
 
相關軟體、套件安裝與除錯工具
相關軟體、套件安裝與除錯工具相關軟體、套件安裝與除錯工具
相關軟體、套件安裝與除錯工具
 
Java单元测试
Java单元测试Java单元测试
Java单元测试
 
How to avoid check style errors
How to avoid check style errorsHow to avoid check style errors
How to avoid check style errors
 
Maven初级培训
Maven初级培训Maven初级培训
Maven初级培训
 
SQL Injection
SQL InjectionSQL Injection
SQL Injection
 
Top100summit 陈辉-游戏测试平台 策划资源文件自动化测试体系
Top100summit 陈辉-游戏测试平台 策划资源文件自动化测试体系Top100summit 陈辉-游戏测试平台 策划资源文件自动化测试体系
Top100summit 陈辉-游戏测试平台 策划资源文件自动化测试体系
 
Foundation of software development 1
Foundation of software development 1Foundation of software development 1
Foundation of software development 1
 
Selenium私房菜(新手入门教程)
Selenium私房菜(新手入门教程)Selenium私房菜(新手入门教程)
Selenium私房菜(新手入门教程)
 
Unit testing
Unit testingUnit testing
Unit testing
 
Python系列1
Python系列1Python系列1
Python系列1
 
嵌入式測試驅動開發
嵌入式測試驅動開發嵌入式測試驅動開發
嵌入式測試驅動開發
 
单元测试(H2等)和持续集成(Hudson)实战简介
单元测试(H2等)和持续集成(Hudson)实战简介单元测试(H2等)和持续集成(Hudson)实战简介
单元测试(H2等)和持续集成(Hudson)实战简介
 
2014/02: 嵌入式測試驅動開發
2014/02: 嵌入式測試驅動開發2014/02: 嵌入式測試驅動開發
2014/02: 嵌入式測試驅動開發
 
20170217 julia小程式到專案發布之旅
20170217 julia小程式到專案發布之旅20170217 julia小程式到專案發布之旅
20170217 julia小程式到專案發布之旅
 
Selenium介绍
Selenium介绍Selenium介绍
Selenium介绍
 
测试快照
测试快照测试快照
测试快照
 
Clipper@datacon.2019.tw
Clipper@datacon.2019.twClipper@datacon.2019.tw
Clipper@datacon.2019.tw
 

Similaire à PHPUnit

Phpunit入门 r2
Phpunit入门 r2Phpunit入门 r2
Phpunit入门 r2Baohua Cai
 
Testing in python 2.7.3
Testing in python 2.7.3Testing in python 2.7.3
Testing in python 2.7.3Wen Liao
 
Foundation of software development 2
Foundation of software development 2Foundation of software development 2
Foundation of software development 2netdbncku
 
Nhibernate+sqlite測試實戰經驗分享
Nhibernate+sqlite測試實戰經驗分享Nhibernate+sqlite測試實戰經驗分享
Nhibernate+sqlite測試實戰經驗分享Wade Huang
 
PHPUnit 入門介紹
PHPUnit 入門介紹PHPUnit 入門介紹
PHPUnit 入門介紹Jace Ju
 
Continuous integration
Continuous integrationContinuous integration
Continuous integrationnetdbncku
 
C语言benchmark覆盖信息收集总结4
C语言benchmark覆盖信息收集总结4C语言benchmark覆盖信息收集总结4
C语言benchmark覆盖信息收集总结4Tao He
 
Introduction to software quality assurance and its implementation
Introduction to software quality assurance and its implementationIntroduction to software quality assurance and its implementation
Introduction to software quality assurance and its implementationYung-Chun Chang
 
敏捷测试中的工具实现
敏捷测试中的工具实现敏捷测试中的工具实现
敏捷测试中的工具实现drewz lin
 
Testing survey
Testing surveyTesting survey
Testing surveyTao He
 
豆瓣I os自动化测试实践和经验
豆瓣I os自动化测试实践和经验豆瓣I os自动化测试实践和经验
豆瓣I os自动化测试实践和经验drewz lin
 
Tcon分享 芈峮
Tcon分享 芈峮Tcon分享 芈峮
Tcon分享 芈峮mijun_hlp
 
從理想、到現實的距離,開啟品味軟體測試之路 - 台灣軟體工程協會 (20220813)
從理想、到現實的距離,開啟品味軟體測試之路 - 台灣軟體工程協會 (20220813)從理想、到現實的距離,開啟品味軟體測試之路 - 台灣軟體工程協會 (20220813)
從理想、到現實的距離,開啟品味軟體測試之路 - 台灣軟體工程協會 (20220813)Rick Hwang
 
Php设计模式介绍
Php设计模式介绍Php设计模式介绍
Php设计模式介绍cyf5513
 
分布式系统测试实践
分布式系统测试实践分布式系统测试实践
分布式系统测试实践drewz lin
 
重構—改善既有程式的設計(chapter 4,5)
重構—改善既有程式的設計(chapter 4,5)重構—改善既有程式的設計(chapter 4,5)
重構—改善既有程式的設計(chapter 4,5)Chris Huang
 
网易移动自动化测试实践(孔庆云)
网易移动自动化测试实践(孔庆云)网易移动自动化测试实践(孔庆云)
网易移动自动化测试实践(孔庆云)drewz lin
 

Similaire à PHPUnit (20)

Phpunit入门 r2
Phpunit入门 r2Phpunit入门 r2
Phpunit入门 r2
 
PHP 单元测试
PHP 单元测试PHP 单元测试
PHP 单元测试
 
Testing in python 2.7.3
Testing in python 2.7.3Testing in python 2.7.3
Testing in python 2.7.3
 
Foundation of software development 2
Foundation of software development 2Foundation of software development 2
Foundation of software development 2
 
Nhibernate+sqlite測試實戰經驗分享
Nhibernate+sqlite測試實戰經驗分享Nhibernate+sqlite測試實戰經驗分享
Nhibernate+sqlite測試實戰經驗分享
 
PHPUnit 入門介紹
PHPUnit 入門介紹PHPUnit 入門介紹
PHPUnit 入門介紹
 
Continuous integration
Continuous integrationContinuous integration
Continuous integration
 
C语言benchmark覆盖信息收集总结4
C语言benchmark覆盖信息收集总结4C语言benchmark覆盖信息收集总结4
C语言benchmark覆盖信息收集总结4
 
Introduction to software quality assurance and its implementation
Introduction to software quality assurance and its implementationIntroduction to software quality assurance and its implementation
Introduction to software quality assurance and its implementation
 
敏捷测试中的工具实现
敏捷测试中的工具实现敏捷测试中的工具实现
敏捷测试中的工具实现
 
Xpp
XppXpp
Xpp
 
Testing survey
Testing surveyTesting survey
Testing survey
 
豆瓣I os自动化测试实践和经验
豆瓣I os自动化测试实践和经验豆瓣I os自动化测试实践和经验
豆瓣I os自动化测试实践和经验
 
Tcon分享 芈峮
Tcon分享 芈峮Tcon分享 芈峮
Tcon分享 芈峮
 
從理想、到現實的距離,開啟品味軟體測試之路 - 台灣軟體工程協會 (20220813)
從理想、到現實的距離,開啟品味軟體測試之路 - 台灣軟體工程協會 (20220813)從理想、到現實的距離,開啟品味軟體測試之路 - 台灣軟體工程協會 (20220813)
從理想、到現實的距離,開啟品味軟體測試之路 - 台灣軟體工程協會 (20220813)
 
Php设计模式介绍
Php设计模式介绍Php设计模式介绍
Php设计模式介绍
 
分布式系统测试实践
分布式系统测试实践分布式系统测试实践
分布式系统测试实践
 
重構—改善既有程式的設計(chapter 4,5)
重構—改善既有程式的設計(chapter 4,5)重構—改善既有程式的設計(chapter 4,5)
重構—改善既有程式的設計(chapter 4,5)
 
C++exception
C++exceptionC++exception
C++exception
 
网易移动自动化测试实践(孔庆云)
网易移动自动化测试实践(孔庆云)网易移动自动化测试实践(孔庆云)
网易移动自动化测试实践(孔庆云)
 

PHPUnit

  • 1. PHPUnit 入門 v1.0
  • 2. 軟體開發流程 分析 設計 實作 測試 維護
  • 4. IEEE ( 電機電子工程師學會 ) - SWEBOK (Software Engineering Body of Knowledge) 依軟體測試 (Software testing) 層級區分六種測試
  • 5. 1. 單元測試 (Unit testing) 2. 整合測試 (Integration testing) 3. 系統測試 (System testing) 4. 系統整合測試 (System integration testing) 5. 由下而上測試 (Bottom Up testing) 6. 由上而下測試 (Top Down testing)
  • 7. 程式語言裡 , 單元測試是由專案程式碼各個區塊受測試是 否可使用的方法 , 它也是軟體測試最小單位 In computer programming, unit testing is a method by which individual units of source code are tested to determine if they are fit for use. A unit is smallest testable part of an application. - Wiki
  • 8. 1. 單元測試 (Unit testing) 2. 整合測試 (Integration testing) 3. 系統測試 (System testing) 4. 系統整合測試 (System integration testing) 5. 由下而上測試 (Bottom Up testing) 6. 由上而下測試 (Top Down testing)
  • 10. 1. 單元測試 (Unit testing) 2. 整合測試 (Integration testing) 3. 系統測試 (System testing) 4. 系統整合測試 (System integration testing) 5. 由下而上測試 (Bottom Up testing) 6. 由上而下測試 (Top Down testing)
  • 12. 1. 單元測試 (Unit testing) 2. 整合測試 (Integration testing) 3. 系統測試 (System testing) 4. 系統整合測試 (System integration testing) 5. 由下而上測試 (Bottom Up testing) 6. 由上而下測試 (Top Down testing)
  • 13. 4. 系統整合測試 (System integration testing)
  • 14. 1. 單元測試 (Unit testing) 2. 整合測試 (Integration testing) 3. 系統測試 (System testing) 4. 系統整合測試 (System integration testing) 5. 由下而上測試 (Bottom Up testing) 6. 由上而下測試 (Top Down testing)
  • 15. 5. 由下而上測試 (Bottom Up testing) 6. 由上而下測試 (Top Down testing)
  • 17.
  • 18. 是 否 曾 經 遇 過 如 下 情 況 : 1. 接過複雜的大型專案 2. 對自己維護的專案,極度沒信心 3. 在專案完成時,常做噩夢,怕隔天出現任何 差錯 4. 時常加班或利用周末休息時間,找程式蟲蟲
  • 20. 但學習撰寫測試是非常值得投資 : 1 、微軟及 IBM 研究報告顯示,可能會增加開發時間 15% ~ 35 % ,但是可減少蟲蟲數 40% ~ 90% ( 註 1) 。 2 、寫程式時,應考慮易於寫測試程式原則。 3 、單元測試程式可以開發程式時順利而不會痛苦。 4 、我們不想在美好的假日或優閒的下班時間 , 被蟲蟲給吸光 , 明明只是簡單改了一行程式碼,為什麼程式卻壞了。 ( 註 1) http://research.microsoft.com/en-us/groups/ese/nagappan_tdd.pdf
  • 23. PHPUnit 身家背景 1. PHPUnit 是 PHP 的測試框架 , 作者是 Sebastian Bergman 2. 它是 xUnit 測試框架家族 3. PHPUnit 已經成為測試業界標準 4. 被知名的 PHP 框架所使用 , 例如 : Zend Framework Symphony CakePHP Doctrine
  • 24. xUnit 簡介 1. 單元測試 2. 自動化測試 3. 源於 Smalltalk 的 SUnit, 由 Kent Beck 移植架構 , 使用 Java 寫成 JUnit, 承襲此架構其他語言相繼出現 , 例如 : (1) C++ 的 CppUnit (2) PHP 的 PHPUnit (3) JavaScript 的 JsUnit, YUI Test, Qunit JsUnit (2001 ~ 2009) 目前已停止開發維護 , 它是 JS 語言第一個 出現的測試套件 Qunit 是 jQuery 作者開發 , 後由社群維護 , 遵循 CommonJS Unit Testing 規範
  • 27. 安裝需求 PHPUnit 3.6 - PHP 5.2.7+ 最低 - PHP 5.3.9+ 建議 配件 : PHP_CodeCoverage - Xdebug 2.0.5+ 最低 - Xdebug 2.1.3+ 建議 PHPUnit 3.7 - PHP 5.3.3+ 最低 - PHP 5.4.0+ 建議 配件 : PHP_CodeCoverage - Xdebug 2.0.5+ 最低 - Xdebug 2.2.0+ 建議
  • 28. 命令列執行 pear config-set auto_discover 1 pear install pear.phpunit.de/PHPUnit
  • 29. 撰寫基本的測試程式 檔案 StackTest.php 1 require_once PHPUnit/Autoload.php 2 3 class StackTest extends PHPUnit_Framework_TestCase 4 { 5 public function testEmpty() 6 { 7 $stack = array(); 8 $this->assertEmpty($stack); 9 } 10 } 11 12 1. 檔案名稱結尾加 Test 13 2. 類別名稱結尾加 Test 14 3. 類別繼承 PHPUnit_Framework_TestCase 15 4. 類別方法名稱開頭加 test, 存取範圍設定成 public 16 5. 類別裡至少要有一個 test 開頭的方法 , 否則測試會顯示失敗 17 6. 類別方法裡至少要有一個 assert 方法被呼叫 , 否則會顯示未完成或省略 18
  • 30. 測試資料準備 (Test Fixtures) 檔案 StackTest.php 1 require_once PHPUnit/Autoload.php 2 3 class StackTest extends PHPUnit_Framework_TestCase 4 { 5 protected $stack; 6 7 protected function setUp() 8 { 9 $this->stack = array(); 10 } 11 12 public function testEmpty() 13 { 14 $this->assertTrue(empty($this->stack)); 15 } 16 } 1. 經由 setUp 或 tearDown 方法 , 可在測試前設置所需資料 2. setUp 在執行測試前呼叫 , tearDown 則在之後被呼叫 3. 藉由這兩種方法 , 可以從是檔案或者網路相關測試 4. 另外 setUpBeforeClass 和 tearDownAfterClass 個別在執行 test 方法前後被呼叫 5. 執行順序 setUp => setUpBeforeClass => test* => tearDownAfterClass => tearDown
  • 32. 什麼是 Mock 物件 ?! 1. 它是測試類別的依賴類別的替代品 2. 取代依賴呼叫方法 , 讓執行測試時暢行無阻 3. 正確使用它 , 可以正確模擬原有依賴類別特性 4. 隔離依賴類別 , 可以幫助你判斷是否目前測試類別錯誤 5. 使用 mock 物件方法稱為 Dependency Injection (DI)
  • 33. Mock 物件實做 class SomeClass { public function doSomething() { // Do something. } } class StubTest extends PHPUnit_Framework_TestCase { public function testStub() { $stub = $this->getMock('SomeClass'); $stub->expects($this->any()) ->method('doSomething') ->will($this->returnValue('foo')); $this->assertEquals('foo', $stub->doSomething()); } }
  • 34. Mock 物件其它使用 1. 可使用 getMockForAbstractClass() 方法測試抽象類別 2. 可使用 getMockFromWsdl() 方法模擬 SOAP 網路服務 3. PHPUnit 支援檔案系統模擬 , 需安裝 vfsStream 套件 , 目前仍為測試版
  • 35. 命令列執行測試 1. 直接輸入 PHPUnit 不加任何參數則返回說明訊息 2. 當測試某個檔案時 , 參數輸入檔案名稱 , 即可執行測試 3. 當測試整個目錄時 , 參數輸入目錄路徑 , PHPUnit 會依 提供目錄路徑遞迴方式一層一層搜尋檔案執行 4. PHPUnit 執行測試後 , 成功時會印出『 . 』 , 失敗時會印出 F, 未完成時會印出 I, 省略跳過則會印出 S 5. 還會顯示執行秒數 , 使用記憶體大小 , 測試了多少方法