版本描述
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'); ?>
首次访问之后,生成以下目录结构:
在example站点中新建文件夹,命名为“Testcase”。
测试Model
创建HelloModel.class.php:
<?php</p> <p>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();
该区分站点运行与测试用例。
<?php define('APP_NAME', 'example'); define('APP_PATH', './../../example/'); define('APP_PHPUNIT', true); require('./../../ThinkPHP/ThinkPHP.php'); class TestSayHello extends PHPUnit_Framework_TestCase {</p> <pre><code>public function setUp() { } public function tearDown(){ } </code></pre> <p>}
在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效果:
在TestSayHello中加入测试用例:
public function testHelloAction() { $hello = new IndexAction(); $this->assertTrue($hello->index() == 'Hello'); }
运行效果
运行Test.php效果如下:
Test通过,至此给ThinkPHP加上了单元测试。
poisonbian 2016/05/10 14:57
照着试了下,发现model无法使用,Common目录下自定义的一些函数也没有加载。研究了一下,觉得在Think.class.php中加APP_PHPUNIT的判断不太合理,不如在App.class.php中,在这儿添加: !APP_PHPUNIT && App::exec();
nick 2015/12/22 14:49
非常感谢,用你的教程,成功调试tp3.1的版本!不过thinkphp.class.php,用的这种方式,才行 // 运行应用 if (APP_PHPUNIT != 'phpunit') App::run(); //!APP_PHPUNIT && App::run();
hujiulin 博主 2015/12/25 14:14
@ 不好意思,才看到您的留言,实际上有可能是您的宏定义没定义好。
nick 2016/01/06 18:25
你好,我又来了,使用使用phpunit测试的时候,反向tp从数据库获取数据出了问题 /** * 得到完整的数据表名 * @access public * @return string */ public function getTableName() { if(empty($this->trueTableName)) { $tableName = !empty($this->tablePrefix) ? $this->tablePrefix : ''; //这里我定义了xda_表前缀,但是tp没有加载貌似,使用的默认think_表前缀了,导致生成的sql表前缀有问题, 不能查出数据。。。 if(!empty($this->tableName)) { $tableName .= $this->tableName; }else{ $tableName .= parse_name($this->name); } $this->trueTableName = strtolower($tableName); } return (!empty($this->dbName)?$this->dbName.'.':'').$this->trueTableName; }
匿名 2016/01/21 15:33
@ 你在配置中是否配置了xda前缀~?
hujiulin 博主 2016/01/21 15:35
@ 你在conf.php配置中是否配置了xda前缀~?
lh 2016/03/01 11:10
tp3.2 用你的方式 在测试controller里的方法时D方法不能直接实例话model 比如Home/Model/UserModel.class.php 要D('Home/User') 才可以请问这是怎么回事,我是在命令行phpunit运行测试脚本
hujiulin 博主 2016/03/01 16:54
@ 你是否用了分组或者分模块特性?
poisonbian 2016/05/10 14:57
照着试了下,发现model无法使用,Common目录下自定义的一些函数也没有加载。研究了一下,觉得在Think.class.php中加APP_PHPUNIT的判断不太合理,不如在App.class.php中,在这儿添加: !APP_PHPUNIT && App::exec();
hujiulin 博主 2016/05/18 11:02
@ 多谢建议。
匿名 2017/06/21 21:18
😎 ❓
匿名 2018/08/14 16:10
@ 👿 👿 👿
匿名 2017/12/06 09:55
🙂 ❗
匿名 2018/08/06 10:43
💡 💡 💡 💡