本文介绍PHPUNIT中一些重要的方法和断言,PHPUNIT中还有大量丰富的断言,对提高单元测试十分有帮助,具体的请参考PHPUNIT用户手册
class assertTest extends PHPUnit_Framework_TestCase {
//例 A.1: assertArrayHasKey() 的用法,断言数组key
public function testAssertArrayHasKey() {
$this -> assertArrayHasKey('foo', array('bar' => 'baz'));
}
//例 A.2: assertClassHasAttribute() 的用法,断言类的属性
public function testAssertClassHasAttribute() {
$this -> assertClassHasAttribute('foo', 'stdClass');
}
//例 A.3: assertClassHasStaticAttribute() 的用法,是否存在静态属性
public function test_assertClassHasStaticAttribute() {
$this -> assertClassHasStaticAttribute('foo', 'stdClass');
}
//例 A.4: assertContains() 的用法,是否包含
public function test_assertContains() {
$this -> assertContains(2, array(
1,
2,
3
));
$this -> assertContains('foo', 'FooBar', '区分大小写');
$this -> assertContains('foo', 'FooBar', '不区分大小写', TRUE);
}
//例 A.5: assertContainsOnly() 的用法,包含类型一至
public function test_assertContainsOnly() {
$this -> assertContainsOnly('string', array(
'1',
'2',
3
));
}
//例 A.6: assertContainsOnlyInstancesOf() 的用法
public function test_assertContainsOnlyInstancesOf() {
$this -> assertContainsOnlyInstancesOf('DateTime', array(
new DateTime(),
new DateTimeZone()
));
}
//例 A.7: assertCount() 的用法,数量
public function test_assertCount() {
$this -> assertCount(0, array('foo'), '数量不一致');
}
//例 A.8: assertEmpty() 的用法
public function test_assertEmpty() {
$this -> assertEmpty(array('foo'), '数组不为空');
// $this -> assertEmpty(0, '0不为空');
// $this -> assertEmpty(false, 'false不为空');
//$this -> assertEmpty(' ', ' 空字符串');
}
//例 A.9: assertEqualXMLStructure() 的用法
public function testFailureWithDifferentNodeNames() {
$expected = new DOMElement('foo');
$actual = new DOMElement('bar');
$this -> assertEqualXMLStructure($expected, $actual);
}
public function testFailureWithDifferentNodeAttributes() {
$expected = new DOMDocument;
$expected -> loadXML('');
$actual = new DOMDocument;
$actual -> loadXML('');
$this -> assertEqualXMLStructure($expected -> firstChild, $actual -> firstChild, TRUE);
}
public function testFailureWithDifferentChildrenCount() {
$expected = new DOMDocument;
$expected -> loadXML('');
$actual = new DOMDocument;
$actual -> loadXML('');
$this -> assertEqualXMLStructure($expected -> firstChild, $actual -> firstChild);
}
public function testFailureWithDifferentChildren() {
$expected = new DOMDocument;
$expected -> loadXML('');
$actual = new DOMDocument;
$actual -> loadXML('');
$this -> assertEqualXMLStructure($expected -> firstChild, $actual -> firstChild);
}
//例 A.12: assertEquals() 的用法,相等
public function test_assertEquals() {
//$this -> assertEquals(1, 0);
//$this -> assertEquals('bar', 'baz');
// $this -> assertEquals("foo\nbar\nbaz\n", "foo\nbah\nbaz\n");
$this -> assertEquals(1.0, 1.1);
// $this -> assertEquals(1.0, 1.1, '', 0.2);
// $this->assertEquals(array('a', 'b', 'c'), array('a', 'c', 'd'));
}
//例 A.17: assertFalse() 的用法,FALSE判断 与之对应的还有assertTrue() 用法相同
public function test_assertFalse() {
$this -> assertFalse(TRUE);
}
//例 A.18: assertFileEquals() 的用法,当 $expected 所指定的文件与 $actual 所指定的文件其内容不同时,报告一个错误,错误讯息的内容由 $message 指定
public function test_assertFileEquals() {
$this -> assertFileEquals('/www/info.php', '/www/info.php2');
}
//例 A.19: assertFileExists() 的用法,当 $filename 所指定的文件不存在时,报告一个错误,错误讯息的内容由 $message 指定。
public function test_assertFileExists() {
$this -> assertFileExists('/www/info.php3');
}
/**
* assertGreaterThan(mixed $expected, mixed $actual[, string $message = ''])
* 当 $actual 的值不大于 $expected 的值时,报告一个错误,错误讯息的内容由 $message 指定。
* assertAttributeGreaterThan() 是便捷包装(convenience wrappers),以某个类或对象的某个 public、 protected 或 private 属性作为实际值来进行比较
*/
public function test_assertGreaterThan() {
$this -> assertGreaterThan(2, 1);
}
/**
* assertGreaterThanOrEqual(mixed $expected, mixed $actual[, string $message = ''])
* 当 $actual 的值不大于且不等于 $expected 的值时,报告一个错误,错误讯息的内容由 $message 指定。
* assertAttributeGreaterThanOrEqual() 是便捷包装(convenience wrappers),以某个类或对象的某个 public、 protected 或 private 属性作为实际值来进行比较。
*/
function test_assertGreaterThanOrEqual() {
$this -> assertGreaterThanOrEqual(2, 1);
}
/**
assertInstanceOf($expected, $actual[, $message = ''])
当 $actual 不是 $expected的实例时,报告一个错误,错误讯息的内容由 $message 指定。
assertNotInstanceOf() 是与之相反的断言,并接受相同的参数。
assertAttributeInstanceOf() and assertAttributeNotInstanceOf() 是便捷包装(convenience wrappers), 可以应用于某个类或对象的某个 public、 protected 或 private 属性。
*/
function test_assertInstanceOf() {
$this -> assertInstanceOf('RuntimeException', new Exception);
}
/**
assertInternalType($expected, $actual[, $message = ''])
当 $actual 不是 $expected 所指明的类型时,报告一个错误,错误讯息的内容由 $message 指定。
assertNotInternalType() 是与之相反的断言,并接受相同的参数。
assertAttributeInternalType() and assertAttributeNotInternalType() 是便捷包装(convenience wrappers), 可以应用于某个类或对象的某个 public、 protected 或 private 属性.
*/
function test_assertInternalType() {
$this -> assertInternalType('string', 42);
//$this -> assertInternalType(344, 42);
}
/**
assertJsonFileEqualsJsonFile(mixed $expectedFile, mixed $actualFile[, string $message = ''])
当 $actualFile 的值与 $expectedFile 的值不匹配时,报告一个错误,错误讯息的内容由 $message 指定。
*还有一个类似的assertXmlFileEqualsXmlFile()
*/
function test_assertJsonFileEqualsJsonFile() {
$this -> assertJsonFileEqualsJsonFile('/www/info.php', 'path/to/actual/file');
}
/**
assertJsonStringEqualsJsonFile(mixed $expectedFile, mixed $actualJson[, string $message = ''])
当 $actualJson 的值与 $expectedFile的值不匹配时,报告一个错误,错误讯息的内容由 $message 指定。
*还有一个类似的assertXmlStringEqualsXmlFile()
*/
function test_assertJsonStringEqualsJsonFile() {
$this -> assertJsonStringEqualsJsonFile('path/to/fixture/file', json_encode(array("Mascott" => "ux")));
}
/**
assertJsonStringEqualsJsonString(mixed $expectedJson, mixed $actualJson[, string $message = ''])
当 $actualJson 的值与 $expectedJson 的值不匹配时,报告一个错误,错误讯息的内容由 $message 指定。
* 还有一个类似的assertXmlStringEqualsXmlString()
*/
function test_assertJsonStringEqualsJsonString() {
$this -> assertJsonStringEqualsJsonString(json_encode(array("Mascott" => "Tux")), json_encode(array("Mascott" => "ux")));
}
/**
assertLessThan(mixed $expected, mixed $actual[, string $message = ''])
当 $actual 的值不小于 $expected 的值时,报告一个错误,错误讯息的内容由 $message 指定。
assertAttributeLessThan() 是便捷包装(convenience wrappers),以某个类或对象的某个 public、 protected 或 private 属性作为实际值来进行比较。
*/
function test_assertLessThan() {
$this -> assertLessThan(1, 2);
}
/**
assertLessThanOrEqual(mixed $expected, mixed $actual[, string $message = ''])
当 $actual 的值不小于且不等于 $expected 的值时,报告一个错误,错误讯息的内容由 $message 指定。
assertAttributeLessThanOrEqual() 是便捷包装(convenience wrappers),以某个类或对象的某个 public、 protected 或 private 属性作为实际值来进行比较。
*/
function test_assertLessThanOrEqual() {
$this -> assertLessThanOrEqual(1, 2);
}
/**
assertNull(mixed $variable[, string $message = ''])
当 $variable 不是 NULL 时,报告一个错误,错误讯息的内容由 $message 指定。
assertNotNull() 是与之相反的断言,并接受相同的参数
*/
function test_assertNull() {
$this -> assertNull('foo');
}
/**
assertObjectHasAttribute(string $attributeName, object $object[, string $message = ''])
当 $object->attributeName 不存在时,报告一个错误,错误讯息的内容由 $message 指定。
assertObjectNotHasAttribute() 是与之相反的断言,并接受相同的参数。
*/
function test_assertObjectHasAttribute() {
$this -> assertObjectHasAttribute('foo', new stdClass);
}
/**
assertRegExp(string $pattern, string $string[, string $message = ''])
当 $string 与正则表达式 $pattern 不匹配时,报告一个错误,错误讯息的内容由 $message 指定。
assertNotRegExp() 是与之相反的断言,并接受相同的参数。
*/
function test_assertRegExp() {
$this -> assertRegExp('/food/', 'bar');
}
/**
assertStringMatchesFormat(string $format, string $string[, string $message = ''])
当 $string 与格式串 $format 不匹配时,报告一个错误,错误讯息的内容由 $message 指定。
assertStringNotMatchesFormat() 是与之相反的断言,并接受相同的参数。
* 格式串中可以使用如下占位符:
%e:表示目录分隔符,以 Linux 系统为例,是 /。
%s:一个或多个除了换行符以外的任意字符(非空白字符或者空白字符)。
%S:零个或多个除了换行符以外的任意字符(非空白字符或者空白字符)。
%a:一个或多个包括换行符在内的任意字符(非空白字符或者空白字符)。
%A:零个或多个包括换行符在内的任意字符(非空白字符或者空白字符)。
%w:零个或多个空白字符。
%i:带符号整数值,例如 +3142、-3142。
%d:无符号整数值,例如 123456。
%x:一个或多个十六进制字符。所谓十六进制字符,指的是在以下范围内的字符:0-9、a-f、A-F。
%f:浮点数,例如 3.142、-3.142、3.142E-10、3.142e+10。
%c:单个任意字符。
*/
function test_assertStringMatchesFormat() {
$this -> assertStringMatchesFormat('%i', 'foo');
}
/**
assertStringMatchesFormatFile(string $formatFile, string $string[, string $message = ''])
当 $string 与 $formatFile 的内容不匹配时,报告一个错误,错误讯息的内容由 $message 指定。
assertStringNotMatchesFormatFile() 是与之相反的断言,并接受相同的参数
*/
function test_assertStringMatchesFormatFile() {
$this -> assertStringMatchesFormatFile('/www/info.php2', 'test');
}
/**
assertSame(mixed $expected, mixed $actual[, string $message = ''])
当两个变量 $expected 和 $actual 的类型与值不完全相同时,报告一个错误,错误讯息的内容由 $message 指定。
assertNotSame() 是与之相反的断言,并接受相同的参数。
assertAttributeSame() and assertAttributeNotSame() 是便捷包装(convenience wrappers), 以某个类或对象的某个 public、 protected 或 private 属性作为实际值来进行比较。
*/
function test_assertSame() {
$this -> assertSame('2204', 2204);
}
/**
assertSame(object $expected, object $actual[, string $message = ''])
当两个变量 $expected 和 $actual 不是指向同一个对象的引用时,报告一个错误,错误讯息的内容由 $message 指定。
*/
function test_assertSame_obj() {
$this -> assertSame(new stdClass, new stdClass);
}
/**
assertStringEndsWith(string $suffix, string $string[, string $message = ''])
当 $string 不以 $suffix 结尾时,报告一个错误,错误讯息的内容由 $message 指定。
assertStringStartsWith(string $prefix, string $string[, string $message = ''])(开始判断)
assertStringEndsNotWith() 是与之相反的断言,并接受相同的参数。
*/
function test_assertStringEndsWith() {
$this -> assertStringEndsWith('suffix', 'foo');
}
/**
assertStringEqualsFile(string $expectedFile, string $actualString[, string $message = ''])
当 $expectedFile 所指定的文件其内容与 $actualString 不相同时,报告一个错误,错误讯息的内容由 $message 指定。
assertStringNotEqualsFile() 是与之相反的断言,并接受相同的参数
*/
function test_assertStringEqualsFile() {
$this -> assertStringMatchesFormatFile('/www/info.php2', 'echo');
}
/**
assertThat()
可以用 PHPUnit_Framework_Constraint 类来订立更加复杂的断言。这些断言可以用 assertThat() 方法对其进行评定。例 A.39展示了如何用 logicalNot() 和 equalTo() 约束条件来表达与 assertNotEquals() 等价的断言。
assertThat(mixed $value, PHPUnit_Framework_Constraint $constraint[, $message = ''])
当 $value 与 $constraint 不匹配时,报告一个错误,错误讯息的内容由 $message 指定。
*/
function test_assertThat() {
// $theBiscuit = new Biscuit('Ginger');
// $myBiscuit = new Biscuit('Ginger');
// $this -> assertThat($theBiscuit, $this -> logicalNot($this -> equalTo($myBiscuit)));
}
}
本文参考PHPUnit手册,查看完整手册地址 :
http://phpunit.de/manual/current/zh_cn/installation.html