desarrolla2/test-bundle

此包为symfony2提供测试工具

安装: 24,452

依赖: 0

建议者: 0

安全: 0

星标: 1

关注者: 3

分支: 0

公开问题: 0

类型:symfony-bundle

2.1.1 2023-05-11 11:39 UTC

This package is auto-updated.

Last update: 2024-09-11 14:53:38 UTC


README

此包允许您使用symfony快速轻松地执行功能测试。

安装

最佳方式是通过在项目的composer.json中包含desarrolla2/test-bundle,并通过packagist安装。

    "require": {
        // ...
        "desarrolla2/test-bundle":  "*"
    }

用法

您只需扩展WebTestCase即可开始使用此包的功能。

use Desarrolla2\TestBundle\Functional\WebTestCase 

class ActivityTest extends WebTestCase

示例

以下是一些示例

  1. 发送请求并检查响应为200 HTML
class ActivityTest extends WebTestCase
{
    public function testIndex()
    {
        $client = $this->getClient();
        $user = $this->logIn($client, 'daniel@devtia.com', $this->getBackendRoles());

        $this->requestAndAssertOkAndHtml(
            $client,
            'GET',
            '_app.activity.index'
        );
    }        
}
  1. 填写表单,提交并检查返回301。当表单通过验证时,我们总是进行重定向。
class ProfileTest extends WebTestCase
{
    public function testChangePassword()
    {
        $client = $this->getClient();
        $user = $this->logIn($client, 'daniel@devtia.com', $this->getBackendRoles());

        $this->requestGetAndPostAndAssertRedirect(
            $client,
            '_app.profile.password',
            [],
            'form',
            ['password' => ['first' => 'changeM3', 'second' => 'changeM3']]
        );
    }        
}

如您所见,使用此包进行功能测试非常简单。继续探索它提供的所有实用功能。

报告

每次执行请求时,响应结果都存储在desarrolla2.request.latest中。如果您的phpunit配置在失败时停止,这里将以HTML格式显示失败的请求响应,这将有助于您更快地找到错误。

此包还提供了命令php bin / console phpunit: statistics --env = test,如果执行它,将显示

  1. 测试执行时间的摘要,请求次数,平均每请求次数和未测试的路由数量。
╰─$ php bin/console phpunit:statistics --env=test
+--------------------------+----------+------------+
| name                     | number   | percentage |
+--------------------------+----------+------------+
| Total execution time     | 1m23s    |            |
| Total requests           | 848      |            |
| Average time per request | 200ms    |            |
| Total routes             | 538      |            |
| Tested routes            | 470      | (87.36%)   |
| Pending routes           | 68       | (12.64%)   |
+--------------------------+----------+------------+
  1. 在文件desarrolla2.routes.tested.txt中,所有请求的详细信息以及执行每个请求所需的时间。
╰─$ cat var/logs/desarrolla2.routes.tested.txt
001. GET _api.activity.index ~0.152
   - /admin/api/activity/1 0.152
002. GET _api.contact.email ~0.168
   - /admin/api/contact/115148/email/daniel@devtia.com 0.168
003. GET _api.contact.phone ~0.128
   - /admin/api/contact/115148/phone/653965048 0.128
004. GET _api.service.index ~0.125
   - /admin/api/service/ 0.125
005. GET _api.sidebar.toggle ~0.091
   - /admin/api/sidebar/toggle 0.091
006. GET _app.activity.index ~2.533
[...]
  1. 在文件desarrolla2.tested.pending.txt中,未测试的路由的详细信息。
╰─$ cat var/logs/desarrolla2.routes.pending.txt
01. GET  _app.report.billing.payment
02. POST _app.transport.locator.view
03. GET  _app_admin_billing_invoice_batch
04. GET  _app_admin_contact_batch
05. GET  _app_admin_file_file_list
06. GET  _app_admin_participant_batch
07. GET  _public.contract.back
08. GET  _public.file.download
09. GET  _public.proposal.view
10. GET  _student.default.switch
[...]
  1. 在文件desarrolla2.classes.profile.txt中,执行测试的详细信息以及执行每个测试所需的时间。
╰─$ cat var/logs/desarrolla2.classes.profile.txt
PublicBundle\Functional\Api\ProvinceTest: 241ms
 - testIndex: 241ms
AppBundle\Functional\FosUserTest: 438ms
 - testDefault: 438ms
AppBundle\Functional\Admin\MailTest: 499ms
 - testDefault: 499ms
AppBundle\Functional\Admin\ConfigurationTest: 712ms
 - testDefault: 712ms
AppBundle\Functional\DefaultTest: 729ms
 - testIndex: 729ms
AppBundle\Functional\Admin\LanguageTest: 759ms
 - testDefault: 759ms
AppBundle\Functional\File\FileTest: 775ms
 - testCategory: 775ms
PublicBundle\Functional\Api\HealthCheckTest: 779ms
 - testIndex: 156ms
 - testSwagger: 623ms
[...]

联系

您可以通过@desarrolla2与我联系。