inquid / luya-testsuite
LUYA 模块和组件的测试用例和数据。让测试更轻松。
2.0.3
2021-05-30 16:24 UTC
Requires
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.x-dev
- dev-master / 1.0.x-dev
- 1.0.31
- 1.0.30
- 1.0.29
- 1.0.28
- 1.0.27
- 1.0.26
- 1.0.25.2
- 1.0.25.1
- 1.0.25
- 1.0.24
- 1.0.23
- 1.0.22
- 1.0.21
- 1.0.20.2
- 1.0.20.1
- 1.0.20
- 1.0.19
- 1.0.18
- 1.0.17.2
- 1.0.17.1
- 1.0.17
- 1.0.16
- 1.0.15
- 1.0.14
- 1.0.13.3
- 1.0.13.2
- 1.0.13.1
- 1.0.13
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2024-08-29 05:38:01 UTC
README
提供 PHPUnit 测试用例和内置 Web 服务器以测试您的应用程序、模块、组件、API 或类。
包含什么?
测试用例
- Web 应用程序测试用例
- 控制台应用程序测试用例
- 服务器(用于 API)测试用例
- CMS 块测试用例
- NgRest 测试用例(用于模型、控制器和 API)
特性
- 消息文件比较特性
- 迁移文件检查特性
固定值
- ActiveRecord 固定值在加载时基于数组或规则定义创建表。
查看完整的文档
安装
将 luyadev/luya-testsuite
包添加到 composer.json 文件中的 require-dev 部分
composer require luyadev/luya-testsuite:^2.0 --dev
在您的应用程序文件夹内创建一个名为 tests
的新文件夹并创建测试类
namespace app\tests; use Yii; class MyTest extends \luya\testsuite\cases\WebApplicationTestCase { public function getConfigArray() { return [ 'id' => 'mytestapp', 'basePath' => dirname(__DIR__), ]; } public function testInstance() { // add your phpunit tests here, like: $this->assertInstanceOf('luya\web\Application', Yii::$app); $this->assertInstanceOf('luya\base\Boot', $this->boot); $this->assertInstanceOf('luya\web\Application', $this->app); } }
要运行单元测试(假设您的测试在 tests/
目录中),请在终端中运行
./vendor/bin/phpunit tests/
为了支持 sqlite 固定值,请安装
sudo apt-get install php-sqlite3
示例测试用例
一些示例,说明如何在不同场景下使用 LUYA 测试套件。
测试 API 和应用程序
当与 API 或客户网站一起工作时,有时您只想测试网站本身,响应是什么,我的更新后所有页面是否仍然正常工作?因此我们提供了 luya\testsuite\cases\ServerTestCase
。
此示例将在 PHP Web 服务器中运行您的 LUYA 应用程序,并测试给定页面的一组响应状态代码或内容。要运行此示例,在 tests
文件夹中创建一个 MyWebsiteTest.php
文件。
namespace app\tests; class MyWebsiteTest extends ServerTestCase { public function getConfigArray() { return [ 'id' => 'mytestapp', 'basePath' => dirname(__DIR__), ]; } public function testSites() { $this->assertUrlHomepageIsOk(); // checks the root url like: https:///mywebsite.com $this->assertUrlIsOk('about'); // checks: https:///mywebsite.com/about $this->assertUrlGetResponseContains('about/me', 'Hello World'); // checks: https:///mywebsite.com/about/me $this->assertUrlIsError('errorpage'); // checks: https:///mywebsite.com/errorpage } }
由于 Web 服务器进程可能以不同的权限级别运行,您必须确保 assets/runtime 文件夹具有所需的权限。
控制器函数测试
我们使用 getMockBuilder()
(如PHPUnit 中的多个示例所示)来设置 DefaultController 并断言已注册的模块 addressbook
。为了测试由数据库错误引起的运行时异常,我们使用模拟 method
函数。
public function testActionIndex() { $module = Yii::$app->getModule('addressbook'); $this->assertInstanceOf('luya\addressbook\frontend\Module', $module); $mock = $this->getMockBuilder(DefaultController::class) ->setConstructorArgs(["id" => "default", "module" => $module]) ->getMock(); $mock->method("actionIndex"); }