stepapo / request-tester
Nette 网络应用程序的测试器。
0.4.0
2024-02-14 14:04 UTC
Requires
- php: >=8.1
- nette/application: ^3.2
- nette/http: ^3.2
- nette/routing: ^3.0
- nette/tester: ^2.5
README
Request Tester 是一个用于测试 Web 应用程序的 Nette 框架扩展。它根据请求的路径、登录的用户或通过表单提交的数据来验证渲染的内容。文档展示了基本示例并解释了配置测试的方法。
示例
让我们定义一个测试,用于提交位于 URL //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 测试器打印机来输出结果,而不是基本的 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 文件用于配置测试场景。它们可以分成以下部分。
测试
测试通过 name
和请求列表定义。
name: authorization requests: example request: # include Request configuration another example request: # include Request configuration
请求
请求配置需要 path
和 asserts
。使用 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