bafs/testify

Testify让编写单元测试变得有趣。它拥有优雅的语法并保持简洁。

v1.0 2016-03-14 11:52 UTC

This package is auto-updated.

Last update: 2024-09-20 12:32:58 UTC


README

Testify是一个针对PHP 5.3+的微型单元测试框架。它追求优雅而非功能堆砌。测试代码不再是苦差事,而是再次变得有趣。

要求

  • 需要PHP 5.3+
  • 推荐使用Composer安装Testify(但也可以手动完成)

使用方法

以下是一个包含两个测试用例的测试套件的示例

require 'vendor/autoload.php';

use Math\MyCalc;
use Testify\Testify;

$tf = new Testify("MyCalc Test Suite");

$tf->beforeEach(function($tf) {
	$tf->data->calc = new MyCalc(10);
});

$tf->test("Testing the add() method", function($tf) {
	$calc = $tf->data->calc;

	$calc->add(4);
	$tf->assert($calc->result() == 14);

	$calc->add(-6);
	$tf->assertEquals($calc->result(), 8);
});

$tf->test("Testing the mul() method", function($tf) {
	$calc = $tf->data->calc;

	$calc->mul(1.5);
	$tf->assertEquals($calc->result(), 12);

	$calc->mul(-1);
	$tf->assertEquals($calc->result(), -12);
});

$tf();

文档

  • __construct( string $title ) - 构造函数
  • test( string $name, [Closure $testCase = null] ) - 添加测试用例。
  • before( Closure $callback ) - 在运行测试用例之前执行一次
  • after( Closure $callback ) - 在运行测试用例之后执行一次
  • beforeEach( Closure $callback ) - 对于每个测试用例,在运行之前执行
  • afterEach( Closure $callback ) - 对于每个测试用例,在运行之后执行
  • run( ) - 运行所有测试和前后函数。调用report()生成HTML报告页面
  • assert( boolean $arg, [string $message = ''] ) - assertTrue()方法的别名
  • assertTrue( boolean $arg, [string $message = ''] ) - 如果给定的表达式为真则通过
  • assertFalse( boolean $arg, [string $message = ''] ) - 如果给定的表达式为假则通过
  • assertEquals( mixed $arg1, mixed $arg2, string [string $message = ''] ) - 如果$arg1 == $arg2则通过
  • assertNotEquals( mixed $arg1, mixed $arg2, string [string $message = ''] ) - 如果$arg1 != $arg2则通过
  • assertSame( mixed $arg1, mixed $arg2, string [string $message = ''] ) - 如果$arg1 === $arg2则通过
  • assertNotSame( mixed $arg1, mixed $arg2, string [string $message = ''] ) - 如果$arg1 !== $arg2则通过
  • assertInArray( mixed $arg, array $arr, string [string $message = ''] ) - 如果$arg是$arr的元素则通过
  • assertNotInArray( mixed $arg, array $arr, string [string $message = ''] ) - 如果$arg不是$arr的元素则通过
  • pass( string [string $message = ''] ) - 无条件通过
  • fail( string [string $message = ''] ) - 无条件失败
  • report( ) - 生成测试套件状态的漂亮CLI或HTML5报告。由run()隐式调用
  • __invoke( ) - run()方法的别名