如何编写行为类似于内置断言的自定义PHPUnit断言? [英] How to write custom PHPUnit assertion that behaves like built-in assertion?

查看:102
本文介绍了如何编写行为类似于内置断言的自定义PHPUnit断言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写自定义断言(如assertFoo($expected, $actual)),其行为类似于针对堆栈跟踪"错误的内置断言?

How can I write a custom assertion, like assertFoo($expected, $actual), that behaves like the built-in assertions with respect to the error "stack trace"?

我目前定义了以下方法(在扩展PHPUnit_Framework_TestCase的类中):

I currently have the following method defined (within a class that extends PHPUnit_Framework_TestCase):

public static function assertFoo($expected, $actual) {
    self::assertEquals($expected, $actual); 
}

如果我从测试中调用此方法,但测试失败,则会在调用堆栈中得到两个项目:

If I call this from a test and the test fails, I get two items in the call stack:

1) PreferencesTest::testSignupTeacher
Failed asserting that 5 matches expected 3.

/vagrant/myproject/tests/integration/PreferencesTest.php:17
/vagrant/myproject/tests/integration/PreferencesTest.php:136

第17行是assertFoo()调用内置assertEquals()并失败的地方;第136行被称为assertFoo().

Line 17 is where assertFoo() calls the built-in assertEquals() and fails; line 136 is there assertFoo() is called.

如果我将测试更改为直接调用assertEquals(),我只会得到一个:

If I change the test to call assertEquals() directly, I only get one:

1) PreferencesTest::testSignupTeacher
Failed asserting that 3 is true.

/vagrant/myproject/tests/integration/PreferencesTest.php:136

手册中有一些文档,但似乎无法涵盖这一点.

There's some documentation in the manual, but it doesn't seem to cover this.

推荐答案

我对问题的第一次猜测(您没有使用PHPUnit_Framework_Constraint_*对象和self::assertThat中的一个)是完全不相关的!实际的答案是phpUnit可以帮助过滤掉堆栈中其自己的代码库中的所有内容,并将函数留在用户空间中!

My first guess of the problem (that you're not using one of the PHPUnit_Framework_Constraint_* objects and self::assertThat) turned out to be completely irrelevant! The actual answer is that phpUnit helpfully filters away from the stack trace anything in its own codebase, and just leaves functions in user space!

执行此操作的代码可以在/path/to/PHPUnit/Util/Filter.php 中找到(其中/path/to//我的机器上的usr/share/php ),感兴趣的功能是getFilteredStacktraceisFiltered.

The code that does this can be found in /path/to/PHPUnit/Util/Filter.php (where /path/to/ is /usr/share/php on my machine) and the functions of interest are getFilteredStacktrace and isFiltered.

如果您想控制此行为,则将自定义断言放入从PHPUnit_Framework_TestCase派生的类中,然后从该类派生测试.在您的自定义类文件中,调用addFileToFilter某处,如下所示:

If you'd like to control this behaviour, then put your custom asserts into a class derived from PHPUnit_Framework_TestCase, then derive your tests from that class. In your custom class file put a call somewhere to addFileToFilter, as shown here:

class My_Base_TestCase extends PHPUnit_Framework_TestCase{
  public static function assertFoo($expected, $actual) {
    self::assertEquals($expected, $actual); 
  }
}

PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'DEFAULT');

然后在另一个文件中:

class CustomTest extends My_Base_TestCase{

  /** */
  public function testSomething2(){
    $this->assertFoo( 8,  5+4 );
  }
}

,它的行为就像内置的assertEquals().

and it will behave just like the built-in assertEquals().

免责声明:这是在使用未记录的行为!我将尝试找出这种机制是否可以合理地面向未来.

DISCLAIMER: This is using undocumented behaviour! I'll try and find out if this mechanism is going to be reasonably future-proof.

这篇关于如何编写行为类似于内置断言的自定义PHPUnit断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆