pr0ggy/kase

此包已被废弃,不再维护。未建议替代包。

轻量级的PHP单元测试框架

0.4.1 2017-05-01 16:53 UTC

This package is not auto-updated.

Last update: 2021-06-12 09:58:21 UTC


README

PHP 5.6及更高版本的轻量级测试框架

Kase旨在创建一个PHP测试框架,具有以下两个主要目标

  1. 尽可能轻量级,同时保持实用性
  2. 提供可插入模块,以满足个人需求,而不使抽象和间接成为源代码开发者的负担。

Kase从Tape JavaScript测试框架中汲取设计灵感

  • 测试以简单的回调形式定义,测试用例之间没有共享状态
  • 将测试套件和测试用例描述作为显式的字符串给出,而不是依赖于测试用例类/函数命名约定

安装

composer require --dev pr0ggy/kase

创建新的Kase测试套件模板文件

./vendor/bin/kase create-suite [-d|--test-dir <test directory, default: /PROJECT/ROOT/tests>]
                               [--namespace <test file namespace>
                               <test file name, relative to test directory>

运行Kase

./vendor/bin/kase run [-c|--config <config file>]
                      [-d|--test-dir <test directory, default: ./>]
                      [-f|--file-pattern <test file pattern, default: '*.test.php'>]

示例配置文件

Kase可以利用用户定义的配置文件来自定义测试资源。示例配置文件可以在example文件夹中找到,其中包含选项的说明。

示例Kase测试套件

请注意,更实际的示例可以在examples文件夹中找到

<?php

namespace Acme;

use function Kase\runner;
use function Kase\test;
use function Kase\skip;
use function Kase\only;
// Kase includes the Kanta assertion library, but feel free to use any exception-based library
use Kanta\Validation as v;

return runner( 'Demo Test Suite',

    test('Test 1 Description', function () {
         v\assert([
             'that' => 'te'.'st',
             'satisfies' => v\is('test'),
             'orFailBecause' => 'string concat failed to produce "test"'
         ]);
    }),

    skip('Test 2 Description', function () {
        // Test is marked as skipped, so no failure will be recorded even though the test fails explicitly
  	    v\fail('This test was failed explicitly');
    }),

    only('Test 3 Description', function () {
    	  // This will be the only test that runs in this suite as the use of 'only' isolates it
        v\assert([
           'that' => true,
           'satisfies' => v\is(true),
           'orFailBecause' => "true isn't true.......hmm......."
        ]);
    })

);

测试套件/测试用例API

function Kase\runner($suiteDescription, ...$suiteTests)

主测试运行器生成函数,它接受测试套件名称和要执行的1个或多个测试用例。给定测试的测试套件作为可调用的返回,如上例所示。

function Kase\test($description, callable $testDefinition)

创建一个具有给定描述和可执行定义的测试用例,它将按顺序运行。测试定义是作为回调给出的,具有单个参数:用于测试用例内断言的验证器对象。

function Kase\skip($description, callable $testDefinition)

创建一个标准测试用例,当执行套件时将被跳过。

function Kase\only($description, callable $testDefinition)

创建一个将单独运行的测试用例(即,所有其他测试用例将被跳过)。在给定时间内,每个套件只能运行一个单独的测试,否则将抛出错误。

断言

Kase包括Kanta断言库,但您可以使用您喜欢的任何基于异常的验证/断言库。

测试Kase

./vendor/bin/phpunit

许可

MIT