Unit testing 如何将模型包含到Yii单元测试文件中

Unit testing 如何将模型包含到Yii单元测试文件中,unit-testing,yii,phpunit,Unit Testing,Yii,Phpunit,我需要包含一个模型,这样我就可以在单元测试中创建一个新的SomeClass。我想它会自动为我这样做,但给了我一个错误 类SomeClass未找到这是因为您的类未自动加载到测试中 在您的应用程序文件夹中,您将有如下内容 protected/ containing protected application files tests/ containing tests for the application

我需要包含一个模型,这样我就可以在单元测试中创建一个新的SomeClass。我想它会自动为我这样做,但给了我一个错误


类SomeClass未找到这是因为您的类未自动加载到测试中

在您的应用程序文件夹中,您将有如下内容

 protected/                containing protected application files
      tests/                 containing tests for the application
         fixtures/           containing database fixtures
         functional/         containing functional tests
         unit/               containing unit tests
         report/             containing coverage reports
         bootstrap.php       the script executed at the very beginning
         phpunit.xml         the PHPUnit configuration file
         WebTestCase.php     the base class for Web-based functional tests
bootstrap.php
包含自动加载所有类(如index.php)的入口脚本;它看起来像这样

$yiit='path/to/yii/framework/yiit.php';
$config=dirname(__FILE__).'/../config/test.php';
require_once($yiit);
require_once(dirname(__FILE__).'/WebTestCase.php');
Yii::createWebApplication($config);
检查
路径/to/framework/yiit.php
是否配置为包含框架的正确目录

您需要将单元测试存储在受保护/测试/单元中;理想情况下,如果您正在测试模型,测试类将从
CDbTestCase
扩展

从运行的测试目录中运行类的测试

phpunit--unit/MyClassTest.php

您应该阅读指南中的测试章节,以充分理解这一点:

单元测试不必使用yii测试框架;当然,您可以使用自己的自动加载器和输入脚本设置
phpunit
,以仅加载特定类等(提高测试速度),但只有在您知道自己在做什么的情况下,才应该这样做;更容易使用Yii测试框架