PHPUnit是一个面向PHP程序员的测试框架,这是一个xUnit的体系结构的单元测试框架。

复杂的项目,通过单元测试能够快速排查bug,有效减少bug的产生。简单的项目,使用php自带的var_dump()print_r()也能很方便的调试bug。

PHPUnit通过运行测试用例里的断言(例如判断返回结果不为空),检查代码是否符合预期。

安装

安装方式有两种。一种是使用phar包,一种是使用Composer。

1、使用phar包

下载地址 https://phpunit.de/

有三个版本:

PHPUnit 6.4 支持PHP 7.0, 和 PHP 7.1。(Current Stable Release)

PHPUnit 5.7 支持 PHP 5.6, PHP 7.0, 和 PHP 7.1。(Old Stable Release)

PHPUnit 4.8 支持PHP 5.3~5.6。(No Longer Supported)

运行方法:

# 通用
php phpunit.phar --version # linux
chmod +x phpunit.phar
sudo mv phpunit.phar /usr/local/bin/phpunit
phpunit --version

可以查看版本号。

2、使用Composer

如果用 Composer 来管理项目的依赖关系,只要在项目的composer.json 文件中简单地加上对 phpunit/phpunit 的依赖关系即可。下面是一个最小化的 composer.json 文件的例子,只定义了一个对 PHPUnit 5.7 的开发时依赖:

{
"require-dev": {
"phpunit/phpunit": "5.5.*"
}
}

要通过 Composer 完成系统级的安装,可以运行:

composer global require "phpunit/phpunit=5.5.*"

请确保 path 变量中包含有 ~/.composer/vendor/bin/

配置PhpStorm使用PHPUnit

1、点击File->Settings->Languages & Frameworks,点击php,设置PHP开发环境:

2、点击php->PHPUnitPHPUnit library里选中Path to phpunit.phar,指定路径,例如:D:\phpsetup\php\phpunit-5.7.4.phar

编写第一个测试用例

1、新建文件夹Testcase,编写SayHello.php:

<?php

class SayHello{

    public function printHello(){
echo 'Hello';
return 'Hello'; }
}
?>

2、新建测试用例SayHelloTest.php

<?php

require_once 'SayHello.php';

class SayHelloTest extends PHPUnit_Framework_TestCase {

    public function setUp(){ }

    public function tearDown(){ }

    public function testConnectionIsValid(){
$hi = new SayHello();
$this->assertTrue($hi->printHello() == 'Hello');
} }

编写完成后,切换到phpunit.phar所在目录命令行执行:

$ php phpunit.phar Testcase/SayHelloTest.php

输出结果:

PHPUnit 5.7.4 by Sebastian Bergmann and contributors.

.                                                                  1 / 1 (100%)Hello

Time: 130 ms, Memory: 10.00MB

OK (1 test, 1 assertion)

结果表明:

测试通过,1个测试方法,1个断言,没有失败。

这里注意的是:

1、所有以Test结尾的类均为测试用例;

2、所有以test开头的方法均是测试方法,会自动运行;

3、setUp是每个测试用例最先运行的方法,tearDown是每个测试用例最后运行的方法;

4、assertTrue用于判断结果是否为true。

如果用的PhpStorm,可以单击文件,右键Run SayHelloTest即可看到相同效果;也可以针对整个文件夹全部执行,选择文件夹Testcase右键Run Testcase即可。

ThinkPHP3.1集成PHPUnit

集成

需要修改的地方:

1、复制index.php为phpunit.php,内容增加:

define('APP_PHPUNIT', true);

示例:

<?php
define('APP_DEBUG', true);
header("Content-type: text/html; charset=utf-8");
//define('APP_PATH', './');
define('APP_PATH', __DIR__ .'/'); define('APP_PHPUNIT', true); require APP_PATH .'Core/ThinkPHP.php'; ?>

需要使用绝对路径。

2、修改ThinkPHP/Lib/Core/App.class.php

run()方法里App::exec()改为:

(APP_PHPUNIT !== true) && App::exec();//支持phpunit

3、ThinkPHP/Core/Lib/Core/增加AjaxReturnEvent.class.php:

<?php

class AjaxReturnEvent extends Exception {
}

4、修改ThinkPHP/Common/runtime.php:

将2处

CORE_PATH.'Core/Think.class.php',
CORE_PATH.'Core/ThinkException.class.php', // 异常处理类
CORE_PATH.'Core/Behavior.class.php',

改为:

CORE_PATH.'Core/Think.class.php',
CORE_PATH.'Core/ThinkException.class.php', // 异常处理类
CORE_PATH.'Core/Behavior.class.php',
CORE_PATH.'Core/AjaxReturnEvent.class.php',

记得是2处。load_runtime_file和build_runtime_cache方法都要修改。

5、修改ThinkPHP/Lib/Core/Action.class.php里ajaxReturn方法:

        switch (strtoupper($type)){
case 'JSON' :
// 返回JSON数据格式到客户端 包含状态信息
header('Content-Type:application/json; charset=utf-8');
if(APP_PHPUNIT === true){throw new AjaxReturnEvent(json_encode($data)); return;}//以支持phpunit捕获结果
exit(json_encode($data));
case 'XML' :
// 返回xml格式数据
header('Content-Type:text/xml; charset=utf-8');
if(APP_PHPUNIT === true){throw new AjaxReturnEvent(xml_encode($data)); return;}//以支持phpunit捕获结果
exit(xml_encode($data));
case 'JSONP':
// 返回JSON数据格式到客户端 包含状态信息
header('Content-Type:application/json; charset=utf-8');
$handler = isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER');
if(APP_PHPUNIT === true){throw new AjaxReturnEvent($handler.'('.json_encode($data).');'); return;}//以支持phpunit捕获结果
exit($handler.'('.json_encode($data).');');
case 'EVAL' :
// 返回可执行的js脚本
header('Content-Type:text/html; charset=utf-8');
if(APP_PHPUNIT === true){throw new AjaxReturnEvent($data); return;}//以支持phpunit捕获结果
exit($data);
default :
// 用于扩展其他返回格式数据
tag('ajax_return',$data);
}

主要做了2件事情:

1、支持phpunit测试模式;

2、防止ajaxReturn里的exit结束了程序。

示例

目录结构(示例程序采用了模块分组):

--example
|--Common
|--Conf
|--ThinkPHP
|--Lib
|--Action
| |--Weixin
| |--Api
| |--UserAction.class.php
|--Model |--Runtime
|--Testcase
|--Weixin
|--Api
|--UserAction.class.php
|--Tpl
|--vendor
|--index.php
|--phpunit.php

1、Model测试

<?php

define('TEST_PATH', dirname(dirname(__FILE__)));
require TEST_PATH .'/../phpunit.php'; class OrderTest extends PHPUnit_Framework_TestCase { public function testGetBillRule(){ $order_model = D('Orders');
$this->assertTrue(is_object($order_model) == true);
$this->assertNotEmpty($order_model->getBillRule());
} }

2、Api测试

<?php

define('TEST_APP', 'Api');
define('TEST_PATH', dirname(dirname(__FILE__)));
require TEST_PATH .'/../phpunit.php'; class OrderTest extends PHPUnit_Framework_TestCase { public function setUp() { //自动加载
spl_autoload_register (function ( $class ) {
include APP_PATH . 'Lib/Action/'.TEST_APP.'/' . $class . '.class.php' ;
});
} public function tearDown(){ } public function testModule(){
$obj = new OrderAction();
$this->assertTrue(is_array($obj->getBillRule()) == true);
} public function testApi(){
try{
$obj = new OrderAction();
$obj->getBillRule(); //由于TP里的ajaxReturn会使用exit结束程序,这里使用异常来得到返回的内容
}catch(AjaxReturnEvent $e){
$res = json_decode($e->getMessage(), treu);
$this->assertNotEmpty($res);
}
} }

参考

1、开始使用 PHPUnit – PHP测试框架

http://phpunit.cn/getting-started.html

2、web3d/TPUnit: ThinkPHP PHPUnit框架集成

https://github.com/web3d/TPUnit/

3、[PHP]PHPUnit安装配置及样例 | CoinIdea的技术分享博客

http://blog.coinidea.com/web开发/php-1088.html

4、《xUnit Test Patterns》学习笔记系列 - CoderZh - 博客园

http://www.cnblogs.com/coderzh/archive/2010/01/23/xUnit-Test-Patterns.html

(未完待续)

PHPUnit笔记的更多相关文章

  1. 【夯实PHP基础】PHPUnit -- PHP测试框架

    本文地址 分享提纲: 1.概述 2.安装 3.编写第一个测试用例 4.PHPUnit高级 5.参考 1.概述 1)[测试框架] 它是一款轻量级的PHP测试框架,是一个xUnit的体系结构的单元测试框架 ...

  2. phpunit实践笔记

    phpunit成为单元测试的代名词已成为共识, 但很多在实际编写测试过程中遇到的很多问题通过手册.网上搜索都很难找到相关资料, 大部分都得通过查看源代码和实践的代码经验解决.欢迎大家拍砖.(在此之前请 ...

  3. 第一次工作->笔记:在phpstrom2019上搭建phpunit单元测试环境,php环境使用docker

    前言:公司大佬让我开发一个工具,并合并到他的工具包中,使用的是github 说明:这里的php环境使用的是laradock.感兴趣的道友自行查找. 工具:php.phpstrom.phpunit.do ...

  4. 工作笔记:phpstrom、docker、phpunit进行单元测试

  5. phpunit学习 3:

    16:17 2015/12/11phpunit学习 3:单元测试的大概步骤是:编写待测试类,编写测试用例类,编写测试类,测试.1.如果你有多个类,多个测试类的test类,那么可以编写一个AllTest ...

  6. phpunit测试学习 1:一点简单的扼要有用的东西的总结 一点入门认识

    16:45 2015/12/8phpunit测试学习 1:一点简单的扼要有用的东西的总结  一点入门认识 具体的入门安装和入门实践请参照文中的推荐博客或网上其他博客推荐博客,我感觉这几篇博客写得很不错 ...

  7. laravel安装 笔记

    http://laod.cn/hosts/2015-google-hosts.html 谷歌FQIP laravel安装和设置流程 1安装composer , VirtualBox和Vagrant 下 ...

  8. 《Create Your own PHP Framework》笔记

    前言 大力推荐该教程:<Create Your own PHP Framework> Symfony的学习蛮累的,官方文档虽然很丰富,但是组织方式像参考书而不是指南,一些不错的指导性文档常 ...

  9. thinkphp5框架笔记(ing)

    重新整理下学习tp5手册的笔记.自己再好好看一次tp5的开发手册,学到哪里记到哪里. 0x01 安装 Composer安装 ThinkPHP5支持使用Composer安装 curl -sS https ...

随机推荐

  1. NodeJs之Path

    Path模块 NodeJs提供的Path模块,使得我们可以对文件路径进行简单的操作. API var path = require('path'); var path_str = '\\Users\\ ...

  2. Shell替换

    如果表达式中包含特殊字符,Shell 将会进行替换.例如,在双引号中使用变量就是一种替换,转义字符也是一种替换. #!/bin/bash a= echo -e "Value of a is ...

  3. 在ubuntu16.10 PHP测试连接MySQL中出现Call to undefined function: mysql_connect()

    1.问题: 测试php7.0 链接mysql数据库的时候发生错误: Fatal error: Uncaught Error: Call to undefined function mysqli_con ...

  4. .NET Core采用的全新配置系统[10]: 配置的同步机制是如何实现的?

    配置的同步涉及到两个方面:第一,对原始的配置文件实施监控并在其发生变化之后从新加载配置:第二,配置重新加载之后及时通知应用程序进而使后者能够使用最新的配置.要了解配置同步机制的实现原理,先得从认识一个 ...

  5. Java 经典入门(一)

    一.什么是 Java 技术?为何需要 Java? Java 是由 Sun Microsystems 在 1995 年首先发布的编程语言和计算平台.有许多应用程序和 Web 站点只有在安装 Java 后 ...

  6. FFmpeg + SoundTouch实现音频的变调变速

    本文使用FFmpeg + SoundTouch实现将音频解码后,进行变调变速处理,并将处理后的结果保存为WAV文件. 主要有以下内容: 实现一个FFmpeg的工具类,保存多媒体文件所需的解码信息 将解 ...

  7. 数据库 DML、DDL、DCL区别 .

    总体解释: DML(data manipulation language): 它们是SELECT.UPDATE.INSERT.DELETE,就象它的名字一样,这4条命令是用来对数据库里的数据进行操作的 ...

  8. MySQL常见面试题

    1. 主键 超键 候选键 外键 主 键: 数据库表中对储存数据对象予以唯一和完整标识的数据列或属性的组合.一个数据列只能有一个主键,且主键的取值不能缺失,即不能为空值(Null). 超 键: 在关系中 ...

  9. Spring MVC数据校验

    在web应用程序中,为了防止客户端传来的数据引发程序异常,常常需要对 数据进行验证.输入验证分为客户端验证与服务器端验证.客户端验证主要通过JavaScript脚本进行,而服务器端验证则主要通过Jav ...

  10. CORS简介

    现在请跟我做:在您的浏览器的地址栏中输入www.yhd.com并敲击回车.在网站内容全部加载完毕后,按F12打开浏览器的调试窗口.当切换到Sources页时,您会发现您当前所看到的一号店的页面是从多个 ...