天天看点

phpunit学习 3:

16:17 2015/12/11

phpunit学习 3:

单元测试的大概步骤是:编写待测试类,编写测试用例类,编写测试类,测试。

1.如果你有多个类,多个测试类的test类,那么可以编写一个AllTests套件。包含所有的待测试的测试类,然后在phpunit下统一执行就行。这个类可能是这样的:

2.最后编写测试类,不编写这个类也可以,在命令行下直接跑phpunitcalculatorTest就行。

参见:http://blog.csdn.net/jucrazy/article/details/6720935

断言

布尔类型

assertTrue 断言为真

assertFalse 断言为假

NULL类型

assertNull 断言为NULL

assertNotNull 断言非NULL

数字类型

assertEquals 断言等于

assertNotEquals 断言不等于

assertGreaterThan 断言大于

assertGreaterThanOrEqual 断言大于等于

assertLessThan 断言小于

assertLessThanOrEqual 断言小于等于

字符类型

assertContains 断言包含

assertNotContains 断言不包含

assertContainsOnly 断言只包含

assertNotContainsOnly 断言不只包含

数组类型

assertArrayHasKey 断言有键

assertArrayNotHasKey 断言没有键

对象类型

assertAttributeContains 断言属性包含

assertAttributeContainsOnly 断言属性只包含

assertAttributeEquals 断言属性等于

assertAttributeGreaterThan 断言属性大于

assertAttributeGreaterThanOrEqual 断言属性大于等于

assertAttributeLessThan 断言属性小于

assertAttributeLessThanOrEqual 断言属性小于等于

assertAttributeNotContains 断言不包含

assertAttributeNotContainsOnly 断言属性不只包含

assertAttributeNotEquals 断言属性不等于

assertAttributeNotSame 断言属性不相同

assertAttributeSame 断言属性相同

assertSame 断言类型和值都相同

assertNotSame 断言类型或值不相同

assertObjectHasAttribute 断言对象有某属性

assertObjectNotHasAttribute 断言对象没有某属性

class类型

class类型包含对象类型的所有断言,还有

assertClassHasAttribute 断言类有某属性

assertClassHasStaticAttribute 断言类有某静态属性

assertClassNotHasAttribute 断言类没有某属性

assertClassNotHasStaticAttribute 断言类没有某静态属性

文件相关

assertFileEquals 断言文件内容等于

assertFileExists 断言文件存在

assertFileNotEquals 断言文件内容不等于

assertFileNotExists 断言文件不存在

XML相关

assertXmlFileEqualsXmlFile 断言XML文件内容相等

assertXmlFileNotEqualsXmlFile 断言XML文件内容不相等

assertXmlStringEqualsXmlFile 断言XML字符串等于XML文件内容

assertXmlStringEqualsXmlString 断言XML字符串相等

assertXmlStringNotEqualsXmlFile 断言XML字符串不等于XML文件内容

assertXmlStringNotEqualsXmlString 断言XML字符串不相等

以下可能更全 但可能没有分类

分类: phpunit(7)

断言(Assertions)是PHPUnit提供的一系列对程序执行结果测试的方法。通俗的讲,就是断言执行程序结果为我们期待的值,如果不是则测试失败,下面是断言方法的详细介绍,内容全部来翻译自PHPUnit的官方文档,部分方法官方介绍的很模糊,我根据官方的源码注释增加了说明和注释

assertArrayHasKey(mixed $key, array $array[, string $message = '']) s断言数组$array含有索引$key, $message用于自定义输出的错误信息,后同

assertClassHasAttribute(string $attributeName, string $className[, string $message = '']) 断言类$className含有属性$attributeName

assertClassHasStaticAttribute(string $attributeName, string $className[, string $message = '']) 断言类$className含有静态属性$attributeName

assertContains(mixed $needle, Iterator|array $haystack[, string $message = ''])断言迭代器对象$haystack/数组$haystack含有$needle

assertNotContains() 与上条相反

assertAttributeContains(mixed $needle, Class|Object $haystack[, string $message = '']) 断言$needle为一个类/对象$haystack可访问到的属性(public, protected 和 private)

assertAttributeNotContains() 与上条相反

assertContains(string $needle, string $haystack[, string $message = '']) 断言字符串$needle在字符串$haystack中

assertContainsOnly(string $type, Iterator|array $haystack[, boolean $isNativeType = NULL, string $message = '']) 断言迭代器对象/数组$haystack中只有$type类型的值, $isNativeType 设定为PHP原生类型,$message同上

assertNotContainsOnly() 与上条相反

assertAttributeContainsOnly() 和 assertAttributeNotContainsOnly() 断言对象的属性只有$type类型和非含有$type类型

assertEmpty(mixed $actual[, string $message = '']) 断言$actual为空

assertNotEmpty() 遇上条相反

assertAttributeEmpty() 和 assertAttributeNotEmpty() 断言对象的所有属性为空或不为空

assertEqualXMLStructure(DOMNode $expectedNode, DOMNode $actualNode[, boolean $checkAttributes = FALSE, string $message = '']) 断言Dom节点$actualNode和DOM节点$expectedNode相同,$checkAttributes FALSE 不断言节点属性,TRUE则断言属性$message同上

assertEquals(mixed $expected, mixed $actual[, string $message = '']) 断言复合类型$actual与$expected相同

assertNotEquals() 与上条相反

assertAttributeEquals() and assertAttributeNotEquals() 断言类属性$actual与$expected相同

assertEquals(array $expected, array $actual[, string $message = '']) 断言数组$actual和数组$expected相同

assertFalse(bool $condition[, string $message = '']) 断言$condition的结果为false

assertFileEquals(string $expected, string $actual[, string $message = '']) 断言文件$actual和$expected相同

assertFileExists(string $filename[, string $message = '']) 断言文件$filename存在

assertFileNotExists() 与上条相反

assertGreaterThan(mixed $expected, mixed $actual[, string $message = '']) 断言$actual比$expected大

assertAttributeGreaterThan() 断言类的属性用

assertGreaterThanOrEqual(mixed $expected, mixed $actual[, string $message = '']) 断言$actual大于等于$expected

assertAttributeGreaterThanOrEqual() 断言类的属性

assertInstanceOf($expected, $actual[, $message = '']) 断言$actual为$expected的实例

assertNotInstanceOf() 与上相反

assertAttributeInstanceOf() and assertAttributeNotInstanceOf() 断言类属性用

assertInternalType($expected, $actual[, $message = '']) 断言$actual的类型为$expected

assertNotInternalType() 与上相反

assertAttributeInternalType() and assertAttributeNotInternalType() 断言类属性用

assertLessThan(mixed $expected, mixed $actual[, string $message = '']) 断言$actual小于$expected

assertAttributeLessThan() 断言类属性小于$expected

assertLessThanOrEqual(mixed $expected, mixed $actual[, string $message = '']) 断言$actual小于等于$expected

assertAttributeLessThanOrEqual() 断言类属性小于等于$expected

assertNull(mixed $variable[, string $message = '']) 断言$variable的值为null

assertNotNull() 与上条相反

assertObjectHasAttribute(string $attributeName, object $object[, string $message = '']) 断言$object含有属性$attributeName

assertObjectNotHasAttribute() 与上条相反

assertRegExp(string $pattern, string $string[, string $message = '']) 断言字符串$string符合正则表达式$pattern

assertNotRegExp() 与上条相反

assertStringMatchesFormat(string $format, string $string[, string $message = '']) 断言$string符合$format定义的格式,例如 %i %s等等

assertStringNotMatchesFormat()

与上条相反

assertStringMatchesFormatFile(string $formatFile, string $string[, string $message = ''])

断言$string路径的文件的格式和$formatFile文件的格式相同

assertStringNotMatchesFormatFile()

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

断言$actual和$expected的类型和值相同

assertNotSame()

assertAttributeSame() and assertAttributeNotSame()

断言类属性用

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

断言对象$actual和对象$expected相同

assertSelectCount(array $selector, integer $count, mixed $actual[, string $message = '', boolean $isHtml = TRUE])

断言在$actual文档中(格式为html或xml)css选择器$selector有$count个,或有符合$selector的元素(设定$count为true),或没有符合$selector的元素(设定$count为false)

assertSelectCount("#binder", true, $xml); // 有一个就行

assertSelectCount(".binder", 3, $xml); // 必须有3个?

assertSelectEquals(array $selector, string $content, integer $count, mixed $actual[, string $message = '', boolean $isHtml = TRUE])

断言在文档$actual中有符合根据$selector的找到符合$content的$count个元素,当$count等于true和false的时候作用如下:

assertSelectEquals("#binder .name", "Chuck", true, $xml); // 所有的name等于Chuck

assertSelectEquals("#binder .name", "Chuck", false, $xml); // 所有的name不等于Chuck

assertSelectRegExp(array $selector, string $pattern, integer $count, mixed $actual[, string $message = '', boolean $isHtml = TRUE])

assertStringEndsWith(string $suffix, string $string[, string $message = ''])

断言$string的末尾为$suffix结束

assertStringEndsNotWith()

assertStringEqualsFile(string $expectedFile, string $actualString[, string $message = ''])

断言$actualString在文件$expectedFile的内容中

assertStringNotEqualsFile()

assertStringStartsWith(string $prefix, string $string[, string $message = ''])

断言$string的开头为$suffix

assertStringStartsNotWith()

assertTag(array $matcher, string $actual[, string $message = '', boolean $isHtml = TRUE])

断言$actual的内容符合$matcher的定义,matcher的定义如下:

# id: 节点必须带有id属性且名称与id设定的相同

# tags: 节点的名称必须与tags的值匹配

# attributes: 节点的属性必须与$attributes数组中的值相匹配

# content: 文本内容必须与$content的值相同.

# parent: 节点的父节点必须符合$parent数组中定义的内容.

# child: 节点的字节点中有至少一个直系子节点满足 $child 数组中定义的内容.

At least one of the node's immediate children must meet the criteria described by the $child associative array.

# ancestor: 节点的父节点中有至少一个节点满足 $ancestor 数组中定义的内容.

At least one of the node's ancestors must meet the criteria described by the $ancestor associative array.

# descendant: 节点的字节点中有至少一个子节点满足 $descendant 数组中定义的内容.

At least one of the node's descendants must meet the criteria described by the $descendant associative array.

# children: 用于计算字节点的联合数组

Associative array for counting children of a node.

* count: 符合匹配标准的字节点数目需要和count的值相同

The number of matching children must be equal to this number.

* less_than: 符合匹配标准的字节点数目需要比count的值少

The number of matching children must be less than this number.

* greater_than: 符合匹配标准的字节点数目需要比count的值多

The number of matching children must be greater than this number.

* only: 另外一个联合数组用于定义配合标准的节点,只有这些节点才会被计算入内

Another associative array consisting of the keys to use to match on the children, and only matching children will be counted

assertTag的代码例子(图片点击放大):

点击查看原图

点击查看原图

More complex assertions can be formulated using the PHPUnit_Framework_Constraint classes

更加复杂的断言可以通过PHPUnit_Framework_Constraint类来制定

PHPUnit_Framework_Constraint_Attribute attribute(PHPUnit_Framework_Constraint $constraint, $attributeName)

约束允许另外一个约束类为一个类或对象的属性

Constraint that applies another constraint to an attribute of a class or an object.

PHPUnit_Framework_Constraint_IsAnything anything()

约束接受任意的输入值

Constraint that accepts any input value.

PHPUnit_Framework_Constraint_ArrayHasKey arrayHasKey(mixed $key)

Constraint that asserts that the array it is evaluated for has a given key.

约束断言评估数组有传入的$key

PHPUnit_Framework_Constraint_TraversableContains contains(mixed $value)

Constraint that asserts that the array or object that implements the Iterator interface it is evaluated for contains a given value.

约束断言一个数组或者实现迭代器接口的对象含有$value

PHPUnit_Framework_Constraint_IsEqual equalTo($value, $delta = 0, $maxDepth = 10)

Constraint that checks if one value is equal to another.

约束断言$value和其他的相同

PHPUnit_Framework_Constraint_Attribute attributeEqualTo($attributeName, $value, $delta = 0, $maxDepth = 10)

Constraint that checks if a value is equal to an attribute of a class or of an object.

约束断言$value和一个类或对象的属性的值相同

PHPUnit_Framework_Constraint_FileExists fileExists()

Constraint that checks if the file(name) that it is evaluated for exists.

约束断言文件是存在的

PHPUnit_Framework_Constraint_GreaterThan greaterThan(mixed $value)

Constraint that asserts that the value it is evaluated for is greater than a given value.

约束断言$value是大于传入的值的

PHPUnit_Framework_Constraint_Or greaterThanOrEqual(mixed $value)

Constraint that asserts that the value it is evaluated for is greater than or equal to a given value.

约束断言$value是大于或等于传入的值的

PHPUnit_Framework_Constraint_ClassHasAttribute classHasAttribute(string $attributeName)

Constraint that asserts that the class it is evaluated for has a given attribute.

约束断言类含有属性$attributeName

PHPUnit_Framework_Constraint_ClassHasStaticAttribute classHasStaticAttribute(string $attributeName)

Constraint that asserts that the class it is evaluated for has a given static attribute.

约束断言类含有静态属性$attributeName

PHPUnit_Framework_Constraint_ObjectHasAttribute hasAttribute(string $attributeName)

Constraint that asserts that the object it is evaluated for has a given attribute.

约束断言对象含有属性$attributeName

PHPUnit_Framework_Constraint_IsIdentical identicalTo(mixed $value)

Constraint that asserts that one value is identical to another.

约束断言$value和其他的完全相同

PHPUnit_Framework_Constraint_IsFalse isFalse()

Constraint that asserts that the value it is evaluated is FALSE.

约束断言$value的值为false

PHPUnit_Framework_Constraint_IsInstanceOf isInstanceOf(string $className)

Constraint that asserts that the object it is evaluated for is an instance of a given class.

约束断言对象是$className的实例

PHPUnit_Framework_Constraint_IsNull isNull()

Constraint that asserts that the value it is evaluated is NULL.

约束断言$value的值为NULL

PHPUnit_Framework_Constraint_IsTrue isTrue()

Constraint that asserts that the value it is evaluated is TRUE.

约束断言$value的值为TRUE

PHPUnit_Framework_Constraint_IsType isType(string $type)

Constraint that asserts that the value it is evaluated for is of a specified type.

约束断言对象的类型的为$type

PHPUnit_Framework_Constraint_LessThan lessThan(mixed $value)

Constraint that asserts that the value it is evaluated for is smaller than a given value.约束断言对象的值小于$value

PHPUnit_Framework_Constraint_Or lessThanOrEqual(mixed $value)

Constraint that asserts that the value it is evaluated for is smaller than or equal to a given value.约束断言对象的值小于等于$value

logicalAnd()

Logical AND.逻辑的And

logicalNot(PHPUnit_Framework_Constraint $constraint)

Logical NOT.逻辑的

logicalOr()

Logical OR.逻辑的OR

logicalXor()

Logical XOR.逻辑的XOR

PHPUnit_Framework_Constraint_PCREMatch matchesRegularExpression(string $pattern)

Constraint that asserts that the string it is evaluated for matches a regular expression.约束断言字符串符合传入的正则表达式$pattern

PHPUnit_Framework_Constraint_StringContains stringContains(string $string, bool $case)

Constraint that asserts that the string it is evaluated for contains a given string.约束断言字符串中含有$string

PHPUnit_Framework_Constraint_StringEndsWith stringEndsWith(string $suffix)

Constraint that asserts that the string it is evaluated for ends with a given suffix.约束断言字符串由$string结尾

PHPUnit_Framework_Constraint_StringStartsWith stringStartsWith(string $prefix)

Constraint that asserts that the string it is evaluated for starts with a given prefix. 约束断言字符串由$string开头

assertThat的用法,使用多个约束和约束逻辑来实现断言(图片点击放大)点击查看原图

assertTrue(bool $condition[, string $message = ''])

断言$condition为True,否则就报告错误

assertXmlFileEqualsXmlFile(string $expectedFile, string $actualFile[, string $message = ''])

断言$actualFile和$expectedFile的xml文件的内容相同,否则就报告错误

assertXmlStringEqualsXmlFile(string $expectedFile, string $actualXml[, string $message = ''])

断言$actualXml的内容和$expectedFile相同,否则就报告错误

assertXmlStringEqualsXmlString(string $expectedXml, string $actualXml[, string $message = ''])

断言$actualXml的内容和$expectedXml相同,否则就报告错误

上一篇PHPUnit学习笔记(三)测试方法进阶

下一篇PHPUnit学习笔记(五)PHPUnit参数详

------------

11:27 2015/12/9

phpunit测试学习 2: 分类总结断言涉及哪些方面

先推荐windows快速打开某处路径下的cmd,进入测试状态:

可以在文件夹中,按住Shift+鼠标右键,这时候你就会看到下面的图片。然后点击在此处打开命令行窗口就行了

参照:http://www.jb51.net/os/windows/111622.html

常规打开cmd进入目录太麻烦太慢了。

基本(assertNotEquals):

真假 是否为空 相等不等 大小等于 类型相同

类(assertClassHasStaticAttribute):

实例,对象(assertInstanceOf):

某对象是某类的实例 不是某对象的实例

字符串(assertStringMatchesFormat):

是否以某字符串为开头 是否符合某正则表达式(assertRegExp) 是否符合定义的格式(例如 %i %s等等)

类属性(assertAttributeContains):

属性类型 有类型 只有类型 含有某类型 含有某属性 真假 相等不等

含有静态属性 含有某静态属性 大小等于 某属性属于某个类 是否是某种类型

数组:

含有某索引

函数:

返回值 返回类型

文件(assertFileExists):

某文件存在与否 文件相同 文件路径下文件的类型: assertStringMatchesFormat断言$string符合$format定义

某段内容在某文件中 的格式,例如 %i %s等等

css(assertSelectCount):

某文档中某选择器有n个 有n个符合的元素

(assertSelectEquals("#binder .name", "Chuck", true, $xml); // 所有的name等于Chuck)

xml(assertEqualXMLStructure):

dom节点相同,

assertContainsOnly

assertContains(可以断言字符串中有某段字符)

assertAttributeGreaterThan(类的属性某比某大)

assertLessThan

assertAttributeInternalType

...........

很有用:

setUp和tearDown这两个方法来解决这个问题。

setUp会在类的每个测试用例运行之前被调用,你可以在里面做一些相关的程序初始化的工作

tearDown方法则会在本类每个测试用例运行完毕之后调用,你可以在里面进行一些相关的清理工作

需要注意的是,不一定写了setUp就要对应写tearDown,tearDown里面用来回收那些占用资源比较大的对象,如连接上的数据库,打开的文件等等。

还有很多方法如下:

断言之前/之后 调用测试用例之前/之后 类调用之前/完之后 出现没有调用成功的测试用例时

参数:

指定生成指定格式的log日志 指定格式覆盖日志 列出可用测试组 现实测试进程 设置结果显示颜色 遇到第一个失败 第一个错误 第一个跳过 第一个未完成的测试是停止 某测试没有定义任何断言 生成概要测试类 读测试代码开启语法检查 测试前运行bootstrap的php文件路径 在php的inxlude_path中增加路径 设置php配置属性 输出调试信息(如测试的名称 什么时候开始执行)

这个不可好像还不错,断言函数分了类的:http://blog.sina.com.cn/s/blog_6d425e1a010199gd.html

--------

16:45 2015/12/8

phpunit测试学习 1:一点简单的扼要有用的东西的总结 一点入门认识

具体的入门安装和入门实践请参照文中的推荐博客或网上其他博客

推荐博客,我感觉这几篇博客写得很不错,虽然一和二可以结合在一起,内容并不多且给新手更容易带来学习信心:

安装建议参照我的另一篇安装博客,因为当时我是按照这种方式失败的,文中提及网络原因导致失败,我当时显示的是安装的某个路径似乎没有维护了,那个url已经不存在了(所以我也不知道是网络原因还是真的那个路径不存在了)(PHP教程:PHPUnit学习笔记(一)PHPUnit介绍及安装:http://be-evil.org/phpunit-study-note-introduction-and-install.html)

PHP教程:PHPUnit学习笔记(二)PHPUnit基本用法:http://be-evil.org/phpunit-study-note-basic-use.html

阶段三,四在阶段二文章的底部有推荐链接,请自行进入学习

PHPUnit可以实现测试方法的依赖关系

class ArrayTest extends PHPUnit_Framework_TestCase 继承于PHPUnit_Framework_TestCase,进一步实现模块测试

@depends:依赖关系通过注释@depends来定义,如果某个测试方法依赖的方法测试没有功过,那么PHPUnit会自动跳过后面所有的依赖测试

@dataProvider: dataProvider标签标注给它提供数据的方法名,定义之后PHPUnit会自动的将数据提供者方法返回的数据依次传入到测试方法中测试.

有个实例:和前一个例子实现的测试内容相同,但是数据提供者返回的是一个迭代器对象而不是是数组(即是对象内的数据提供方法返回一个new的新对象---即是这个类的对象)

检查异常: PHPUnit中,我们有3种方式来检查异常是否抛出

@expectedException: 方法一: 注释法, 用@expectedException 标定期待的异常

$this->setExpectedException: 方法二: 设定法,使用 $this->setExpectedException 设定期待的异常

try catch + fail: 方法三: try catch + fail法

有时候我们的代码在运行时会出现php错误,如整除0,文件不存在等等

PHPUnit中,它会自动把错误转换为异常PHPUnit_Framework_Error并抛出,我们只需要在测试方法中设定抓取这个异常即可:

实例:

/**

* @expectedException PHPUnit_Framework_Error // 期待PHPUnit_Framework_Error的异常

*/

function test (){.....}

断言(Assertions)是PHPUnit提供的一系列对程序执行结果测试的方法。通俗的讲,就是断言执行程序结果为我们期待的值

assertArrayHasKey(mixed $key, array $array[, string $message = ''])

断言数组$array含有索引$key, $message用于自定义输出的错误信息,后同

-----------

16:12 2015/12/8

phpunit测试成功, phpunit测试实践代码

代码写在www目录下,以类名命名代码文件,我的文件名为ArrayTest.php,类名为ArrayTest,内部写了简单的测试代码:

<?php

// require_once 'PHPUnit/Autoload.php';

// require_once 'ArrayTeller.class.php';

// require_once 'PHPUnit/Framework.php';

class ArrayTest extends PHPUnit_Framework_TestCase

{

public function testNewArrayIsEmpty()

{

// 创建数组fixture。

$fixture = array();

// 断言数组fixture的尺寸是0。

$this->assertEquals(0, sizeof($fixture));

}

/**

*定义test标签声明该方法的测试方法

*@test

*/

public function indexEquals()

$stack = array(1, 2, 3);

$this->assertEquals(2, $stack[1]);

}

,然后

cmd的DOS窗口进入www目录下,

执行phpunit ArrayTest.php,回车执行即可

终于成功了