habanero / spectre
简单的规格测试工具
v0.3.7
2023-12-12 09:29 UTC
Requires
- php: >=5.3.4
- habanero/clipper: ^0.2.6
- phpunit/php-code-coverage: ~2.0.10
- phpunit/php-text-template: ^1.2
- phpunit/phpunit-mock-objects: ^2.3
README
旨在以简单的方式编写和运行规格。快速。
- 类和函数的模拟支持*
- 使用PHPUnit进行代码覆盖率报告
- 不要与类纠缠!
- 保存为TAP或JSON
- 监控模式
如何做?
composer.json
{ "require-dev": { "habanero/spectre": "v0.2.0" } }
inc/sum.php
<?php function sum($a, $b) { return $a + $b; }
specs/sum-spec.php
<?php require 'inc/sum.php'; describe('sum()', function () { it('sums two numbers', function () { expect(sum(2, 2))->toBe(4); }); });
执行您的规格
$ vendor/bin/spectre specs
Running specs...
sum()
✓ sums two numbers ... OK
Done (0.0017s)
可用的匹配器
- toBe($value) — 严格相等比较(使用
===
) - toBeA($value) — 数据类型比较(使用
is_<type>()
) - toBeLike($value) — toEqual 的别名
- toEquals($value) — toEqual 的别名
- toBeGreaterThan($value) — 使用
>
操作符的比较 - toBeLessThan($value) — 使用
<
操作符的比较 - toBeAnInstanceOf($value) — toBeInstanceOf 的别名
- toBeInstanceOf($value) — 使用
instanceof
操作符的比较 - toBeEmpty() — 对
empty()
进行测试 - toBeTruthy() — 测试 truthy-values
- toBeFalsy() — 测试 falsy-values
- toBeArray() — 使用
is_array()
进行测试 - toBeBoolean() — toBeBool 的别名
- toBeBool() — 使用
is_bool()
进行测试 - toBeCallable() — 使用
is_callable()
进行测试 - toBeDouble() — 使用
is_double()
进行测试 - toBeFloat() — 使用
is_float()
进行测试 - toBeInt() — 使用
is_int()
进行测试 - toBeInteger() — 使用
is_integer()
进行测试 - toBeLong() — 使用
is_long()
进行测试 - toBeNull() — 使用
is_null()
进行测试 - toBeNumeric() — 使用
is_numeric()
进行测试 - toBeObject() — 使用
is_object()
进行测试 - toBeReal() — 使用
is_real()
进行测试 - toBeResource() — 使用
is_resource()
进行测试 - toBeScalar() — 使用
is_scalar()
进行测试 - toBeString() — 使用
is_string()
进行测试 - toHaveKey($value) — 使用
isset()
测试数组 - toHaveLength([$value]) — 使用
count()
和strlen()
进行测试 - toEndWith($value) — 测试尾部子串
- toStartWith($value) — 测试首部子串
- toContains($value) — toContain 的别名
- toContain($value) — 测试包含子串
- toEqual($value) — 弱相等比较(使用
==
) - toMatch($value) — 使用正则表达式测试字符串
- toPrint($value) — 测试缓冲子串(捕获包括/echo带有缓冲的内容)
- toThrow([$value]) — 测试异常,如果提供
$value
将测试instanceof
- toWarn([$value]) — 测试用户级别的错误、通知和警告的缓冲子串(不包括致命的)
自定义匹配器
要注册您自己的匹配器,您应该实现以下代码
\Spectre\Base::customMatchers('toBeCustomValue', function ($expected, $value) { // test $value against $this->expected // then return true or false // // or for custom messages: // // return array( // 'result' => $expected === $value, // 'negative' => "Expected '{subject}' {verb} '{value}', but it did not", // 'positive' => "Did not expect '{subject}' {verb} '{value}', but it did", // ); });
注意,任何额外的参数都将传递给 $value
参数之后。
模拟支持
自 0.3.0
版本以来,您可以使用相同的API模拟类和一些内置函数,有关更多示例,请参阅 spec/mock-spec.php。
简而言之,您可以如下模拟任何类或函数
// class $stub = spy($namespace, $className) ->methods('testMethod') ->getMock(); $stub->expects($callback = any()) ->method('testMethod') ->willReturn(42); // function $stub = fun($namespace, $function) ->expects($callback = any()) ->willReturn(42);
可用的约束
- any()
- never()
- atLeast($count)
- 至少一次执行()
- 执行一次()
- 精确执行($count)次
- 最多执行($count)次
- 在第($index)次执行
- 返回值($test)
- 返回值映射($test)
- 返回参数($index)
- 返回回调函数($fn)
- 返回自身()
- 抛出异常($e)
- 在连续调用时()
一些约束也是间谍,给定一个 $callback
引用,你可以稍后询问
- 是否已被调用()
- 获取调用次数()
- 获取调用次数总和()
CLI选项
不带参数运行类型 vendor/bin/spectre
以获取以下内容
Usage: vendor/bin/spectre [options] <folders|files> -h --help Display this help -w --watch Enables the watch mode -t --timeout Timeout in seconds for watch mode -c --coverage Enables code coverage instrumentation -f --filter Filter for executing specific tests by name -x --exclude Folders and files to exclude from coverage -o --output Custom filename for saving coverage report -r --reporter Default reporter for coverage. Options: JSON, TAP
示例
您几乎可以用几种方式混合所有参数,例如
$ vendor/bin/spectre specs -rTAP -c -xvendor -xspecs
$ vendor/bin/spectre ./specs /path/to/specs --coverage --exclude docs
$ vendor/bin/spectre $PWD/specs --output results.json