maxvu / sliver
一个小巧的PHP测试框架。
2.0.0
2016-07-10 18:37 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-28 16:20:41 UTC
README
一个基于闭包的简单Test类。
快速开始
将 sliver 作为Composer依赖项引入
# composer require --dev maxvu/sliver
实例化 maxvu\sliver\Test,使用一个闭包来展示要测试的行为作为其唯一的构造参数。
<?php use maxvu\sliver\Test; $tests = [ (new Test( function ( $test ) { # ... } ))->name( '1 + 1 = 2' ); ]; ?>
使用 $test->assert() 捕获一个值,并使用以下任何方法对其应用期望
<?php $test->assert( $a )->like( $b ) # $a == $b $test->assert( $a )->eq( $b ) # $a === $b $test->assert( $a )->ne( $b ) # $a != $b $test->assert( $a )->gt( $b ) # $a > $b $test->assert( $a )->ge( $b ) # $a >= $b $test->assert( $a )->lt( $b ) # $a < $b $test->assert( $a )->le( $b ) # $a <= $b $test->assert( $a )->null( $b ) # $a === null $test->assert( $a )->true( $b ) # $a === true $test->assert( $a )->false( $b ) # $a === false $test->assert( $a )->truthy( $b ) # $a $test->assert( $a )->falsy( $b ) # !$a $test->assert( $a )->contains( $b ) # in_array( $b, $a ) OR # false !== strpos( strval( $a ), strval( $b ) ) $test->assert( $a )->matches( $b ) # preg_match( $b, $a ); ?>
将这些 Test 列表提供给 maxvu\sliver\Runner(例如 maxvu\sliver\ConsoleRunner)并对其调用 run()
<?php require 'vendor/autoload.php'; use maxvu\sliver\Test; use maxvu\sliver\ConsoleRunner; (new ConsoleRunner([ (new Test( function ( $test ) { $test->assert( 1 + 1 )->eq( 2 ); } ))->name( '1 + 1 = 2' ) ]))->show_passing( 1 )->run(); ?>
将每个测试的详细信息打印到控制台
$ php tests.php
1 + 1 = 2 [ OK ] (0.00045s)
1 / 1 tests passing (100.00%)
2 / 2 conditions passing (100.00%)
$