PHP 测试的对象化方法。

0.3.1 2018-01-07 14:29 UTC

This package is not auto-updated.

Last update: 2024-09-12 07:16:04 UTC


README

PHP Version Latest Stable Version License Build Status

PHP 代码测试的对象化方法。

Tester 的目标是:易于学习、轻量级、面向对象和可扩展。

文档在此

安装

库以 composer 包的形式发布。

composer require --dev thomasnordahldk/tester

测试

测试通过创建 TestCase 类来定义。

class MyTestCase implements TestCase
{
    public function getDescription(): string
    {
        return "My new unit test";
    }
    
    public function run(Tester $tester): void
    {
        $tester->assert(true, "This assertion passes!");
        $tester->assert(false, "This assertion fails!");
    }
}

测试案例定义为 测试描述 和一个 运行测试方法

文档: 创建测试案例.

测试套件

测试套件通过 TestSuite 类定义,该类通过描述和 TestCase 类数组创建。

$unit_tests = new TestSuite("Unit tests", [new UserUnitTest, AddressUnitTest]);

文档: 测试套件.

运行测试

库自带了一个原生的测试运行器,它可以从命令行界面运行,并输出测试的摘要。

要运行的测试在根 composer 目录中的 test.php 文件中定义。该文件应返回一个测试套件数组。

# test.php
$unit_tests = new TestSuite("Unit tests", [new UserUnitTest, new AddressUnitTest]);

return [$unit_tests];
$composer-root/~ bin/tester

---------------------------------------------------------------------------
 - Unit tests - cases: 2
 --- Unit test of User ✔
 --- Unit test of Address ✔
---------------------------------------------------------------------------
success: 2, failure: 0, assertions: 8, time: 0.04s
---------------------------------------------------------------------------

有关原生测试运行器脚本的选项的全面描述

文档: 如何运行测试.

灵感来源

此库受测试库 mindplay-dk/testies 的启发。