atoum / bdd-extension
atoum Spec BDD 扩展
Requires
- php: ^5.4.0 || ^7.0.0 || ^8.0.0
Requires (Dev)
- atoum/atoum: ^2.9 || ^3.0 || ^4.0
This package is auto-updated.
Last update: 2024-09-20 23:08:43 UTC
README
此扩展可以帮助您以 行为驱动开发 的方式编写测试(规范)。
示例
public function should_format_underscore_separated_method_name() { $this ->given($formatter = new testedClass()) ->then ->invoking->format(__FUNCTION__)->on($formatter) ->shouldReturn('should format underscore separated method name') ; }
输出将看起来像这样
安装它
使用 composer 安装扩展
composer require --dev atoum/bdd-extension
使用 atoum 配置文件启用扩展
<?php // .atoum.php require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; use atoum\atoum\bdd; $extension = new bdd\extension($script); $runner->addExtension($extension);
使用它
您的第一个规范
此扩展要求您将规范放置在 specs
命名空间中,并使其扩展 atoum\spec
。让我们为 jubianchi\example\formatter
类编写一个规范
<?php namespace jubianchi\example\specs; use atoum; use jubianchi\example\formatter as testedClass; class formatter extends atoum\spec { public function should_format_underscore_separated_method_name() { $this ->given($formatter = new testedClass()) ->then ->invoking->format(__FUNCTION__)->on($formatter) ->shouldReturn('should format underscore separated method name') ; } public function shouldFormatCamelCaseMethodName() { $this ->given($formatter = new testedClass()) ->then ->invoking->format(__FUNCTION__)->on($formatter) ->shouldReturn('should format camel case method name') ; } public function should_formatMixed__MethodName() { $this ->given($formatter = new testedClass()) ->then ->invoking->format(__FUNCTION__)->on($formatter) ->shouldReturn('should format mixed method name') ; } }
运行您的第一个规范
$ vendor/bin/atoum -d specs > jubianchi\example\formatter... ✘ should format camel case method name 256: Tested class 'jubianchi\example\formatter' does not exist for test class 'jubianchi\example\specs\formatter' File: /atoum-bdd-extension/formatter.php ✘ should format underscore separated method name 256: Tested class 'jubianchi\example\formatter' does not exist for test class 'jubianchi\example\specs\formatter' File: /atoum-bdd-extension/formatter.php ✘ should format mixed method name 256: Tested class 'jubianchi\example\formatter' does not exist for test class 'jubianchi\example\specs\formatter' File: /atoum-bdd-extension/formatter.php Failure (1 spec, 3/3 examples, 0 void example, 0 skipped example, 0 uncompleted example, 0 failure, 3 errors, 0 exception)!
然后再次运行它,直到您得到绿色
$ vendor/bin/atoum -d specs > jubianchi\example\formatter... ✔ should format underscore separated method name ✔ should format camel case method name ✔ should format mixed method name Success (1 spec, 3/3 examples, 0 void example, 0 skipped example, 6 assertions)!
您可以使用 --loop
标志逐步工作: vendor/bin/atoum -d specs --loop
阅读报告
规范报告使用一组图标来识别示例的状态
✘
用于标记失败的示例(错误、异常或断言失败)✔
用于标记通过的示例↣
用于标记跳过的示例∅
用于标记空示例(未实现的规范)
规范语法
调用方法
调用方法是在测试对象上调用方法的过程,以便测试人员可以断言方法的行为
<?php namespace jubianchi\example\specs; use atoum; use jubianchi\example\formatter as testedClass; class formatter extends atoum\spec { public function should_invoke_method() { $this ->given($formatter = new testedClass()) ->then ->invoking->format(__FUNCTION__)->on($formatter) // ... ; } }
此示例将使用一个参数 __FUNCTION__
调用 $formatter
对象的 format
方法。
根据您喜欢的语法,有多种方法可以调用方法
$this->invoking->format(__FUNCTION__)->on($formatter); $this->invoking('format', __FUNCTION__)->on($formatter); $this->invokingFormat(__FUNCTION__)->on($formatter);
断言返回值
规范扩展提供了一些快捷方式来断言方法返回值。
<?php namespace jubianchi\example\specs; use atoum; use jubianchi\example\formatter as testedClass; class formatter extends atoum\spec { public function should_format_underscore_separated_method_name() { $this ->given($formatter = new testedClass()) ->then ->invoking->format(__FUNCTION__)->on($formatter) ->shouldReturn('should format underscore separated method name') ; } }
如您所见,shouldReturn
允许您断言调用方法返回值。同样,有多种方式可以做到这一点
$this->invoking->format(__FUNCTION__)->on($formatter)->shouldReturn('...'); $this->invoking->format(__FUNCTION__)->on($formatter)->returns('...'); $this->invoking->format(__FUNCTION__)->on($formatter)->should->return('...');
使用参数调用 shouldReturn
将回退到 atoum\asserters\variable::isEqual
断言,这是 atoum 中最不严格的断言。您当然可以使用严格断言进行类型检查
$this->invoking->format(__FUNCTION__)->on($formatter)->shouldReturn->string->isEuqlaTo('...')
断言抛出的异常
断言异常相当简单,与断言返回值类似
<?php namespace jubianchi\example\specs; use atoum; use jubianchi\example\formatter as testedClass; class formatter extends atoum\spec { public function should_format_underscore_separated_method_name() { $this ->given($formatter = new testedClass()) ->then ->invoking->format(uniqid())->on($formatter) ->shouldThrow('invalidArgumentException') ; } }
对于 shouldReturn
,shouldThrow
提供了一些替代语法
$this->invoking->format(__FUNCTION__)->on($formatter)->shouldThrow('exception'); $this->invoking->format(__FUNCTION__)->on($formatter)->throws('exception'); $this->invoking->format(__FUNCTION__)->on($formatter)->should->throw('exception');
以这种方式调用 shouldThrow
将回退到 atoum\asserters\exception::isInstanceOf
,如果您不想检查异常类型,您可以简单地省略参数
$this->invoking->format(__FUNCTION__)->on($formatter)->shouldThrow; $this->invoking->format(__FUNCTION__)->on($formatter)->throws; $this->invoking->format(__FUNCTION__)->on($formatter)->should->throw;
当然,您也可以使用正则断言检查异常消息
$this->invoking->format(__FUNCTION__)->on($formatter)->shouldThrow->hasMessage('...'); $this->invoking->format(__FUNCTION__)->on($formatter)->throws('invalidArgumentException')->hasMessage('...');
链接
许可证
bdd-extension 在 BSD-3-Clause 许可下发布。有关详细信息,请参阅附带的有效许可文件。