测试

未匹配的标注

测试

phpunit.xml是phpunit的配置文件。默认情况下,该文件只在 /tests/Feature/tests/Unit 中运行测试。

为了测试你的模块,需要将它们的路径添加到这个文件中,手动为每个模块添加条目是不可取的,所以使用通配符 * 动态地包含这些模块:

<testsuite name="Modules">
    <directory suffix="Test.php">./Modules/*/Tests/Feature</directory>
    <directory suffix="Test.php">./Modules/*/Tests/Unit</directory>
</testsuite>

还要确保你已经将数据库连接设置为 sqlite 并使用内存数据库:

<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>

phpunit.xml 示例

文件看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
        <testsuite name="Modules">
            <directory suffix="Test.php">./Modules/*/Tests/Feature</directory>
            <directory suffix="Test.php">./Modules/*/Tests/Unit</directory>
        </testsuite>
    </testsuites>
    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">./app</directory>
        </include>
    </coverage>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="sqlite"/>
        <server name="DB_DATABASE" value=":memory:"/>
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>

现在,当 phpunit 从模块运行测试时,全局的tests文件夹将运行。

运行测试

当你运行phpunit时:

vendor/bin/phpunit

所有测试都将运行。

运行单个测试

如果你只想运行一个测试方法、类或模块,可以使用filter标志:

vendor/bin/phpunit --filter 'contacts'

这将运行 Contacts 模块中的所有测试。

运行一个测试方法:

vendor/bin/phpunit --filter 'can_delete_contact'

这将匹配测试

/** @test */
public function can_delete_contact(): void
{
    $this->authenticate();
    $contact = Contact::factory()->create();
    $this->delete(route('app.contacts.delete', $contact->id))->assertRedirect(route('app.contacts.index'));

    $this->assertDatabaseCount('contacts', 0);
}

测试一个完整的测试文件:

vendor/bin/phpunit --filter 'ContactTest' 

使用 PestPHP

使用 PestPHP pestphp.com

你经常需要运行 Pest testCase 类,并在文件开头导入它。

uses(Tests\TestCase::class);

当使用 Pest 时,你不需要类,以下是一个有效的文件:

<?php

use Modules\Contacts\Models\Contact;

uses(Tests\TestCase::class);

test('can see contact list', function() {
    $this->authenticate();
    $this->get(route('app.contacts.index'))->assertOk();
});

test('can delete contact', function() {
    $this->authenticate();
    $contact = Contact::factory()->create();
    $this->delete(route('app.contacts.delete', $contact->id))->assertRedirect(route('app.contacts.index'));

    $this->assertDatabaseCount('contacts', 0);
});

运行测试套件:

vendor/bin/pest

如果你只想运行一个测试方法、类或模块,可以使用filter标志:

vendor/bin/pest --filter 'contacts'

这将匹配测试

test('can_delete_contact', function() {
    $this->authenticate();
    $contact = Contact::factory()->create();
    $this->delete(route('app.contacts.delete', $contact->id))->assertRedirect(route('app.contacts.index'));

    $this->assertDatabaseCount('contacts', 0);
});

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
slowlyo
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 0
发起讨论 查看所有版本


暂无话题~