assertequals() php,assertEquals()

assertEquals()

assertEquals(mixed $expected, mixed $actual[, string $message = ”])

当两个变量 $expected 和 $actual 不相等时报告错误,错误讯息由 $message 指定。

assertNotEquals() 是与之相反的断言,接受相同的参数。

assertAttributeEquals() 和 assertAttributeNotEquals() 是便捷包装(convenience wrapper),以某个类或对象的某个 public、protected 或 private 属性作为实际值来进行比较。

Example A.13. assertEquals() 的用法

<?php class EqualsTest extends PHPUnit_Framework_TestCase { public function testFailure() { $this->assertEquals(1, 0); } public function testFailure2() { $this->assertEquals('bar', 'baz'); } public function testFailure3() { $this->assertEquals("foonbarnbazn", "foonbahnbazn"); } } ?>

phpunit EqualsTest PHPUnit 5.0.0 by Sebastian Bergmann and contributors. FFF Time: 0 seconds, Memory: 5.25Mb There were 3 failures: 1) EqualsTest::testFailure Failed asserting that 0 matches expected 1. /home/sb/EqualsTest.php:6 2) EqualsTest::testFailure2 Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'bar' +'baz' /home/sb/EqualsTest.php:11 3) EqualsTest::testFailure3 Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ 'foo -bar +bah baz ' /home/sb/EqualsTest.php:16 FAILURES! Tests: 3, Assertions: 3, Failures: 3.

如果 $expected 和 $actual 是某些特定的类型,将使用更加专门的比较方式,参阅下文。

assertEquals(float $expected, float $actual[, string $message = ”, float $delta = 0])

当两个浮点数 $expected 和 $actual 之间的差值(的绝对值)大于 $delta 时报告错误,错误讯息由 $message 指定。

关于为什么 $delta 参数是必须的,请阅读《关于浮点运算,每一位计算机科学从业人员都应该知道的事实》。

Example A.14. 将assertEquals()用于浮点数时的用法

<?php class EqualsTest extends PHPUnit_Framework_TestCase { public function testSuccess() { $this->assertEquals(1.0, 1.1, '', 0.2); } public function testFailure() { $this->assertEquals(1.0, 1.1); } } ?>

phpunit EqualsTest PHPUnit 5.0.0 by Sebastian Bergmann and contributors. .F Time: 0 seconds, Memory: 5.75Mb There was 1 failure: 1) EqualsTest::testFailure Failed asserting that 1.1 matches expected 1.0. /home/sb/EqualsTest.php:11 FAILURES! Tests: 2, Assertions: 2, Failures: 1.

assertEquals(DOMDocument $expected, DOMDocument $actual[, string $message = ”])

当 $expected 和 $actual 这两个 DOMDocument 对象所表示的 XML 文档对应的无注释规范形式不相同时报告错误,错误讯息由 $message 指定。

Example A.15. assertEquals()应用于 DOMDocument 对象时的用法

<?php class EqualsTest extends PHPUnit_Framework_TestCase { public function testFailure() { $expected = new DOMDocument; $expected->loadXML(''); $actual = new DOMDocument; $actual->loadXML(''); $this->assertEquals($expected, $actual); } } ?>

phpunit EqualsTest PHPUnit 5.0.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.00Mb There was 1 failure: 1) EqualsTest::testFailure Failed asserting that two DOM documents are equal. --- Expected +++ Actual @@ @@ <?xml version="1.0"?> - - - + + + /home/sb/EqualsTest.php:12 FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertEquals(object $expected, object $actual[, string $message = ”])

当 $expected 和 $actual 这两个对象的属性值不相等时报告错误,错误讯息由 $message 指定。

Example A.16. assertEquals()应用于对象时的用法

<?php class EqualsTest extends PHPUnit_Framework_TestCase { public function testFailure() { $expected = new stdClass; $expected->foo = 'foo'; $expected->bar = 'bar'; $actual = new stdClass; $actual->foo = 'bar'; $actual->baz = 'bar'; $this->assertEquals($expected, $actual); } } ?>

phpunit EqualsTest PHPUnit 5.0.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.25Mb There was 1 failure: 1) EqualsTest::testFailure Failed asserting that two objects are equal. --- Expected +++ Actual @@ @@ stdClass Object ( - 'foo' => 'foo' - 'bar' => 'bar' + 'foo' => 'bar' + 'baz' => 'bar' ) /home/sb/EqualsTest.php:14 FAILURES! Tests: 1, Assertions: 1, Failures: 1.

assertEquals(array $expected, array $actual[, string $message = ”])

当 $expected 和 $actual 这两个数组不相等时报告错误,错误讯息由 $message 指定。

Example A.17. assertEquals() 应用于数组时的用法

<?php class EqualsTest extends PHPUnit_Framework_TestCase { public function testFailure() { $this->assertEquals(array('a', 'b', 'c'), array('a', 'c', 'd')); } } ?>

phpunit EqualsTest PHPUnit 5.0.0 by Sebastian Bergmann and contributors. F Time: 0 seconds, Memory: 5.25Mb There was 1 failure: 1) EqualsTest::testFailure Failed asserting that two arrays are equal. --- Expected +++ Actual @@ @@ Array ( 0 => 'a' - 1 => 'b' - 2 => 'c' + 1 => 'c' + 2 => 'd' ) /home/sb/EqualsTest.php:6 FAILURES! Tests: 1, Assertions: 1, Failures: 1.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值