SlideShare a Scribd company logo
1 of 29
PHP 单元测试

PHPUnit介绍以及使用示例


蓝燕光
2012-7-25
软件测试
单元测试

功能测试

集成测试

性能测试

系统测试

验收测试

回归测试

。。。。。。
单元测试
定义:验证程序中类方法和函数执行后的预期结果

好处:尽早发现错误,修正BUG
   自动化测试,提高效率
   单元测试用例本身就是一个demo
   给代码重构提供保障
   给程序设计提供思考

缺点:浪费时间?
   太麻烦?

范围: TDD(Test-Driven Development) 测试驱动开发
   开发时测试,测试功能预期结果正确
   调试时测试,回归测试,验证缺陷是否可以重现

职责:单元测试是开发人员该做的事
PHPUnit
PHPUnit是PHP单元测试框架,由 SEBASTIAN BERGMANN 开发维护。
目前最新版本3.7,稳定版本3.6

官方网站:http://www.phpunit.de/manual/current/en/index.html
GitHub: https://github.com/sebastianbergmann/phpunit
伯格曼个人网站:http://sebastian-bergmann.de/
PHPUnit 安装
PHPUnit 基于PEAR(PHP Extension and Application Repository)安装

注意:
PHPUnit 3.6 需要PHP 5.2.7及以上版本,推荐PHP 5.3.9及以上版本。
PHPUnit 3.6 PHP_CodeCoverage 模块依赖 Xdebug 2.0.5 及以上版本,
推荐Xdebug 2.1.3 及以上版本。

1、升级PEAR包
  pear upgrade PEAR

2、安装PHPUnit
  pear config-set auto_discover 1
  pear install  pear.phpunit.de/PHPUnit
PHPUnit 约定
1、测试类以Test结尾,例如类Stack的测试类为StackTest

2、测试类继承PHPUnit_Framework_TestCase

3、测试是公共方法,并且以test*命名

4、使用assertEquals()断言方法进行测试
PHPUnit 示例
测试一个测试用例
phpunit -v StackTest.php
PHPUnit 特性
Assertions 断言
Annotations 注释
Data Providers 数据提供商
Fixtures 装载器
Test Doubles 隔离测试
Database Testing 数据库测试
Assertions 断言

assertEquals(mixed $expected, mixed $actual[, string
  $message = ''])



   期望值                   实际结果               测试失败抛出的信息
Annotations 注释
注释在PHPUnit里面是做特殊用途的,有时候可以做反向代码生成.

这里的注释是指在程序中除了描述之外的另外功能.
其他使用注释作为代码辅助的项目
phpDocumentor 2
Symfony 2
eZ Components
Doctrine 2

/**
 * @test
 */
public function initialBalanceShouldBe0()
{
    $this->assertEquals(0, $this->ba->getBalance());
}
Test Dependencies 依赖测试
Data Providers 数据提供商




1、provider 必须是 public方法
2、provider 的返回集应该是一个数组或者可迭代的对象
3、不要依赖使用 provider 的方法
4、provider 在 setUp 方法触发之前执行
Testing Exceptions 异常测试




           使用注释
Testing Exceptions 异常测试




        使用setExpectedException
Testing Exceptions 测试异常
Testing PHP Errors 测试错误
Testing Output 测试输出




expectOutputRegex(string $regularExpression)

void expectOutputString(string $expectedString)

bool setOutputCallback(callable $callback)
命令行测试



. 表示成功
F 表示测试失败
E 表示测试错误
S 表示测试被跳过
I 表示测试未完成
命令行测试
Fixtures 装载器
Fixtures 装载器
 setUp()        测试函数执行前重置执行
 tearDown()


 setUpBeforeClass()     测试类执行前后出发
 tearDownAfterClass()


 assertPreConditions() 断言前后,测试失败不触发
 assertPostConditions()
Incomplete Tests 未完成测试




void markTestIncomplete()
void markTestIncomplete(string $message
Skipping Tests 跳过测试




void markTestSkipped()
void markTestSkipped(string $message)
Skeleton Generator 测试生成




通过 phpunit-skelgen 命令
可以自动生成测试文件
Skeleton Generator 测试生成




通过注释可以直接生成测试断言
PHPUnit XML 配置
Netbeans & PHPUnit &
Xdebug
 Netbeans 支持PHPUnit 测试代码生成
  和测试。
 Xdebug 用于统计单元测试覆盖率
目录结构

More Related Content

What's hot

Testing in Python @ Kaosiung.py 2014.05.26
Testing in Python @ Kaosiung.py 2014.05.26Testing in Python @ Kaosiung.py 2014.05.26
Testing in Python @ Kaosiung.py 2014.05.26Chun-Yu Tseng
 
認試軟體測試的世界 & TDD/BDD 入門
認試軟體測試的世界 & TDD/BDD 入門認試軟體測試的世界 & TDD/BDD 入門
認試軟體測試的世界 & TDD/BDD 入門wantingj
 
單元測試介紹
單元測試介紹單元測試介紹
單元測試介紹Adison wu
 
前端单元测试
前端单元测试前端单元测试
前端单元测试LC2009
 
持續整合與單元測試
持續整合與單元測試持續整合與單元測試
持續整合與單元測試昱劭 劉
 
测试快照
测试快照测试快照
测试快照jacquesqj
 
2012 China 软件测试大会
2012 China 软件测试大会2012 China 软件测试大会
2012 China 软件测试大会mayun1688
 
打造完全免費的,JAVA專案持續整合環境_ 2013 java developer_day_by 李書豪
打造完全免費的,JAVA專案持續整合環境_ 2013 java developer_day_by 李書豪打造完全免費的,JAVA專案持續整合環境_ 2013 java developer_day_by 李書豪
打造完全免費的,JAVA專案持續整合環境_ 2013 java developer_day_by 李書豪奕孝 陳
 
The way to continuous delivery
The way to continuous deliveryThe way to continuous delivery
The way to continuous deliveryQiao Liang
 
持续集成中的六步提交
持续集成中的六步提交持续集成中的六步提交
持续集成中的六步提交Qiao Liang
 
Static Code Analysis 靜態程式碼分析
Static Code Analysis 靜態程式碼分析Static Code Analysis 靜態程式碼分析
Static Code Analysis 靜態程式碼分析Bill Lin
 
图片管理Java迁移项目测试总结
图片管理Java迁移项目测试总结图片管理Java迁移项目测试总结
图片管理Java迁移项目测试总结Ryan YU
 
service-oriented agile team-Q con-beijing2012
service-oriented agile team-Q con-beijing2012service-oriented agile team-Q con-beijing2012
service-oriented agile team-Q con-beijing2012Qiao Liang
 
Phpunit入门 r2
Phpunit入门 r2Phpunit入门 r2
Phpunit入门 r2Baohua Cai
 
关于敏捷测试思想的分享Cici 20110826
关于敏捷测试思想的分享Cici 20110826关于敏捷测试思想的分享Cici 20110826
关于敏捷测试思想的分享Cici 20110826izhuzhume
 
2014/02: 嵌入式測試驅動開發
2014/02: 嵌入式測試驅動開發2014/02: 嵌入式測試驅動開發
2014/02: 嵌入式測試驅動開發AgileCommunity
 
TDD 介紹
TDD 介紹TDD 介紹
TDD 介紹williewu
 

What's hot (20)

Tip for Editors
Tip for EditorsTip for Editors
Tip for Editors
 
Testing in Python @ Kaosiung.py 2014.05.26
Testing in Python @ Kaosiung.py 2014.05.26Testing in Python @ Kaosiung.py 2014.05.26
Testing in Python @ Kaosiung.py 2014.05.26
 
認試軟體測試的世界 & TDD/BDD 入門
認試軟體測試的世界 & TDD/BDD 入門認試軟體測試的世界 & TDD/BDD 入門
認試軟體測試的世界 & TDD/BDD 入門
 
單元測試介紹
單元測試介紹單元測試介紹
單元測試介紹
 
前端单元测试
前端单元测试前端单元测试
前端单元测试
 
持續整合與單元測試
持續整合與單元測試持續整合與單元測試
持續整合與單元測試
 
测试快照
测试快照测试快照
测试快照
 
2012 China 软件测试大会
2012 China 软件测试大会2012 China 软件测试大会
2012 China 软件测试大会
 
Xpp
XppXpp
Xpp
 
打造完全免費的,JAVA專案持續整合環境_ 2013 java developer_day_by 李書豪
打造完全免費的,JAVA專案持續整合環境_ 2013 java developer_day_by 李書豪打造完全免費的,JAVA專案持續整合環境_ 2013 java developer_day_by 李書豪
打造完全免費的,JAVA專案持續整合環境_ 2013 java developer_day_by 李書豪
 
The way to continuous delivery
The way to continuous deliveryThe way to continuous delivery
The way to continuous delivery
 
Yii phpunit
Yii phpunitYii phpunit
Yii phpunit
 
持续集成中的六步提交
持续集成中的六步提交持续集成中的六步提交
持续集成中的六步提交
 
Static Code Analysis 靜態程式碼分析
Static Code Analysis 靜態程式碼分析Static Code Analysis 靜態程式碼分析
Static Code Analysis 靜態程式碼分析
 
图片管理Java迁移项目测试总结
图片管理Java迁移项目测试总结图片管理Java迁移项目测试总结
图片管理Java迁移项目测试总结
 
service-oriented agile team-Q con-beijing2012
service-oriented agile team-Q con-beijing2012service-oriented agile team-Q con-beijing2012
service-oriented agile team-Q con-beijing2012
 
Phpunit入门 r2
Phpunit入门 r2Phpunit入门 r2
Phpunit入门 r2
 
关于敏捷测试思想的分享Cici 20110826
关于敏捷测试思想的分享Cici 20110826关于敏捷测试思想的分享Cici 20110826
关于敏捷测试思想的分享Cici 20110826
 
2014/02: 嵌入式測試驅動開發
2014/02: 嵌入式測試驅動開發2014/02: 嵌入式測試驅動開發
2014/02: 嵌入式測試驅動開發
 
TDD 介紹
TDD 介紹TDD 介紹
TDD 介紹
 

Viewers also liked

持续集成入门
持续集成入门持续集成入门
持续集成入门Lieping Xie
 
How effective is the combination of your main
How effective is the combination of your mainHow effective is the combination of your main
How effective is the combination of your mainAlice Humphries
 
Contoh edit modul
Contoh edit modulContoh edit modul
Contoh edit modulHasyim Nara
 
Evaluation of hybrid library marketing strategies
Evaluation of hybrid library marketing strategiesEvaluation of hybrid library marketing strategies
Evaluation of hybrid library marketing strategiesAgus Rusmana
 
Mike hansen theory
Mike hansen theoryMike hansen theory
Mike hansen theoryMikeHansen18
 
autoimmune diseases in pregnancy
autoimmune diseases in pregnancy autoimmune diseases in pregnancy
autoimmune diseases in pregnancy Abdullah Abdurrhman
 
Protein structure
Protein structureProtein structure
Protein structurenidhiinjbp
 
Botany an overview
Botany an overviewBotany an overview
Botany an overviewnidhiinjbp
 
RSpec 讓你愛上寫測試
RSpec 讓你愛上寫測試RSpec 讓你愛上寫測試
RSpec 讓你愛上寫測試Wen-Tien Chang
 
Agile meetup - user story mapping workshop
Agile meetup - user story mapping workshopAgile meetup - user story mapping workshop
Agile meetup - user story mapping workshopJen-Chieh Ko
 
Protein synthesis
Protein synthesisProtein synthesis
Protein synthesisnidhiinjbp
 

Viewers also liked (18)

持续集成入门
持续集成入门持续集成入门
持续集成入门
 
How effective is the combination of your main
How effective is the combination of your mainHow effective is the combination of your main
How effective is the combination of your main
 
Contoh edit modul
Contoh edit modulContoh edit modul
Contoh edit modul
 
Evaluation of hybrid library marketing strategies
Evaluation of hybrid library marketing strategiesEvaluation of hybrid library marketing strategies
Evaluation of hybrid library marketing strategies
 
The moderns
The modernsThe moderns
The moderns
 
A gift of a friend
A gift of a friendA gift of a friend
A gift of a friend
 
Mike hansen theory
Mike hansen theoryMike hansen theory
Mike hansen theory
 
Advor profile
Advor profileAdvor profile
Advor profile
 
Organizac 120417231150-phpapp01
Organizac 120417231150-phpapp01Organizac 120417231150-phpapp01
Organizac 120417231150-phpapp01
 
Llama
LlamaLlama
Llama
 
Organizac 120417231150-phpapp01
Organizac 120417231150-phpapp01Organizac 120417231150-phpapp01
Organizac 120417231150-phpapp01
 
autoimmune diseases in pregnancy
autoimmune diseases in pregnancy autoimmune diseases in pregnancy
autoimmune diseases in pregnancy
 
Protein structure
Protein structureProtein structure
Protein structure
 
Chapter 2 genpsych
Chapter 2 genpsychChapter 2 genpsych
Chapter 2 genpsych
 
Botany an overview
Botany an overviewBotany an overview
Botany an overview
 
RSpec 讓你愛上寫測試
RSpec 讓你愛上寫測試RSpec 讓你愛上寫測試
RSpec 讓你愛上寫測試
 
Agile meetup - user story mapping workshop
Agile meetup - user story mapping workshopAgile meetup - user story mapping workshop
Agile meetup - user story mapping workshop
 
Protein synthesis
Protein synthesisProtein synthesis
Protein synthesis
 

Similar to PHP 单元测试

有效的单元测试.ppt
有效的单元测试.ppt有效的单元测试.ppt
有效的单元测试.ppttest499702
 
Web testing automation
Web testing automationWeb testing automation
Web testing automationkuozui
 
如何在實務上使用TDD來開發 twmvc#12
如何在實務上使用TDD來開發 twmvc#12如何在實務上使用TDD來開發 twmvc#12
如何在實務上使用TDD來開發 twmvc#12twMVC
 
twMVC#12 | 如何在實務上使用 TDD 來開發
twMVC#12 | 如何在實務上使用 TDD 來開發twMVC#12 | 如何在實務上使用 TDD 來開發
twMVC#12 | 如何在實務上使用 TDD 來開發twMVC
 
PHPUnit slide formal
PHPUnit slide formalPHPUnit slide formal
PHPUnit slide formaljameslabs
 
打造面向服务的敏捷团队 Q con-beijing2012
打造面向服务的敏捷团队 Q con-beijing2012打造面向服务的敏捷团队 Q con-beijing2012
打造面向服务的敏捷团队 Q con-beijing2012Qiao Liang
 
Top100summit automan x之框架介绍 王超
Top100summit automan x之框架介绍 王超Top100summit automan x之框架介绍 王超
Top100summit automan x之框架介绍 王超drewz lin
 
從理想、到現實的距離,開啟品味軟體測試之路 - 台灣軟體工程協會 (20220813)
從理想、到現實的距離,開啟品味軟體測試之路 - 台灣軟體工程協會 (20220813)從理想、到現實的距離,開啟品味軟體測試之路 - 台灣軟體工程協會 (20220813)
從理想、到現實的距離,開啟品味軟體測試之路 - 台灣軟體工程協會 (20220813)Rick Hwang
 
敏捷测试中的工具实现
敏捷测试中的工具实现敏捷测试中的工具实现
敏捷测试中的工具实现drewz lin
 
Qa engineer training
Qa engineer trainingQa engineer training
Qa engineer trainingychw365
 
Top100summit 宗刚-全生命周期性能评估体系的实践
Top100summit 宗刚-全生命周期性能评估体系的实践Top100summit 宗刚-全生命周期性能评估体系的实践
Top100summit 宗刚-全生命周期性能评估体系的实践drewz lin
 
使用 Pytest 進行單元測試 (PyCon TW 2021)
使用 Pytest 進行單元測試 (PyCon TW 2021)使用 Pytest 進行單元測試 (PyCon TW 2021)
使用 Pytest 進行單元測試 (PyCon TW 2021)Max Lai
 
同济优秀课程设计 - 软件测试报告
同济优秀课程设计 - 软件测试报告同济优秀课程设计 - 软件测试报告
同济优秀课程设计 - 软件测试报告Kerry Zhu
 
大话Php之性能
大话Php之性能大话Php之性能
大话Php之性能liqiang xu
 
分布式系统测试实践
分布式系统测试实践分布式系统测试实践
分布式系统测试实践drewz lin
 
Continuous integration
Continuous integrationContinuous integration
Continuous integrationnetdbncku
 
单元测试(H2等)和持续集成(Hudson)实战简介
单元测试(H2等)和持续集成(Hudson)实战简介单元测试(H2等)和持续集成(Hudson)实战简介
单元测试(H2等)和持续集成(Hudson)实战简介isxylands
 
移动开发敏捷实践
移动开发敏捷实践移动开发敏捷实践
移动开发敏捷实践Yuan Mai
 
Angular Testing for Book Club Online of Angular Taiwan
Angular Testing for Book Club Online of Angular TaiwanAngular Testing for Book Club Online of Angular Taiwan
Angular Testing for Book Club Online of Angular Taiwan志龍 陳
 

Similar to PHP 单元测试 (20)

有效的单元测试.ppt
有效的单元测试.ppt有效的单元测试.ppt
有效的单元测试.ppt
 
Web testing automation
Web testing automationWeb testing automation
Web testing automation
 
如何在實務上使用TDD來開發 twmvc#12
如何在實務上使用TDD來開發 twmvc#12如何在實務上使用TDD來開發 twmvc#12
如何在實務上使用TDD來開發 twmvc#12
 
twMVC#12 | 如何在實務上使用 TDD 來開發
twMVC#12 | 如何在實務上使用 TDD 來開發twMVC#12 | 如何在實務上使用 TDD 來開發
twMVC#12 | 如何在實務上使用 TDD 來開發
 
PHPUnit slide formal
PHPUnit slide formalPHPUnit slide formal
PHPUnit slide formal
 
打造面向服务的敏捷团队 Q con-beijing2012
打造面向服务的敏捷团队 Q con-beijing2012打造面向服务的敏捷团队 Q con-beijing2012
打造面向服务的敏捷团队 Q con-beijing2012
 
Top100summit automan x之框架介绍 王超
Top100summit automan x之框架介绍 王超Top100summit automan x之框架介绍 王超
Top100summit automan x之框架介绍 王超
 
從理想、到現實的距離,開啟品味軟體測試之路 - 台灣軟體工程協會 (20220813)
從理想、到現實的距離,開啟品味軟體測試之路 - 台灣軟體工程協會 (20220813)從理想、到現實的距離,開啟品味軟體測試之路 - 台灣軟體工程協會 (20220813)
從理想、到現實的距離,開啟品味軟體測試之路 - 台灣軟體工程協會 (20220813)
 
敏捷测试中的工具实现
敏捷测试中的工具实现敏捷测试中的工具实现
敏捷测试中的工具实现
 
UnitTest.pptx
UnitTest.pptxUnitTest.pptx
UnitTest.pptx
 
Qa engineer training
Qa engineer trainingQa engineer training
Qa engineer training
 
Top100summit 宗刚-全生命周期性能评估体系的实践
Top100summit 宗刚-全生命周期性能评估体系的实践Top100summit 宗刚-全生命周期性能评估体系的实践
Top100summit 宗刚-全生命周期性能评估体系的实践
 
使用 Pytest 進行單元測試 (PyCon TW 2021)
使用 Pytest 進行單元測試 (PyCon TW 2021)使用 Pytest 進行單元測試 (PyCon TW 2021)
使用 Pytest 進行單元測試 (PyCon TW 2021)
 
同济优秀课程设计 - 软件测试报告
同济优秀课程设计 - 软件测试报告同济优秀课程设计 - 软件测试报告
同济优秀课程设计 - 软件测试报告
 
大话Php之性能
大话Php之性能大话Php之性能
大话Php之性能
 
分布式系统测试实践
分布式系统测试实践分布式系统测试实践
分布式系统测试实践
 
Continuous integration
Continuous integrationContinuous integration
Continuous integration
 
单元测试(H2等)和持续集成(Hudson)实战简介
单元测试(H2等)和持续集成(Hudson)实战简介单元测试(H2等)和持续集成(Hudson)实战简介
单元测试(H2等)和持续集成(Hudson)实战简介
 
移动开发敏捷实践
移动开发敏捷实践移动开发敏捷实践
移动开发敏捷实践
 
Angular Testing for Book Club Online of Angular Taiwan
Angular Testing for Book Club Online of Angular TaiwanAngular Testing for Book Club Online of Angular Taiwan
Angular Testing for Book Club Online of Angular Taiwan
 

PHP 单元测试