Chinaunix首页 | 论坛 | 博客
  • 博客访问: 674511
  • 博文数量: 132
  • 博客积分: 10060
  • 博客等级: 上将
  • 技术积分: 1732
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-21 12:35
个人简介

迷惘的码农。

文章分类

全部博文(132)

文章存档

2013年(1)

2011年(2)

2010年(9)

2009年(41)

2008年(79)

我的朋友

分类:

2008-03-25 10:47:36

第 4 章 编写PHPUnit测试

显示我们必须如何修改中的2各测试以使它们可用于PHPUnit。

范例 4.1: 使用PHPUnit测试数组和sizeof()


require_once 'PHPUnit/Framework.php';
 
class ArrayTest extends PHPUnit_Framework_TestCase
{
    public function testNewArrayIsEmpty()
    {
        // 创建数组fixture。
        $fixture = array();
 
        // 断言数组fixture的尺寸是0。
        $this->assertEquals(0, sizeof($fixture));
    }
 
    public function testArrayContainsAnElement()
    {
        // 创建数组fixture。
        $fixture = array();
 
        // 向数组fixture增加一个元素。
        $fixture[] = 'Element';
 
        //断言数组fixture的尺寸是1。
        $this->assertEquals(1, sizeof($fixture));
    }
}
?>

 

只要你想到输入一些东西到print语句或调试表达式中,就用测试代替它。

 
 --Martin Fowler

显示使用PHPUnit编写测试的基本步骤:

  1. 将类Class的测试写入类ClassTest

  2. ClassTest继承(通常)自PHPUnit_Framework_TestCase

  3. 测试都是公用方法,命名为test*

    另外,你可以在方法的文档注释块(docblock)中使用@test注解来把它标记为测试方法。

  4. 在测试方法中,类似assertEquals() (见)的断言方法用来断言一个实际值(应该)匹配一个期望值。

数据提供者

测试方法能接受人意的参数。这些参数将由一个数据提供者方法提供(中的provider())。 要用到的数据提供着方法是用@dataProvider注解指定的。

数据提供者方法必须是publicstatic的,而且返回值必须是数组的数组或者实现了Iterator接口并且每次迭代产生一个数组的对象。对于每个数组(即集合的部分),都将以其内容为参数调用测试方法。

范例 4.2: 使用数据提供者


class DataTest extends PHPUnit_Framework_TestCase
{
    public static function provider()
    {
        return array(
          array(0, 0, 0),
          array(0, 1, 1),
          array(1, 0, 1),
          array(1, 1, 3)
        );
    }
 
    /**
     * @dataProvider provider
     */
    public function testAdd($a, $b, $c)
    {
        $this->assertEquals($c, $a + $b);
    }
}
?>
phpunit DataTest
PHPUnit 3.2.10 by Sebastian Bergmann.

...F

Time: 0 seconds

There was 1 failure:

1) testAdd(DataTest) with data (1, 1, 3)
Failed asserting that matches expected value .
/home/sb/DataTest.php:21

FAILURES!
Tests: 4, Failures: 1.

测试异常

显示如何使用@expectedException注解测试被测试的代码中是否跑出异常。

范例 4.3: 使用@expectedException注解


require_once 'PHPUnit/Framework.php';
 
class ExceptionTest extends PHPUnit_Framework_TestCase
{
    /**
     * @expectedException InvalidArgumentException
     */
    public function testException()
    {
    }
}
?>
phpunit ExceptionTest
PHPUnit 3.2.10 by Sebastian Bergmann.

F

Time: 0 seconds

There was 1 failure:

1) testException(ExceptionTest)
Expected exception InvalidArgumentException

FAILURES!
Tests: 1, Failures: 1.

另外,可用setExpectedException()方法设定期望的异常,如所示。

范例 4.4: 期望被测试代码引起异常


require_once 'PHPUnit/Framework.php';
 
class ExceptionTest extends PHPUnit_Framework_TestCase
{
    public function testException()
    {
        $this->setExpectedException('InvalidArgumentException');
    }
}
?>
phpunit ExceptionTest
PHPUnit 3.2.10 by Sebastian Bergmann.

F

Time: 0 seconds

There was 1 failure:

1) testException(ExceptionTest)
Expected exception InvalidArgumentException

FAILURES!
Tests: 1, Failures: 1.

现是可用于测试异常的方法。

表 4.1. 用于测试异常的方法

方法含义
void setExpectedException(string $exceptionName)设置期望的异常名称为$exceptionName
String getExpectedException()返回期望的异常名称。

你也可以使用中显示的方法测试异常。

范例 4.5: 测试异常的另一方法


require_once 'PHPUnit/Framework.php';
 
class ExceptionTest extends PHPUnit_Framework_TestCase {
    public function testException() {
        try {
            // ... 期望引发异常的代码 ...
        }
 
        catch (InvalidArgumentException $expected) {
            return;
        }
 
        $this->fail('未引发期望的异常。');
    }
}
?>

如果中期望引发异常的代码没有引发期望的异常,那么后面对fail() (见)的调用将终端测试并发信号报告测试问题。如果引发了期望的异常,将执行catch块,测试将成功结束。

阅读(2249) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~