为模块 yii2 创建测试
Creating tests for modules yii2
您好,我是 yii2 测试的新手,我在 app/tests 中设置了测试环境和测试,并且工作正常。
现在我想为我的模块之一设置测试,它是 modules/v1 并且包含命令、组件、控制器和模型文件夹。
我已经使用 codecept bootstrap
在 modules/v1 中为测试创建了初始设置,并将其设置如下
codeception.yml
namespace: v1
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
settings:
bootstrap: /../_bootstrap.php
colors: true
memory_limit: 1024M
extensions:
enabled:
- Codeception\Extension\RunFailed
modules:
enabled:
- Yii2:
configFile: '/../../config/test.php'
tests/unit.suite.yml
actor: UnitTester
modules:
enabled:
- Asserts
- \v1\Helper\Unit
tests/functional.suite.yml
actor: FunctionalTester
modules:
enabled:
- Asserts
- \v1\Helper\Functional
tests/_bootstrap.php
<?php
define('YII_ENV', 'test');
defined('YII_DEBUG') or define('YII_DEBUG', true);
require_once(__DIR__ . '/../../../vendor/yiisoft/yii2/Yii.php');
require __DIR__ .'/../../../vendor/autoload.php';
现在我要测试的是 moduels/v1/commands/ 中的命令操作
ReportController.php
<?php
namespace app\modules\v1\commands;
class ReportController extends Controller
{
public function actionOverview()
{
echo 1;
}
}
在我的 tests\functional\commands\ReportControllerCest.php 中,我想 运行 来自 commands\ReportController.php 的操作概述,我正在这样做
<?php
namespace modules\v1\tests\functional\commands;
use v1\FunctionalTester;
use Yii;
class ReportControllerCest
{
public function _before(FunctionalTester $I)
{
}
public function _after(FunctionalTester $I)
{
}
// tests
public function tryToTest(FunctionalTester $I)
{
Yii::$app->createControllerByID('report')->run('overview');
}
}
它不工作并给我以下错误
1) ReportControllerCest: Try to test
Test tests\functional\commands\ReportControllerCest.php:tryToTest
[yii\base\InvalidRouteException] Unable to resolve the request: report/overview
#1 C:\xampp\htdocs\preroll\vendor\yiisoft\yii2\base\Controller.php:189
#2 C:\xampp\htdocs\preroll\modules\v1\tests\functional\commands\ReportControllerCest.php:20
#3 modules\v1\tests\functional\commands\ReportControllerCest->tryToTest
表示它不检查 commands/ReportController。php
问题。
1-这是应该如何使用单独的测试文件夹测试模块?如果是那么我做错了什么?如果不是,那么正确的方法是什么?
2-我如何测试命令(控制台命令)?
我将把我实现目标的工作留给遇到同样问题的其他人。
我在 Advanced Usage 中使用了 codeception Environments
并为控制台和 Web 应用程序设置不同的配置文件。
然后在你 运行 测试时使用 --env
codecept run --env console
您好,我是 yii2 测试的新手,我在 app/tests 中设置了测试环境和测试,并且工作正常。
现在我想为我的模块之一设置测试,它是 modules/v1 并且包含命令、组件、控制器和模型文件夹。
我已经使用 codecept bootstrap
在 modules/v1 中为测试创建了初始设置,并将其设置如下
codeception.yml
namespace: v1
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
settings:
bootstrap: /../_bootstrap.php
colors: true
memory_limit: 1024M
extensions:
enabled:
- Codeception\Extension\RunFailed
modules:
enabled:
- Yii2:
configFile: '/../../config/test.php'
tests/unit.suite.yml
actor: UnitTester
modules:
enabled:
- Asserts
- \v1\Helper\Unit
tests/functional.suite.yml
actor: FunctionalTester
modules:
enabled:
- Asserts
- \v1\Helper\Functional
tests/_bootstrap.php
<?php
define('YII_ENV', 'test');
defined('YII_DEBUG') or define('YII_DEBUG', true);
require_once(__DIR__ . '/../../../vendor/yiisoft/yii2/Yii.php');
require __DIR__ .'/../../../vendor/autoload.php';
现在我要测试的是 moduels/v1/commands/ 中的命令操作 ReportController.php
<?php
namespace app\modules\v1\commands;
class ReportController extends Controller
{
public function actionOverview()
{
echo 1;
}
}
在我的 tests\functional\commands\ReportControllerCest.php 中,我想 运行 来自 commands\ReportController.php 的操作概述,我正在这样做
<?php
namespace modules\v1\tests\functional\commands;
use v1\FunctionalTester;
use Yii;
class ReportControllerCest
{
public function _before(FunctionalTester $I)
{
}
public function _after(FunctionalTester $I)
{
}
// tests
public function tryToTest(FunctionalTester $I)
{
Yii::$app->createControllerByID('report')->run('overview');
}
}
它不工作并给我以下错误
1) ReportControllerCest: Try to test
Test tests\functional\commands\ReportControllerCest.php:tryToTest
[yii\base\InvalidRouteException] Unable to resolve the request: report/overview
#1 C:\xampp\htdocs\preroll\vendor\yiisoft\yii2\base\Controller.php:189
#2 C:\xampp\htdocs\preroll\modules\v1\tests\functional\commands\ReportControllerCest.php:20
#3 modules\v1\tests\functional\commands\ReportControllerCest->tryToTest
表示它不检查 commands/ReportController。php
问题。 1-这是应该如何使用单独的测试文件夹测试模块?如果是那么我做错了什么?如果不是,那么正确的方法是什么? 2-我如何测试命令(控制台命令)?
我将把我实现目标的工作留给遇到同样问题的其他人。
我在 Advanced Usage 中使用了 codeception Environments
并为控制台和 Web 应用程序设置不同的配置文件。
然后在你 运行 测试时使用 --env
codecept run --env console