PHPUnit 详解

本文介绍了 PHP 单元测试框架 PHPUnit

官方网站:https://phpunit.de/

GitHub:https://github.com/sebastianbergmann/phpunit

安装

composer 引入执行命令。

1
$ composer require --dev phpunit/phpunit

使用

1
2
3
4
5
6
7
8
9
10
11
use PHPUnit\Framework\TestCase;

// 被测试的类加 Test

class ClassTest extends TestCase
{
// test 加被测试的函数

public function testFunction()
// code
}

不用另外编写测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Calculator  
{
/**
* @assert (0, 0) == 0
* @assert (0, 1) == 1
* @assert (1, 0) == 1
* @assert (1, 1) == 2
*/

public function sum($a, $b)
{
return $a + $b;
}
}
1
$ phpunit --skeleton Calculator.php

命令参数

1
--coverage-* # clover crap4j html php text  为运行的测试生成带有代码覆盖率信息的日志文件

示例

0%