f2 / asserty
1.0.6
2020-01-11 18:54 UTC
README
懒人单元测试。当事情简单时,它就完成了。
我开发这个工具是因为phpunit非常冗长,几乎所有你想要测试的事情都需要写很多代码。这可能适合大型组织,但许多项目实际上很小。
一个最小示例测试;
<?php
require('vendor/autoload.php');
// The simplest tests are for something "truthy":
F2\assert(file_exists(__DIR__.'/../composer.lock'), "Did you delete composer.lock?");
// Exceptions and errors can be tested inside a closure:
F2\expect(\Exception::class, function() {
$api = new SomeAPI();
}, "Expected an exception");
F2\expect(E_NOTICE, function() {
file_get_contents('file-that-does-not-exist');
});
安装
这是一个composer包... 使用--dev
以避免在生产环境中加载asserty。
# composer require --dev f2/asserty
编写测试
创建一个文件./tests/001-example-tests.php
<?php
require('vendor/autoload.php');
use function F2/asserty, F2/expect;
/**
* These will not cause errors and will not output anything
*/
// All are truthy
asserty(1 > 0);
asserty(is_string("Asserty is simple"));
asserty(![], "Empty arrays are falsey, so this is fine");
// Optional error description
asserty(true, 'Something is really wrong here...');
// Optional exit code
asserty(true, 'Something is really wrong here...', 10);
/**
* These will cause errors
*/
// Unless you specify null as the exit code, the tests will stop after the first error is encountered
asserty(false, "False is considered an error condition", null);
asserty([], "Empty arrays are falsey", null);
?>
运行./tests/中的所有测试
# ./vendor/bin/asserty
`
## Run a single test
php tests/001-my-tests.php
asserty(is_int(123));
asserty
## Quick example
Create a test.php file:
<?php require('vendor/autoload.php'); use function F2\asserty; asserty("A" > "O", "在这个领域;Alpha大于Omega");
Run the file:
$ php test.php [F2\asserty] 在这个领域,Alpha大于Omega
## Batch testing
f2/asserty comes with a command line tool for doing batch testing.
$ ./vendor/bin/asserty
The command will then look for a folder named 'tests/' in the current
directory and start iterating over all php files there.
$ ./vendor/bin/asserty [ERROR] tests/asserty-failed.php | [F2\asserty] 在这个领域,Alpha大于Omega [OK] tests/nothing-wrong.php | 干得好! [ERROR] tests/sub-folder/foo.php | PHP解析错误:在行3处遇到意外的文件结束符,位置在/var/www/fubber2/PACKAGES/asserty/tests/sub-folder/foo.php [F2\asserty] 出现了2个错误
## Exit Codes
Asserty will emit non-zero exit codes if tests fail, so with some
shell scripting you can easily automate stuff.
To execute a command if testing succeeds:
$ ./vendor/bin/asserty && echo "FOUL PLAY"
To execute a command if everything as perfect:
$ ./vendor/bin/asserty || echo "Good"