Yii2中如何使用CodeCeption

 

前言:

Yii2是一款非常优秀的php框架,Yii2的官方发行版整合了Codeception测试框架。在使用Yii2框架的项目中,我们可以非常方便地利用Codeception进行单元测试、功能测试和验收测试。现在我们就利用Codeception在Yii2下实现简单的单元测试。

 

在进行单元测试前,需要做一些准备工作。

 

1. 首先确保你的机器安装了Composer,否则请自行安装,这是安装教程

 

2. 打开命令行,并切换目录到项目根目录,分别运行如下命令。

Cmmand代码  收藏代码
1
2
3
4
5
6
7
composer require "fxp/composer-asset-plugin:*" 
   
composer require "codeception/codeception=*" 
   
composer require "codeception/specify=*" 
   
composer require "codeception/verify=*" 

  

3.首先创建测试文件,在tests目录下面运行如下命令

1
..\vendor\bin\codecept generate:test unit \models\ExampleValidation 

结果如下

 ExampleValidationTest.php源码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php 
namespace tests\codeception\unit\models; 
   
use Yii; 
use app\models\DepartmentModel; 
use yii\codeception\TestCase; 
   
class ExampleTest extends TestCase 
    use \Codeception\Specify; 
       
    private $_dept
   
    /**
     * @var \UnitTester
     */ 
    protected $tester
   
    protected function _before() 
    
        $this->_dept = new DepartmentModel(); 
    
   
    protected function _after() 
    
    
   
    public function testValidation() 
    
        $this->specify('Department validation fail', function() { 
   
            /* 给dept_name赋一个重复的值,然后assertFalse */ 
            $this->_dept->dept_name = 'Biology'
            $this->assertFalse($this->_dept->validate()); 
        }); 
   
        $this->specify('Department validation pass', function() { 
   
            /* 给dept_name赋一个尚未重复的值,然后assertTrue */ 
             $this->_dept->dept_name = 'Math'
             $this->assertTrue($this->_dept->validate()); 
         }); 
    
   

  

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
 
namespace common\tests\unit\models;
 
class CollectionTest extends \Codeception\Test\Unit
{
    protected $tester;
 
 
    protected function _before()
    {
 
    }
 
    protected function _after()
    {
 
    }
 
    public function testCollection1()
    {
        $this->assertTrue(2 == 2);
    }
 
    public function testCollection2()
    {
        $this->assertTrue(1 > 2);
    }
 
}

  

然后在命令行测试结果如下

指定到类

1
D:\Project\PHP\yii2\common>..\vendor\bin\codecept run unit \models\CollectionTest

 

指定到方法

1
D:\Project\PHP\yii2\common>..\vendor\bin\codecept run unit \models\CollectionTest:testCollection1

 

显示有一个错误

 

注,_bootstrap.php 文件记得引入Yii的自动加载

1
2
3
4
5
6
7
8
<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');
defined('YII_APP_BASE_PATH') or define('YII_APP_BASE_PATH', __DIR__ . '/../../');
 
require_once(__DIR__ . '/../../vendor/autoload.php');
require_once(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../config/bootstrap.php');

  

 

参考:

Yii2框架下,使用Codeception进行单元测试:http://tangl163.iteye.com/blog/2288538

Yii2中如何使用CodeCeption:http://hustnaive.github.io/php/2015/06/16/work-with-yii-and-codeception.html

codeception (3)在yii2下创建Unit Tests (单元测试):https://segmentfault.com/a/1190000005926883

https://www.cnblogs.com/zergling9999/p/6052766.html

posted @   程序生(Codey)  阅读(1311)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!
点击右上角即可分享
微信分享提示