inquid/luya-testsuite

LUYA 模块和组件的测试用例和数据。让测试更轻松。

安装: 132

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分叉: 7

类型:luya-core

2.0.3 2021-05-30 16:24 UTC

README

Build Status Test Coverage Total Downloads Latest Stable Version Join the chat at https://gitter.im/luyadev/luya

提供 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");
}