assertEquals()


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

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

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

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

值得注意的是,应用 assertEquals()assertNotEquals() 时,如果其中一个变量是数值零(无论整数还是浮点数),另一个变量是字符串,那么会同时比较变量的类型(换言之,会用 === 比较操作符,而不是普通的 == 比较操作符)。在假定 0 == "" 为 true(根据 自动类型转换)的情况下使用 assertEquals()assertNotEquals() 这可能会导致问题,因为事实上其判定结果为 false。


例 A.13: assertEquals() 的用法

  1. <?php
  2. class EqualsTest extends PHPUnit_Framework_TestCase
  3. {
  4. public function testFailure()
  5. {
  6. $this->assertEquals(1, 0);
  7. }
  8.  
  9. public function testFailure2()
  10. {
  11. $this->assertEquals('bar', 'baz');
  12. }
  13.  
  14. public function testFailure3()
  15. {
  16. $this->assertEquals("foo\nbar\nbaz\n", "foo\nbah\nbaz\n");
  17. }
  18.  
  19. public function testFailure4()
  20. {
  21. $this->assertEquals("", 0)
  22. }
  23. }
  24. ?>
  1. phpunit EqualsTest
  2. PHPUnit 4.8.0 by Sebastian Bergmann and contributors.
  3.  
  4. FFF
  5.  
  6. Time: 0 seconds, Memory: 5.25Mb
  7.  
  8. There were 3 failures:
  9.  
  10. 1) EqualsTest::testFailure
  11. Failed asserting that 0 matches expected 1.
  12.  
  13. /home/sb/EqualsTest.php:6
  14.  
  15. 2) EqualsTest::testFailure2
  16. Failed asserting that two strings are equal.
  17. --- Expected
  18. +++ Actual
  19. @@ @@
  20. -'bar'
  21. +'baz'
  22.  
  23. /home/sb/EqualsTest.php:11
  24.  
  25. 3) EqualsTest::testFailure3
  26. Failed asserting that two strings are equal.
  27. --- Expected
  28. +++ Actual
  29. @@ @@
  30. 'foo
  31. -bar
  32. +bah
  33. baz
  34. '
  35.  
  36. /home/sb/EqualsTest.php:16
  37.  
  38. 4) EqualsTest::testFailure4
  39. Failed asserting that 0 matches expected ''.
  40.  
  41. /home/sb/EqualsTest.php:21
  42.  
  43. FAILURES!
  44. Tests: 4, Assertions: 4, Failures: 4.

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

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

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

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


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

  1. <?php
  2. class EqualsTest extends PHPUnit_Framework_TestCase
  3. {
  4. public function testSuccess()
  5. {
  6. $this->assertEquals(1.0, 1.1, '', 0.2);
  7. }
  8.  
  9. public function testFailure()
  10. {
  11. $this->assertEquals(1.0, 1.1);
  12. }
  13. }
  14. ?>
  1. phpunit EqualsTest
  2. PHPUnit 4.8.0 by Sebastian Bergmann and contributors.
  3.  
  4. .F
  5.  
  6. Time: 0 seconds, Memory: 5.75Mb
  7.  
  8. There was 1 failure:
  9.  
  10. 1) EqualsTest::testFailure
  11. Failed asserting that 1.1 matches expected 1.0.
  12.  
  13. /home/sb/EqualsTest.php:11
  14.  
  15. FAILURES!
  16. Tests: 2, Assertions: 2, Failures: 1.

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

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


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

  1. <?php
  2. class EqualsTest extends PHPUnit_Framework_TestCase
  3. {
  4. public function testFailure()
  5. {
  6. $expected = new DOMDocument;
  7. $expected->loadXML('<foo><bar/></foo>');
  8.  
  9. $actual = new DOMDocument;
  10. $actual->loadXML('<bar><foo/></bar>');
  11.  
  12. $this->assertEquals($expected, $actual);
  13. }
  14. }
  15. ?>
  1. phpunit EqualsTest
  2. PHPUnit 4.8.0 by Sebastian Bergmann and contributors.
  3.  
  4. F
  5.  
  6. Time: 0 seconds, Memory: 5.00Mb
  7.  
  8. There was 1 failure:
  9.  
  10. 1) EqualsTest::testFailure
  11. Failed asserting that two DOM documents are equal.
  12. --- Expected
  13. +++ Actual
  14. @@ @@
  15. <?xml version="1.0"?>
  16. -<foo>
  17. - <bar/>
  18. -</foo>
  19. +<bar>
  20. + <foo/>
  21. +</bar>
  22.  
  23. /home/sb/EqualsTest.php:12
  24.  
  25. FAILURES!
  26. Tests: 1, Assertions: 1, Failures: 1.

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

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


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

  1. <?php
  2. class EqualsTest extends PHPUnit_Framework_TestCase
  3. {
  4. public function testFailure()
  5. {
  6. $expected = new stdClass;
  7. $expected->foo = 'foo';
  8. $expected->bar = 'bar';
  9.  
  10. $actual = new stdClass;
  11. $actual->foo = 'bar';
  12. $actual->baz = 'bar';
  13.  
  14. $this->assertEquals($expected, $actual);
  15. }
  16. }
  17. ?>
  1. phpunit EqualsTest
  2. PHPUnit 4.8.0 by Sebastian Bergmann and contributors.
  3.  
  4. F
  5.  
  6. Time: 0 seconds, Memory: 5.25Mb
  7.  
  8. There was 1 failure:
  9.  
  10. 1) EqualsTest::testFailure
  11. Failed asserting that two objects are equal.
  12. --- Expected
  13. +++ Actual
  14. @@ @@
  15. stdClass Object (
  16. - 'foo' => 'foo'
  17. - 'bar' => 'bar'
  18. + 'foo' => 'bar'
  19. + 'baz' => 'bar'
  20. )
  21.  
  22. /home/sb/EqualsTest.php:14
  23.  
  24. FAILURES!
  25. Tests: 1, Assertions: 1, Failures: 1.

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

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


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

  1. <?php
  2. class EqualsTest extends PHPUnit_Framework_TestCase
  3. {
  4. public function testFailure()
  5. {
  6. $this->assertEquals(array('a', 'b', 'c'), array('a', 'c', 'd'));
  7. }
  8. }
  9. ?>
  1. phpunit EqualsTest
  2. PHPUnit 4.8.0 by Sebastian Bergmann and contributors.
  3.  
  4. F
  5.  
  6. Time: 0 seconds, Memory: 5.25Mb
  7.  
  8. There was 1 failure:
  9.  
  10. 1) EqualsTest::testFailure
  11. Failed asserting that two arrays are equal.
  12. --- Expected
  13. +++ Actual
  14. @@ @@
  15. Array (
  16. 0 => 'a'
  17. - 1 => 'b'
  18. - 2 => 'c'
  19. + 1 => 'c'
  20. + 2 => 'd'
  21. )
  22.  
  23. /home/sb/EqualsTest.php:6
  24.  
  25. FAILURES!
  26. Tests: 1, Assertions: 1, Failures: 1.