bafs / testify
Testify让编写单元测试变得有趣。它拥有优雅的语法并保持简洁。
v1.0
2016-03-14 11:52 UTC
Requires
- php: >=5.3.0
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()方法的别名