ThinkPHP配置PHPUnit

参考链接: http://blog.coinidea.com/web%E5%BC%80%E5%8F%91/php-1096.html

版本描述

PHP: 5.3

PHPUnit

ThinkPHP 3.1.3

IDE: PHPStorm 10 (推荐)

PHPStorm配置PHPUnit

详见博文:

http://blog.coinidea.com/web%E5%BC%80%E5%8F%91/php-1088.html

ThinkPHP部署

官方代码下载:

http://www.thinkphp.cn/down.html

初始化站点:

http://www.thinkphp.cn/info/60.html

测试用例

本例中,根目录的index.php的配置如下:

<?php
define('APP_NAME', 'example');
define('APP_PATH', '../example/');
define('APP_PHPUNIT', false);
define('APP_DEBUG', true);
require('../ThinkPHP/ThinkPHP.php');
?>

首次访问之后,生成以下目录结构:

image

在example站点中新建文件夹,命名为“Testcase”。

测试Model

创建HelloModel.class.php:

<?php
 
class HelloModel extends Model
{
    public function sayHello()
    {
        print 'Hello';
        return 'Hello';
    }
}

在Test文件夹中新建Test.php文件作为PHPUnit,其中注意require ThinkPHP作为初始化框架环境,另外在Think.class.php中,修改

**start()****函数中,**App::run() !APP_PHPUNIT && App::run();

该区分站点运行与测试用例。

image

<?php
define('APP_NAME', 'example');
define('APP_PATH', './../../example/');
define('APP_PHPUNIT', true);
require('./../../ThinkPHP/ThinkPHP.php');
class TestSayHello extends PHPUnit_Framework_TestCase {
 
    public function setUp() { }
 
    public function tearDown(){ }
 
}
在TestSayHello中加入测试用例:

public function testHelloModel()
{
    $hello = D('Hello');
    $this->assertTrue( $hello->sayHello('Hello') == 'Hello');
}

测试Action 修改IndexAction.class.php如下:

<?php
class IndexAction extends Action
{
    public function index()
    {
        $hello = D("Hello");
        return $hello->sayHello();
    }
}

浏览器访问Index效果:

image

在TestSayHello中加入测试用例:

public function  testHelloAction()
{
    $hello = new IndexAction();
    $this->assertTrue($hello->index() == 'Hello');
}

运行效果

运行Test.php效果如下:

image

Test通过,至此给ThinkPHP加上了单元测试。

—————————–

照着试了下,发现model无法使用,Common目录下自定义的一些函数也没有加载。研究了一下,觉得在Think.class.php中加APP_PHPUNIT的判断不太合理,不如在App.class.php中,在这儿添加: !APP_PHPUNIT && App::exec();

参考链接: http://blog.coinidea.com/web%E5%BC%80%E5%8F%91/php-1096.html

转载于:https://my.oschina.net/u/1470240/blog/1934445

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值