• Yii2中如何使用CodeCeption


    前言:

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

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

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

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

    Cmmand代码  收藏代码
    composer require "fxp/composer-asset-plugin:*"  
      
    composer require "codeception/codeception=*"  
      
    composer require "codeception/specify=*"  
      
    composer require "codeception/verify=*"  
    

      

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

    ..vendorincodecept generate:test unit modelsExampleValidation  
    

    结果如下

     ExampleValidationTest.php源码如下

    <?php  
    namespace testscodeceptionunitmodels;  
      
    use Yii;  
    use appmodelsDepartmentModel;  
    use yiicodeceptionTestCase;  
      
    class ExampleTest extends TestCase  
    {  
        use CodeceptionSpecify;  
          
        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());  
             });  
        }  
      
    }  
    

      

    <?php
    
    namespace common	estsunitmodels;
    
    class CollectionTest extends CodeceptionTestUnit
    {
        protected $tester;
    
    
        protected function _before()
        {
    
        }
    
        protected function _after()
        {
    
        }
    
        public function testCollection1()
        {
            $this->assertTrue(2 == 2);
        }
    
        public function testCollection2()
        {
            $this->assertTrue(1 > 2);
        }
    
    }
    

      

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

    指定到类

    D:ProjectPHPyii2common>..vendorincodecept run unit modelsCollectionTest
    

    指定到方法

    D:ProjectPHPyii2common>..vendorincodecept run unit modelsCollectionTest:testCollection1
    

    显示有一个错误

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

    <?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

  • 相关阅读:
    工业级DTU无线数据传输终端
    4G DTU主要应用的场景
    4G DTU在油田远程监控中的应用
    模拟量采集模块哪个品牌好
    模拟量采集是什么?模拟量采集怎么应用?
    串行通信和串口通信有什么区别
    什么是模拟量,模拟量输出,模拟量输入
    嵌入式串口转以太网模块作用
    串口服务器和Modbus网关有什么不同
    SVN客户端的安装配置与使用
  • 原文地址:https://www.cnblogs.com/cxscode/p/8260227.html
Copyright © 2020-2023  润新知