PHPUnit介绍及安装

PHPUnit学习笔记(一)PHPUnit介绍及安装

最近学习并在项目中运用了PHPUnit做自动化测试,我将在博客上基于我的PHPUnit学习笔记进行连载,详细的介绍这个自动化测试框架。笔记内容基本上基于PHPUnit的官方文档和例子,里面加上我自己理解的翻译和配合描述代码。本笔记使用的PHPUnit版本为3.5.13, 测试平台为ubuntu10.10 PHP5.3.3

什么是PHPUnit?

PHPUnit是一个轻量级的PHP测试框架。它是在PHP5下面对JUnit3系列版本的完整移植,是xUnit测试框架家族的一员(它们都基于模式先锋Kent Beck的设计)

PHPUnit的安装

Linux各大发行版本基本上都带有phpunit的包,安装非常方便,例如ubuntu下直接运行

sudo apt-get install phpunit

即可安装好phpunit

我重点讲一下windows下面phpunit的安装流程,相对linux,windows的要麻烦的多

1. 按照常规下载php的zip包和配置好php.ini,这里的例子使用的是C:\php-5.2.17-Win32

2. 把你的php目录加入系统环境变量path中,重启系统

点击查看原图

2. 开始 运行 输入 cmd,然后切换到你的php目录,我当前的就是C:\php-5.2.17-Win32

3. 输入go-pear.bat

首先脚本会询问是把pear安装为系统范围的还是本地拷贝,这里我们默认选择系统,直接回车即可

点击查看原图

4. 这时显示当前的路径配置,并询问你是否修改,我们保持默认依然回车即可,回车后脚本就会开始自动安装pear库了

点击查看原图

5. 安装的时候脚本会提示你设定php.ini的里面include_path,我们按照要求在php.ini里面设置好,设置好后回车即可

点击查看原图

6. 最后脚本会提醒你导入pear的系统变量注册文件,这个文件就在你的php目录中

点击查看原图

7. 输入回车,pear的安装就完成了, 测试pear是否装好,可以直接在命令行输入pear,如果你看到下列的输出,那就是ok了

点击查看原图

8. 开始安装phpunit,首先升级pear,输入命令

pear upgrade pear

9. 再依次输入下列命令添加pear的频道, 添加的时候可能会因为网络问题可能会提示失败,多试几次即可

pear channel-discover components.ez.no 

pear channel-discover pear.phpunit.de

pear channel-discover pear.symfony-project.com

小提示:

添加时如果出现下列错误提示,请在php.ini里面开启 php_openssl.dll 这个扩展

Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?

10. 输入下列命令开始安装phpunit,同样,安装的时候因为网络问题可能会提示失败,多试几次即可

pear install --alldeps --force phpunit/phpunit

11. 待命令运行完毕后,phpunit就安装好了,我们可以通过输入phpunit来测试是否安装成功

点击查看原图

如果你输入php出现上图的显示,那么你的phpunit就安装完成了。


PHPUnit学习笔记(二)PHPUnit基本用法

上篇日记中我介绍了phpunit的配置方法,今天就开始介绍如何用PHPUnit来编写测试用例,下面我来引入一个测试PHP数组操作的测试用例,这个例子会给你展示PHPUnit常规的用法和测试用例编写的步骤。当前目标主要是对PHPUnit的基本使用有一个大概的了解,类似开发程序教程中的Hello World程序,知道程序怎么写,怎么去运行。

编写一个栈测试的例子的步骤:

1. 定义一个测试类StackTest保存于StackTest.php中

2. 这个类继承于PHPUnit_Framework_TestCase

3. 定义类中的测试方法,类的所有的测试用例方法需要用test开头,当然,你也可以在使用@test注释来定义一个名字不为test开头的方法为测试方法

4. 在这些测试方法中,我们需要使用断言方法(assertion methods)如assertEquals()来断言实际传入的参数和期待的参数的值相同来达到测试的目的.

下面我们来看代码:

点击查看原图

例子代码中我使用断言方法assertEquals来断言我期待$stack相关的值

注意代码中高亮的方法indexEquals,它并没有使用test开头,但是我在注释中增加了@test标签,那么phpunit依然会将其作为一个测试方法运行,下面我们来运行方法和运行结果

进入命令行,使用phpunit StackTest来执行StackTest.php的测试,运行结果如下:

点击查看原图

.F 表示执行完毕且出现断言失败

Time: 0 seconds, Memory: 5.50Mb

表示执行时间为0秒,使用5.50MB内存

There was 1 failure:

这里有一个失败

1) StackTest::indexEuqals

StackTest类的indexEquals方法


Failed asserting that <integer:1> matches expected <integer:2>.

断言值为2但是传入的值却是1

/home/colt/workspace/PHP/test/StackTest.php:28

FAILURES!
Tests: 2, Assertions: 6, Failures: 1.

执行了2个测试,共6个断言,失败1

由于我们在indexEquals断言$stack索引0的值为2,但是实际上值的是1,所以这个断言没有通过测试并向我们报告了失败。

本例子通过一个简单易懂的测试类解开测试的神秘面纱:

通俗的讲,单元测试就是在测试用例类中,定义一系列的测试方法,在方法中使用断言(assert)来测试你程序中的相关函数、类、接口、过程的执行结果是否和你预期的是一样的,如果某个部分的执行结果没有与你期待的结果相同,PHPUnit就会向你报告问题,你也可以方便的根据报告确认和修复程序中的Bug。针对你的程序写好详细的完整单元测试,你就不用每次完成一个新功能后逐个的测试你软件的所有功能,而且在程序发布之前,你至少可以通过测试消灭大部分的内部逻辑Bug和缺陷。同样,你在之后的维护开发中,如果在某个功能的修改中不小心改坏了与其相关连的其他模块,那么先前完善的单元测试也会向你报告出相关的问题让你及时发现和修复问题。


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

在上篇日记中我介绍了写一个基本的测试类的方法,今天在基本测试类的基础上,再来介绍测试方法的进阶功能。

测试方法间的依赖关系

PHPUnit可以实现测试方法的依赖关系,也就是说,一个测试方法的参数的内容和是否会运行依赖于另外一个测试方法结果,依赖关系通过注释@depends来定义.这个特性一般用于检查代码的逻辑过程,一个逻辑的执行前提是另外一个逻辑的执行结果.下面来看代码例子:

点击查看原图

在上面的测试类StackTest中,定义了2个依赖测试方法,testPush依赖于testEmpty, testPop依赖于testPush.那么在测试运行时,testEmpty中的断言执行完毕也没有出现问题时,方法中的return语句会将 $stack传给依赖于它的testPush,作为testPush的参数传入到testPush中,testPush执行完毕之后,也会将$stack 传给依赖于它的testPop,只要断言检查没有出现异常,那么PHPUnit就会根据依赖关系依次执行依赖的测试方法,直到依赖关系结束为止.

另外,为了方便快速的确定问题的所在,如果某个测试方法依赖的方法测试没有功过,那么PHPUnit会自动跳过后面所有的依赖测试,下面看代码例子:

点击查看原图

testOne中断言为True,但是传入的是false,testOne的测试不会通过,那么依赖于testOne的testTwo也会被自动跳过.

数据提供者(Data Providers)

在前面的例子我们可以看到:测试方法是可以有参数的,在依赖关系中参数的值是它依赖的测试方法传入.那么某个测试方法没有依赖的方法,我们怎么给它传入参数做测试呢?PHPUnit给我们提供了数据提供者方法来为测试方法传入数据.

数据提供者方法需要定义在当前的测试类中,在测试方法的注释中使用@dataProvider标签标注给它提供数据的方法名,定义之后PHPUnit会自动的将数据提供者方法返回的数据依次传入到测试方法中测试.

数据提供者相当于都取大量数据测试时的封装

下面我们来看代码:

点击查看原图

在上面的代码中,方法add_provider就是测试方法testAdd的数据提供者方法,它会依次给testAdd传入4组测试数据,testAdd会测试4次.

在这里要注意的是数据提供者方法返回数据的格式: 需要返回的是2维数组,第二维数组值的位置,对应测试方法参数的位置,参数个数和数组长度要相等,否则PHPUnit会报错,下面是个用于理解的简单的例子:

return array(
     array(参数1,参数2,参数3,参数4,参数N),
     array(参数1,参数2,参数3,参数4,参数N),
);

除了数组外,PHPUnit还支持数据提供者方法返回迭代器对象,迭代器的介绍估计大部分PHPer比较陌生,详细的介绍可以去 http://php.net/manual/en/class.iterator.php 察看
,下面我们来看代码(点击图片放大察看):

点击查看原图

和前一个例子实现的测试内容相同,但是数据提供者返回的是一个迭代器对象而不是是数组


数据提供者方法和依赖关系的限制

当一个测试方法依赖于另外一个使用data providers测试方法时,这个测试方法将会在它依赖的方法至少测试成功一次后运行,同时使用data providers的测试方法的执行的结果不能传入一个依赖它的测试方法中.这个解释来自官方的文档,理解起来可能有点难,我们通过代码来描述下这个限制:

点击查看原图

上面代码例子中:

情况1: add_provider提供的数据至少有一对数据相等

testC会执行一次, 因为testC是依赖于testB的, 但是testB使用了数据提供者方法,那么testC中是无法收到testB return的值的

情况2: add_provider提供的数据没有一对数据相等

testC永远不会执行

测试异常

有时候我们需要测试某些情况下代码是否按照要求抛出了相关的异常.在PHPUnit中,我们有3种方式来检查异常是否抛出.

方法一: 注释法, 用@expectedException 标定期待的异常
/**
 * @expectedException InvalidArgumentException
 */  
public function  testException()
{
}

方法二: 设定法,使用 $this->setExpectedException 设定期待的异常
public function  testException()
{
    $this->setExpectedException('InvalidArgumentException');
}

方法三: try catch + fail法
public function  testException() {
     try {
         // 这里写上会引发异常的代码 
    }  catch (InvalidArgumentException $expected) {
         // 抓到异常测试通过 
         return ;
    }

     // 没抓到异常就算失败  
    $this->fail( 'An expected exception has not been raised.'  );
}

测试PHP错误

有时候我们的代码在运行时会出现php错误,如整除0,文件不存在等等.在PHPUnit中,它会自动把错误转换为异常PHPUnit_Framework_Error并抛出,我们只需要在测试方法中设定抓取这个异常即可:

/**
 * @expectedException PHPUnit_Framework_Error // 期待PHPUnit_Framework_Error的异常
 */  
public function  testFailingInclude()
{
     // include一个不存在的文件,就会抛出一个PHPUnit_Framework_Error的异常 
     include 'not_existing_file.php'; 
}


PHPUnit学习笔记(四)断言详解

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

assertArrayHasKey(mixed $key, array $array[, string $message = '']) 
断言数组$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参数详解:

    本文直接翻译自PHPUnit官方文档,个人翻译水平有限,可能会存在某些词和意思翻译不准的地方,进请谅解!

    Runs the tests that are provided by the class UnitTest. This class is expected to be declared in the UnitTest.php sourcefile.

    UnitTest must be either a class that inherits from PHPUnit_Framework_TestCase or a class that provides a public static suite() method which returns an PHPUnit_Framework_Test object, for example an instance of the PHPUnit_Framework_TestSuite class.
    运行类UnitTest提供的所有测试,这个类需要定义在UnitTest.php这个文件中.
    类UnitTest需要从PHPUnit_Framework_TestCase继承或者提供一个公开的静态方法suite()返回一个PHPUnit_Framework_Test对象的实例.
    
phpunit UnitTest Test.php
    Runs the tests that are provided by the class UnitTest. This class is expected to be declared in the specified sourcefile.
    运行UnitTest提供的所有测试,这个类应该在定义在Test.php中
    
--log-junit

    Generates a logfile in JUnit XML format for the tests run.
    生成JUnit XML格式的日志文件


--log-tap

    Generates a logfile using the Test Anything Protocol (TAP) format for the tests run. 
    生成TAP格式的日志文件
    
--log-dbus

    Log test execution using DBUS.
    使用DBUS记录测试的执行情况


--log-json

    Generates a logfile using the JSON format.
    生成JSON格式的日志文件
    
--coverage-html

    Generates a code coverage report in HTML format. 
    生成html格式的代码覆盖报告


    Please note that this functionality is only available when the tokenizer and Xdebug extensions are installed.
    请注意这个功能只能在tokenizer和Xdebug安装后才能使用


--coverage-clover

    Generates a logfile in XML format with the code coverage information for the tests run. 
    生成xml格式的代码覆盖报告


    Please note that this functionality is only available when the tokenizer and Xdebug extensions are installed.
    请注意这个功能只能在tokenizer和Xdebug安装后才能使用


--testdox-html and --testdox-text

    Generates agile documentation in HTML or plain text format for the tests that are run. 
    生成记录已运行测试的html或者纯文本格式的文件文档


--filter

    Only runs tests whose name matches the given pattern. The pattern can be either the name of a single test or a regular expression that matches multiple test names.


    只运行名字符合参数规定的格式的测试,参数可以是一个测试的名字或者一个匹配多个测试名字的正则表达式


--group

    Only runs tests from the specified group(s). A test can be tagged as belonging to a group using the @group annotation.
    只运行规定的测试组,一个测试可以使用@group注释来分组
    The @author annotation is an alias for @group allowing to filter tests based on their authors.
    @author注视是一个和@group关联的注释标签,用来根据作者来过滤测试


--exclude-group

    Exclude tests from the specified group(s). A test can be tagged as belonging to a group using the @group annotation.
    只包含规定的多个测试组,一个测试可以使用@group注释来分组


--list-groups

    List available test groups.
    列出可用的测试组
    
--loader

    Specifies the PHPUnit_Runner_TestSuiteLoader implementation to use.
    定义使用PHPUnit_Runner_TestSuiteLoader的接口


    The standard test suite loader will look for the sourcefile in the current working directory and in each directory that is specified in PHP's include_path configuration directive. Following the PEAR Naming Conventions, a class name such as Project_Package_Class is mapped to the sourcefile name Project/Package/Class.php.
    标准的standard test suite loader会在当前的目录和php的include_path中根据PEAR的命名规则的类,一个叫做Project_Package_Class的类 会指向到文件Project/Package/Class.php
    
--repeat

    Repeatedly runs the test(s) the specified number of times.
    根据定义的数字重复运行测试
    
--tap

    Reports the test progress using the Test Anything Protocol (TAP).
    使用Test Anything Protocol格式报告测试进程
    
--testdox

    Reports the test progress as agile documentation.
    使用agile documentation格式报告测试进程


--colors

    Use colors in output.
    在输出结果中使用颜色


--stderr

    Optionally print to STDERR instead of STDOUT.
    使用STDERR替代STDOUT输出结果


--stop-on-error

    Stop execution upon first error.
    在遇到第一个错误时停止执行
    
--stop-on-failure

    Stop execution upon first error or failure.
    在遇到第一个失败时停止执行
    
--stop-on-skipped

    Stop execution upon first skipped test.
    在遇到第一个跳过的测试时停止执行


--stop-on-incomplete

    Stop execution upon first incomplete test.
    在遇到第一个未完成的测试时停止执行


--strict

    Mark a test as incomplete if no assertions are made.
    当一个测试没有定义任何断言时将其标记为未完成的测试


--verbose

    Output more verbose information, for instance the names of tests that were incomplete or have been skipped.
    输出例如未完成的测试的名字,跳过的测试的名字


--wait

    Waits for a keystroke after each test. This is useful if you are running the tests in a window that stays open only as long as the test runner is active.
    在每个测试开始之前等待用户按键,这个在你一个保持打开的窗口中运行很长的测试时很有帮助


--skeleton-class

    Generates a skeleton class Unit (in Unit.php) from a test case class UnitTest (in UnitTest.php).
    从一个测试类中生成一个概要测试类
    
--skeleton-test

    Generates a skeleton test case class UnitTest (in UnitTest.php) for a class Unit (in Unit.php). See Chapter 17 for more details.
    在Unit.php内为类Unit生成一个概要测试类UnitTest


--process-isolation

    Run each test in a separate PHP process.
    在多个php进程中运行所有测试


--no-globals-backup

    Do not backup and restore $GLOBALS.
    不备份和还原$GLOBALS变量


--static-backup

    Backup and restore static attributes of user-defined classes.
    备份和还原用户定义的类中的静态变量


--syntax-check

    Enables the syntax check of test source files.
    对测试的代码文件开启语法检查


--bootstrap

    A "bootstrap" PHP file that is run before the tests.
    定义测试前运行的bootstrap的php文件的路径


--configuration, -c

    Read configuration from XML file. See Appendix C for more details.
    从xml文件中读取配置,增加-c参数看更多的内容

    If phpunit.xml or phpunit.xml.dist (in that order) exist in the current working directory and --configuration is not used, the configuration will be automatically read from that file.
    如果phpunit.xml或phpunit.xml.dist(根据这个模式)在当前的目录中存在且--configuration参数没有使用的时候,配置信息会被自动读取


--no-configuration

    Ignore phpunit.xml and phpunit.xml.dist from the current working directory.
    自动跳过当前目录的phpunit.xml和phpunit.xml.dist配置文件


--include-path

    Prepend PHP's include_path with given path(s).
    在php的include_path增加路径


-d

    Sets the value of the given PHP configuration option.
    定义php的配置属性


--debug

    Output debug information such as the name of a test when its execution starts.
    输出调试信息如测试的名称及该测试什么时候开始执行
    
Note
提示
When the tested code contains PHP syntax errors, the TextUI test runner might exit without printing error information. 
The standard test suite loader can optionally check the test suite sourcefile for PHP syntax errors, but not sourcefiles included by the test suite sourcefile.
当测试代码中含有php语法错误的时候,测试器会退出且不会打印任何错误信息,standard test suite loader可选择性检查测试文件源代码的PHP语法错误,但是不会检查测试文件中引入的其他的代码文件的语法错误



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值