christophwurst/nextcloud_testing

基于PHPUnit的Nextcloud简单快捷的单元和集成测试框架

v1.0.0 2023-10-30 07:42 UTC

This package is auto-updated.

Last update: 2024-08-30 01:49:33 UTC


README

基于PHPUnit的Nextcloud 25+简单快捷的单元和集成测试框架

特性

  • 单元测试的最小setUp/tearDown开销
  • 简单的基于特质的机制,在集成测试后重置数据库
  • 基于PHPUnit

单元测试

使用TestCase作为测试用例的基类

<?php

use ChristophWurst\Nextcloud\Testing\TestCase;

class ControllerTest extends TestCase {

    … your test code …

}

集成测试

在测试用例中包含DatabaseTransaction特质,测试完成后数据库的更改将被回滚

<?php

use ChristophWurst\Nextcloud\Testing\DatabaseTransaction;
use ChristophWurst\Nextcloud\Testing\TestCase;

class ControllerTest extends TestCase {

    use DatabaseTransaction;

    … your test code …

}

Selenium测试

在测试用例中包含Selenium特质,并通过$this->webDriver访问WebDriver实例

<?php

use ChristophWurst\Nextcloud\Testing\Selenium;
use ChristophWurst\Nextcloud\Testing\TestCase;

class ControllerTest extends TestCase {

    use Selenium;

    public function testWithSelenium() {
        …

        $this->webDriver->get('http://localhost:8080/index.php/login');

        …
    }

}

此框架针对Sauce Labs作为测试后端,运行测试浏览器实例。因此,需要设置SAUCE_USERNAMESAUCE_ACCESS_KEY环境变量。SELENIUM_BROWSER允许您选择用于运行测试的浏览器。

测试用户

在测试用例中包含TestUser特质以方便生成新的测试用户。该特质将创建一个新的用户,具有随机的UID(包括冲突检测)。

<?php

use ChristophWurst\Nextcloud\Testing\TestCase;
use ChristophWurst\Nextcloud\Testing\TestUser;

class ControllerTest extends TestCase {

    use TestUser;

    public function testWithSelenium() {
        …

        $user = $this->createTestUser();

        …
    }

}

返回的用户类型为IUser。您可以使用$user->getUID()读取其UID。注意,用户在测试后不会被删除。