$needle 不是 $haystack的元素时报告错误,错误讯息由 $message 指定。

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


例 A.5: assertContains() 的用法

  1. PHPUnit 6.5.0 by Sebastian Bergmann and contributors.
  2.  
  3. F
  4.  
  5. Time: 0 seconds, Memory: 5.00Mb
  6.  
  7. There was 1 failure:
  8.  
  9. 1) ContainsTest::testFailure
  10. Failed asserting that an array contains 4.
  11.  
  12. /home/sb/ContainsTest.php:6
  13.  
  14. FAILURES!
  15. Tests: 1, Assertions: 1, Failures: 1.

assertContains(string $needle, string $haystack[, string $message = '', boolean $ignoreCase = false])

如果 $ignoreCasetrue,测试将按大小写不敏感的方式进行。


例 A.6: assertContains() 的用法

  1. PHPUnit 6.5.0 by Sebastian Bergmann and contributors.
  2.  
  3. F
  4.  
  5. Time: 0 seconds, Memory: 5.00Mb
  6.  
  7. There was 1 failure:
  8.  
  9. 1) ContainsTest::testFailure
  10. Failed asserting that 'foobar' contains "baz".
  11.  
  12. /home/sb/ContainsTest.php:6
  13.  
  14. FAILURES!
  15. Tests: 1, Assertions: 1, Failures: 1.
  1. PHPUnit 6.5.0 by Sebastian Bergmann and contributors.
  2.  
  3. F.
  4.  
  5. Time: 0 seconds, Memory: 2.75Mb
  6.  
  7. There was 1 failure:
  8.  
  9. 1) ContainsTest::testFailure
  10. Failed asserting that 'FooBar' contains "foo".
  11.  
  12. /home/sb/ContainsTest.php:6
  13.  
  14. FAILURES!
  15. Tests: 2, Assertions: 2, Failures: 1.