按 Enter 键跳转到正文

PHPUnit使用指南

PHPUnit 使用指南

一、介绍

PHPUnit 是 PHP 领域最流行的单元测试框架,它受到 JUnit 的启发,为 PHP 开发者提供了编写和运行单元测试的工具和方法。使用 PHPUnit 可以带来以下好处:

  • 验证代码的正确性,确保函数和方法按预期工作
  • 便于代码重构,修改后可以快速验证是否破坏了现有功能
  • 帮助理解代码功能,测试用例本身就是一种文档
  • 支持测试驱动开发 (TDD) 流程

二、安装 PHPUnit

1. 安装 PHPUnit(可以通过 composer require --dev phpunit/phpunit 安装)

2. 配置PhpStorm 运行 PHPUnit(Docker环境为例)

2.1. 配置 Docker 远程解释器

  1. 打开 PhpStorm,进入 File > Settings > Build, Execution, Deployment > Docker
  2. 在右侧选择 Docker for Windows(通常会自动检测)
  3. 点击 Apply 确认配置,确保连接成功(底部会显示 “Connection successful”)
  4. 进入 File > Settings > Languages & Frameworks > PHP
  5. 点击 CLI Interpreter 右侧的 … 按钮,点击左上角 + 号,选择 From Docker, Vagrant, VM, WSL, Remote…
  6. 选择 Docker 并找到你的 php 容器
  7. 选择容器中的 PHP 可执行文件路径(如 /usr/local/bin/php)
  8. 点击 OK 完成配置,PhpStorm 会自动检测 PHP 版本和扩展

2.2 配置 PHPUnit

  1. 进入 File > Settings > Languages & Frameworks > PHP > Test Frameworks

  2. 点击 + 号,选择 PHPUnit by Remote Interpreter,选择之前配置的 Docker PHP 解释器

  3. 选择 PHPUnit 的配置方式:

    • 如果你有 phpunit.xml 文件:(正确修改修改xml文件)选择 Use configuration file 并指定路径

    • 如果你想使用 PhpStorm 的配置:选择 Use alternative configuration fileDefault configuration file

2.3 配置 Docker 容器中的路径映射

  1. 在 Docker 解释器配置中,点击 ... 按钮编辑

  2. Path mappings 部分,添加你的项目路径映射:

    • 本地路径:你的项目在主机上的路径(如 /Users/you/project

    • 远程路径:容器中的项目路径(如 /var/www/html

4. 运行 PHPUnit 测试

  1. 打开你的测试文件

  2. 右键点击测试类或方法

  3. 选择 Run 'TestName'Debug 'TestName'

  4. 或者使用 PhpStorm 的 Run 菜单运行所有测试

三、基础使用方法

1. 简单示例

  1. 创建被测试类 如

    1// src/Calculator.php
    2class Calculator
    3{
    4  public function add($a, $b)
    5  {
    6      return $a + $b;
    7  }
    8}
    
  2. 创建测试类

    测试类通常放在 tests 目录下,类名以 Test 结尾,继承 PHPUnit\Framework\TestCase。

     1// tests/CalculatorTest.php
     2use PHPUnit\Framework\TestCase;
     3
     4class CalculatorTest extends TestCase
     5{
     6    public function testAdd()
     7    {
     8        $calculator = new Calculator();
     9        $result = $calculator->add(2, 3);
    10        $this->assertEquals(5, $result);
    11    }
    12}
    
  3. 运行测试

    ./vendor/bin/phpunit tests/CalculatorTest.php
    

2. 常用断言方法

 1$this->assertEquals($expected, $actual);      // 值相等
 2$this->assertSame($expected, $actual);       // 类型和值都相等
 3$this->assertTrue($condition);               // 为 true
 4$this->assertFalse($condition);              // 为 false
 5$this->assertNull($variable);                // 为 null
 6$this->assertNotNull($variable);             // 不为 null
 7$this->assertEmpty($variable);               // 为空
 8$this->assertNotEmpty($variable);            // 不为空
 9$this->assertCount($expectedCount, $array);  // 数组元素数量
10$this->assertContains($needle, $haystack);   // 包含
11$this->assertInstanceOf($class, $object);    // 是某类的实例
12$this->assertStringContainsString($needle, $haystack); // 字符串包含

3. 使用 phpunit.xml 配置测试套件

1<!-- phpunit.xml -->
2<?xml version="1.0" encoding="UTF-8"?>
3<phpunit bootstrap="vendor/autoload.php">
4    <testsuites>
5        <testsuite name="Application Test Suite">
6            <directory>tests</directory>
7        </testsuite>
8    </testsuites>
9</phpunit>

运行整个测试套件:

1./vendor/bin/phpunit

4. 数据提供器

数据提供器允许你用不同的数据集运行同一个测试方法。

 1/**
 2 * @dataProvider additionProvider
 3 */
 4public function testAdd($a, $b, $expected)
 5{
 6    $this->assertSame($expected, (new Calculator())->add($a, $b));
 7}
 8
 9public function additionProvider()
10{
11    return [
12        [0, 0, 0],
13        [0, 1, 1],
14        [1, 0, 1],
15        [1, 1, 3] // 故意错误的数据
16    ];
17}

5. 测试依赖

测试方法可以依赖于其他测试方法的结果:

 1public function testEmpty()
 2{
 3    $stack = [];
 4    $this->assertEmpty($stack);
 5    return $stack;
 6}
 7
 8/**
 9 * @depends testEmpty
10 */
11public function testPush(array $stack)
12{
13    array_push($stack, 'foo');
14    $this->assertSame('foo', $stack[count($stack)-1]);
15    $this->assertNotEmpty($stack);
16    return $stack;
17}

6. 测试异常

测试代码是否抛出了预期的异常:

1public function testException()
2{
3    $this->expectException(InvalidArgumentException::class);
4    $this->expectExceptionMessage('Division by zero');
5    
6    $calculator = new Calculator();
7    $calculator->divide(10, 0);
8}

7. 测试输出

测试代码的输出:

1public function testOutput()
2{
3    $this->expectOutputString('Hello World');
4    echo 'Hello World';
5}

发表评论