PHPUnit是unit家族中专门针对PHP做的一个单元测试工具

1.安装

见网上各种教程,pear安装方式, 

note:如果你的phpunit报出某文件未加载上或类不存在,请尝试修改php.ini 的 include_path, 将pear的路径加载进去,如我的php.ini:

#mac

include_path = "/usr/lib/php/pear"

 

2.文档

官方地址:http://www.phpunit.de/manual/current/en/index.html

command:

unitphp testfile.php

创建一个测试用例

你的测试类需要继承 PHPUnit_Framework_TestCase, 如:

class testMyClass extends PHPUnit_Framework_TestCase {
    //do something…... 
}

创建一个测试方法

需要测试的 method 以 test 开头,并且访问权限为public,如:

public function testInstance() {
    //do something…... 
} 

另一种方式是通过annotation方式指明此方法为测试方法:

/**
 * @test
 */ 
public funcion TheMethodIsTest() {
     //do something…... 
}

使用depends指定依赖关系, 如:

class testMyClass extends PHPUnit_Framework_TestCase 
{
  public function testClass()
  {
    $this->assertTrue(class_exists('myClass'));
    return new myClass();
  }

  /**
  * @depends getInstance
  */
  public function testInstance($instance)
  {
    $this->assertInstanceOf('myClass', $instance);
  }
}

note: 

1. depends 指定的依赖未测试通过时,后面的测试方法将跳过,停止执行, 标识为:S
2. 由于phpunit是按照声明顺序进行执行,你必须保证depends指定依赖在你的测试方法之前

使用 dataProvider 提供测试数据 如:

/** 
 * @dataProvider provider 
 */ 
public function testAdd($a, $b, $c) 
{ 
    $this->assertEquals($a, $b + $c); 
}
public function provider() 
{
    return array( array(2, 1, 1), array(3, 2, 1) );
}

note:

1. provider 方法必须为public

2. provider 支持迭代器

3. 同时指定dataProvider和depends时,参数的传递顺序为 dataProvider, depends

4. 依赖一个使用dataProvider的测试方法,必须有一个测试通过才能执行, 且将不会得到传值, 如:

/**
 * @dataProvider provider
 */
public testAdd($a, $b, $c)
{
     $this->assertEquals($a, $b + $c);
     return $a;
}
public function provider()
{
      return array(
               array(2, 1, 1),
               array(3, 2, 1)
      ); 
}
/**
 * @depends testAdd
 */
public function testResult($num)
{
     var_dump($num); // NULL
}

使用expectedException 测试异常 如:

/**
 * @expectedException Exception
 */
public function testException()
{
     throw new Exception();
}

或者这样:

/**
 * @expectedException Exception
 * @expectedExceptionMessage message body
 * @expectedExceptionCode 100
 */
public function testException()
{
     throw new Exception('message body', 100);
}

note:

1. expectedExceptionMessage/expectedExceptionCode可单独和expectedException使用,但expectedException必须存在

2. expectedExceptionMessage 指定的字符串只要存在在异常消息内, 不用全等于

3. expectedException可作用于php Error,转化的异常为:PHPUnit_Framework_Error, 如:

/**
 * @expectedException PHPUnit_Framework_Error
 */
public function testException()
{
     inculde 'File_Is_not_Exist.php';
}

也可以使用setExpectedException()方法设置测试异常:

public function testException()
{
     $this->setExpectedException('Exception', 'message body', 100);
     throw new Exception('message body', 100);
}

  

另外的一个办法:

public function testException() {
     try {
         // ... Code that is expected to raise an exception ...
     }
     catch (InvalidArgumentException $expected) {
         return;
     }
     $this->fail('An expected exception has not been raised.');
}

使用expectOutputString来捕获输出, 如:

public function testOutput()
{
     $this->expectOutputString('something');
     echo 'something';
}

note: 输出必须完全一致才能通过。

此外还有 expectOutputRegex('正则表达式'), setOutputCallback(回调函数)

setOutputCallback需要和其他两个搭配使用, 如:

public function testOutputUseCallback()
{
     $this->expectOutputString(json_encode(array(1,2,3)));
     $this->setOutputCallback(array($this, 'mycallback'));
     echo json_encode(array(1,2));
}

public function mycallback($string)
{
     $array = json_decode($string, true);
     $array[] = 3;
     return json_encode($array);
}

  

使用assertEquals()比较数字/浮点时,可指定差值, 比较数组时必须键值排序完全一致

其他断言语句参考官方网站。

断言结果输出:
. 断言成功 F 断言失败 E 发生错误 S 测试被忽略 I 测试被标记为不完整或尚未完成

使用 commend:

phpunit {{path/}}

执行整个目录下的测试用例, 可以通过phpunit.xml进行配置, 关于配置项, 请看这里:

http://www.phpunit.de/manual/current/en/appendixes.configuration.html

note: 请注意你是在哪个目录下执行的phpunit, phpunit.xml加载bootstrap时是依照phpunit执行时的所在目录,如果你指定的是相对目录的话。

 

其他的annotations, 请关注这里:
http://www.phpunit.de/manual/current/en/appendixes.annotations.html

开启php的xdebug , 可生成代码覆盖率,在你的unitphp.xml中指定生成html格式输出,可方便友好的查看结果。

使用以下特性使你的测试用例看起来更清爽 (特性方法的执行顺序如排列顺序):

setUpBeforeClass

setUp

assertPreConditions

{{testCaseMethod}}

assertPostConditions

tearDown

setUp

assertPreConditions

{{testCaseMethod}}

tearDown

onNotSuccessfulTest

tearDownAfterClass

 

这里是以上特性的说明:

http://www.phpunit.de/manual/current/en/fixtures.html

posted on 2013-03-24 17:31  Farmer.Li  阅读(280)  评论(0)    收藏  举报