美文网首页
Yii2下的Codeception测试-1准备工作

Yii2下的Codeception测试-1准备工作

作者: biaoqianwo | 来源:发表于2018-02-27 09:59 被阅读466次

    这是一个系列文章,分3部分:
    1、准备工作--Yii2高级模板的安装,以及编写一个RESTful
    2、测试工作--单元测试和API测试
    3、力争上游--覆盖率、基础概念、引用文献等

    这是第一部分
    测试之前我们有2件准备工作:一是安装Yii2高级模板,二是编写一个RESTful。

    安装

    目的是将yii2-app-advanced的测试跑起来。

    1、如果你已经安装了composer,可以跳到下一步。

    2、安装和配置Yii2高级模板,参看yii2-app-advanced的Install

    3、目前安装的Yii2.0.14,需要将composer.json中"codeception/verify": "~0.3.1",改为"codeception/verify": "~1.0.0",
    否则会出现[Error] Class 'PHPUnit_Framework_Assert' not found。别忘了执行composer update

    4、因为建议测试数据库和开发、正式环境分开。
    所以需要新建数据库yii2advanced_test,然后执行php yii_test migrate。每次有数据库的变更都要执行一次。这也是migrate的价值体现

    5、执行./vendor/bin/codecept run运行测试。参看Testing

    建立一个restful的演示

    目的是为测试准备一个方法和model,用于后面的测试。

    1、增加一个应用blog(也可以定义为api等),必看Adding more applications
    别忘了执行php init对blog进行安装。
    然后如果你的数据库配置有变化,请修改common/config/main-local.php的db。

    2、执行php yii migrate/create create_news_table,新建一个news表,在console/migrations/下面多了一个类似m180228_073146_create_news_table.php的文件,添加字段

      'code' => $this->string(32)->notNull()->unique(),
      'title' => $this->string()->notNull(),
      'content' => $this->text()->null(),
      'status' => $this->smallInteger()->notNull()->defaultValue(0),
      'created_at' => $this->integer()->notNull()->defaultValue(0),
      'created_by' => $this->integer()->notNull()->defaultValue(0),
      'updated_at' => $this->integer()->notNull()->defaultValue(0),
      'updated_by' => $this->integer()->notNull()->defaultValue(0),
    

    在common\models下新建News,也可以用gii工具生成。最简单的类似:

    namespace common\models;
    class News extends \yii\db\ActiveRecord
    { 
        public function rules()
        {
            return [
                [['code', 'title'], 'required'],
                [['content'], 'string'],
                [['status', 'created_at', 'created_by', 'updated_at', 'updated_by'], 'integer'],
                [['code'], 'string', 'max' => 32],
                [['title'], 'string', 'max' => 255],
                [['code'], 'unique'],
            ];
        }
    }
    

    别忘记执行php yii_test migrate

    3、遵照RESful规范新建news的CRUD,参看Rest快速入门
    1)、新建控制器:

    namespace blog\controllers;
    use yii\rest\ActiveController;
    class NewsController extends ActiveController
    {
        public $modelClass = 'common\models\News';
    }
    

    2)、配置URL规则,修改blog/config/main.php

    'urlManager' => [
                'enablePrettyUrl' => true,
                //'enableStrictParsing' => true,
                'showScriptName' => false,
                'rules' => [
                    ['class' => 'yii\rest\UrlRule', 'controller' => 'news'],
                ],
            ],
    

    3)、启用 JSON 输入和输出,修改blog/config/main.php

    'request' => [
                //'class' => '\yii\web\Request',
                'enableCookieValidation' => false,
                'parsers' => [
                    'application/json' => 'yii\web\JsonParser',
                ],
    ],
    'response' => [
                'format' => yii\web\Response::FORMAT_JSON,
                'charset' => 'UTF-8',
    ],
    

    4)、利用postman检查news的restful api。

    4、为了演示测试,我们在common下新建文件夹helpers,新建CustomString.php,新增一个方法。
    内容如下:

    namespace common\helpers;
    class CustomString
    {
        //生成一个code 
        public static function generateCode($length = 20, $prefix = '')
        {
            return $prefix . \Yii::$app->security->generateRandomString($length);
        }
    }
    

    并且,在common\models\news.php的rules方法后新增:

    public function beforeValidate()
        {
            if (empty($this->code)) {
                $this->code = CustomString::generateCode(10, 'n');
            }
            return parent::beforeValidate();
        }
    

    准备工作已经完成,这里到没有什么,就是Yii2的常规开发。

    下一篇:测试工作

    相关文章

      网友评论

          本文标题:Yii2下的Codeception测试-1准备工作

          本文链接:https://www.haomeiwen.com/subject/holpxftx.html