stepapo/url-tester

Nette 网络应用程序的测试工具。

0.4.0 2024-02-14 14:04 UTC

This package is auto-updated.

Last update: 2024-10-01 00:14:29 UTC


README

Request Tester 是一个用于测试网络应用程序的 Nette 框架扩展。它根据请求的路径、登录的用户或通过表单提交的数据验证渲染的内容。文档展示了基本示例并解释了配置测试的方法。

示例

让我们定义一个测试提交位于 //example.com/auth 的授权表单。

定义

使用 tests/config/authorization.neon 定义一个测试

name: authorization
path: auth
requests:
    with wrong name:
        form:
            name: signupForm
            post:
                login: WRONG_NAME
                password: secret1234
        asserts:
            renders: This login does not exist
    with wrong passowrd:  
        form:
            name: signupForm
            post:
                login: name@example.com
                password: WRONG_PASSWORD
        asserts:
            renders: The password is wrong
    with wrong address:
        path: auht
        asserts:
            httpCode: 404
    with correct input:
        form:
            name: signupForm
            post:
                login: name@example.com
                password: secret1234
        asserts:
            renders: Login successful

数据提供者

使用 tests/config.php 收集所有测试定义并将它们包含在 Nette Tester 的数据提供者中

require __DIR__ . '/../vendor/autoload.php';

$return = [];

foreach (Nette\Utils\Finder::findFiles('*.neon')->from(__DIR__ . '/config') as $file) {
    $config = (array) Nette\Neon\Neon::decode(Nette\Utils\FileSystem::read($file));
    $return[$config['name']] = $config;
}

return $return;

测试用例

创建 tests/PresenterTest.php 测试用例

$container = App\Bootstrap::bootForTests()->createContainer();

/**
 * @testCase
 * @dataProvider config.php
 */
class PresenterTest extends Stepapo\RequestTester\Tester\TestCase
{
}

$container->createInstance(PresenterTest::class, [Nette\Tester\Environment::loadData()])->run();

运行器设置

可以使用特定的 URL Tester 打印机来输出结果,而不是基本的 Nette Tester 打印机。为此,创建 tests/runner-setup.php

require __DIR__ . '/../vendor/autoload.php';

$runner->outputHandlers[] = new Stepapo\RequestTester\Tester\Printer(
    $runner,
    require __DIR__ . '/config.php',
);

运行

要运行测试,请使用标准的 Nette Tester 命令。确保测试数据库已准备就绪,并且临时文件夹已清理。

基本命令

$ tester tests

带有设置的命令

$ tester --setup tests/runner-setup.php -o none -c tests/php.ini --coverage tests/coverage.html --coverage-src app -j 8 --cider tests

配置

NEON 文件用于配置测试场景。它们可以分成以下几部分。

测试

测试由 namerequests 列表定义。

name: authorization
requests:
    example request: # include Request configuration    
    another example request: # include Request configuration

请求

请求配置需要 pathasserts。使用 identity 指定应该登录哪个用户。如果想要提交表单,请使用 form。可以使用 requests 指定子请求,这些子请求继承父请求配置,并在需要时覆盖其中的一些。

path: auth
identity: # include Identity configuration
form: # include Form configuration
asserts: # include Assert configuration
requests:
    example subrequest: # include Request configuration    
    another example subrequest: # include Request configuration

身份

登录用户的 id 是必需的。

id: 1
roles:
    - user
    - admin

表单

name: signupForm
post:
    login: name@domain.com
    password: secret1234

断言

验证无效请求

httpCode: 404

验证浏览器中渲染的内容

renders:
    - Login successful
notRenders:
    - Login required

验证 API 调用的结果

json:
    id: 1
    name: John Doe