funkatron/funit
Composer 安装命令:
composer require --dev funkatron/funit
包简介
The testing microframework for PHP 5.3+, partially inspired by QUnit.
README 文档
README
FUnit is a testing microframework for PHP 5.3+, partially inspired by QUnit. FUnit was created by Ed Finkler for Fictive Kin.
If you can code PHP, you can write tests with FUnit.
Features
- Simple to write tests and get output – start writing tests fast
- Short, straightforward syntax
- Can be run from the CLI – no web server required
- Fancy colored output in terminal
Example
<?php require __DIR__ . '/FUnit.php'; use \FUnit as fu; // note the alias to "fu" for terseness fu::test("this is a test", function() { fu::ok(1, "the integer '1' is okay"); fu::ok(0, "the integer '0' is not okay"); // this will fail! }); $exit_code = fu::run(); exit($exit_code);
Will output:
> php example_standalone.php
Running test 'this is a test...'
RESULTS:
--------------------------------------------
TEST: this is a test (1/2):
* PASS ok() the integer '1' is okay
* FAIL ok() the integer '0' is not okay
ERRORS/EXCEPTIONS: 0
TOTAL ASSERTIONS: 1 pass, 1 fail, 0 expected fail, 2 total
TESTS: 1 run, 0 pass, 1 total
See the example_standalone.php file for more, or try running it with php example_standalone.php
Alternately, if you load standalone_example.php in a web browser, it will output a very simple HTML version of the text report. If you're running PHP 5.4 or above, you can use the dev server to view it like so:
php -S 0.0.0.0:8000 example_standalone.php
And then open http://0.0.0.0:8000 in a web browser.
Methods
Test Suite Building Methods
-
FUnit::test($name, \Closure $test)
Add a test with the name $name and an anonymous function $test. $test would contain various assertions, likeFUnit::ok() -
FUnit::run($report = true, $filter = null, $report_format = 'text')
Runs the registered tests.$report(boolean): Iffalseis passed, the report output is suppressed.$filter(string): If this is passed, only tests that contain the$filterstring will be run.$report_format(string): Default is 'text'. Also accepts 'xunit'.
-
FUnit::setup(\Closure $setup)
Register a function to run at the start of each test. SeeFUnit::fixture() -
FUnit::teardown(\Closure $setup)
Register a function to run at the end of each test. SeeFUnit::fixture()andFUnit::reset_fixtures() -
FUnit::fixture($key, [$val])
Retrieve or register a fixture. Use this in FUnit::setup() to assign fixtures to keys, and retrieve those fixtures in your tests -
FUnit::reset_fixtures()
Clears out all fixtures in the FUnit::$fixtures array. This doesn't guarantee clean shutdown/close
Assertions
-
FUnit::ok($a, $msg = null)
Assert that $a is truthy. Optional $msg describes the test -
FUnit::not_ok($a, $msg = null)
Assert that $a is not truthy. Optional $msg describes the test -
FUnit::equal($a, $b, $msg = null)
Assert that $a == $b. Optional $msg describes the test -
FUnit::not_equal($a, $b, $msg = null)
Assert that $a != $b. Optional $msg describes the test -
FUnit::strict_equal($a, $b, $msg = null)
Assert that $a === $b. Optional $msg describes the test -
FUnit::not_strict_equal($a, $b, $msg = null)
Assert that $a !== $b. Optional $msg describes the test -
FUnit::has($needle, $haystack, $msg = null)
Assert that an array or object ($haystack) has a key or property ($needle) -
FUnit::not_has($needle, $haystack, $msg = null)
Assert that an array or object ($haystack) does not have a key or property ($needle). Forgive my grammar. -
FUnit::fail($msg = null, [$expected = null])
Force a failed assertion. If$expected === true, it's marked as an expected failure -
FUnit::expect_fail($msg = null)
Assets an expected failure. Equivalent toFUnit::fail('msg', true) -
FUnit::pass($msg = null)
Force a successful assertion. -
FUnit::throws($callback, $params, $exception = null, $msg = null)
Assert that$callbackthrows an exception of type$exception.$callbackmust be a callable -
FUnit::all_ok($a, $callback, $msg = null)
Iterate over all the items in$aand pass each to$callback. If the callback returnstruefor all, it passes -- otherwise it fails.$callbackmust be a callable
Utility Methods
-
FUnit::report($format = 'text')
Output the test report. If you've suppressed reporting output previously, you can use this to output the report manually. -
FUnit::exit_code()
Retrieve the exit code. If any test fails, the exit code will be set to1. Otherwise0. You can use this value to return a success or failure result with the PHP functionexit().
Configuration Methods
-
FUnit::set_disable_reporting($state)
If passedtrue, report will not be output after test runs finish. Re-enable by passingfalse. -
FUnit::set_debug($state)
If passedtrue, extra debugging info (including timing and details about assertion failures) will be output. Disable by passingfalse. -
FUnit::set_silence($state)
If passedtrue, only the report will be output -- no progress, debugging info, etc. Disable by passingfalse.
Report formats
By default, FUnit outputs a colorful text output, formatted for the terminal. You can also output reports in xunit-style xml.
The report format is the third parameter of FUnit::run():
Example:
// Outputs a colored text report. This is the default format. FUnit::run(true, null, 'text'); // Outputs xUnit-style xml FUnit::run(true, null, 'xunit');
Browser output
The standard text report format will output as very simple HTML if the test file is loaded up through a web server. You can test this with the dev server if you're running PHP 5.4+:
php -S 0.0.0.0:8000 test_file.php
And then open http://0.0.0.0:8000 in a web browser.
CLI Test Runner Utility
FUnit was designed to not require a separate test runner tool, but it does come with one at bin/fu (or vendor/bin/fu if you've installed via Composer). fu allows you to run tests in a single file, a group of files in a directory, and filter what tests are run.
Examples:
-
fu --help
Get detailed help and information on all options -
fu ./tests
Scan the directory./testsfor files that have "test(s)" in their names, and run those tests -
fu tests.php
Execute tests intests.php -
fu -d tests.php
Execute tests intests.phpwith additional debugging info -
fu --filter="API" tests.php
Execute only the tests in tests.php that have "API" in the name -
fu -s tests.php
Execute the tests tests.php, but suppress all output other than the report
Note: When fu loads multiple test files, it requires each one. That means all the code within each is executed. Calls to FUnit::run() are suppressed, but non-FUnit code (like exit() calls or require statements) could cause issues.
Installation
Install with Composer
If you're using Composer to manage dependencies, you can add FUnit with it.
{
"require": {
"funkatron/funit": "dev-master"
}
}
Note that FUnit has not yet reached 1.0! That means BC may break!
If you install via Composer, you can use the auto-generated autoloader to load FUnit, like so:
<?php require "vendor/autoload.php" use \FUnit as fu; fu::test("this is a test", function() { fu::ok(1, "the integer '1' is okay"); fu::ok(0, "the integer '0' is not okay"); // this will fail! }); fu::run();
Install source from GitHub
To install the source code:
git clone git://github.com/funkatron/FUnit.git
And include it in your scripts:
require_once '/path/to/FUnit/FUnit.php';
Install source from zip/tarball
Alternatively, you can fetch a tarball or zipball:
$ curl https://github.com/funkatron/FUnit/tarball/master | tar xzv
(or)
$ wget https://github.com/funkatron/FUnit/tarball/master -O - | tar xzv
Using a Class Loader
If you're using a class loader (e.g., Symfony Class Loader) for PSR-0-style class loading:
$loader->registerNamespace('FUnit', 'path/to/vendor/FUnit');
Upgrading
If you're using a version older than 0.5, the namespace/class name changed to follow PSR-0 autoloader standards. The base class is now \FUnit, not \FUnit\fu. You can still call all your methods with fu::XXX() by aliasing the namespace like so:
use \FUnit as fu
funkatron/funit 适用场景与选型建议
funkatron/funit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.89k 次下载、GitHub Stars 达 84, 最近一次更新时间为 2013 年 10 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「testing」 「test」 「Simple」 「unit」 「microphp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 funkatron/funit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 funkatron/funit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 funkatron/funit 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Testing Suite For Lumen like Laravel does.
A simple library for make Lexer and Parsers to build a language
A simple library that allows transform any kind of data to native php data or whatever
A simple library that uses the hydration pattern to fill objects given different sources of data.
Micro Active Record library in PHP, support chain calls, events, and relations.
The PHP SDK for Checkmango
统计信息
- 总下载量: 1.89k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 84
- 点击次数: 20
- 依赖项目数: 17
- 推荐数: 2
其他信息
- 授权协议: Apache
- 更新时间: 2013-10-14