Skills2026年4月27日·1 分钟阅读

PHPUnit — The Standard Testing Framework for PHP

PHPUnit is the de facto unit testing framework for PHP, providing assertions, mocks, and code coverage analysis.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
PHPUnit Overview
直接安装命令
npx -y tokrepo@latest install d812b4a6-4277-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

PHPUnit is the most widely adopted testing framework in the PHP ecosystem. Created by Sebastian Bergmann, it brings xUnit-style testing to PHP with a comprehensive assertion library, built-in mocking, and code coverage reporting. Almost every major PHP framework and CMS integrates with PHPUnit out of the box.

What PHPUnit Does

  • Provides over 60 assertion methods for values, arrays, strings, exceptions, and output
  • Includes a built-in mock object framework for stubs, spies, and expectations
  • Generates code coverage reports in HTML, Clover XML, and Cobertura formats
  • Supports data providers for parameterized test methods
  • Integrates with CI pipelines via JUnit XML output

Architecture Overview

PHPUnit runs as a CLI tool that discovers test classes extending TestCase. The runner reflects on each class to find methods prefixed with test or annotated with #[Test]. Each test method executes in an isolated instance so that object state does not persist between tests. Coverage collection uses either Xdebug, PCOV, or php-code-coverage as the driver, instrumenting source files to track executed lines and branches.

Self-Hosting & Configuration

  • Install globally via Composer or as a project-level dev dependency
  • Configure with phpunit.xml for test suites, bootstrap files, and coverage filters
  • Use --filter to run specific test methods or classes by name pattern
  • Enable --testdox for human-readable output that documents behavior
  • Set XDEBUG_MODE=coverage when collecting coverage with Xdebug 3+

Key Features

  • Rich assertion API covers types, arrays, JSON, XML, files, and regular expressions
  • Data providers generate multiple test runs from arrays or generators
  • Mock builder supports interface mocking, method stubbing, and invocation counting
  • Code coverage with line, branch, and path-level granularity
  • Attributes (PHP 8.1+) replace docblock annotations for cleaner metadata

Comparison with Similar Tools

  • Pest — a wrapper around PHPUnit with a more concise syntax; PHPUnit offers the underlying engine and full API
  • Codeception — higher-level acceptance and functional testing; PHPUnit focuses on unit and integration tests
  • PHPSpec — spec-driven development approach; PHPUnit follows the classic xUnit pattern
  • Behat — BDD with Gherkin syntax; PHPUnit provides assertion-based tests without a domain language layer
  • SimpleTest — an older PHP testing library now largely superseded by PHPUnit

FAQ

Q: Which PHP versions does PHPUnit support? A: PHPUnit 11 requires PHP 8.2+. Older PHPUnit branches support earlier PHP versions.

Q: How do I run PHPUnit tests in parallel? A: Use the paratest package, which wraps PHPUnit and distributes tests across multiple processes.

Q: Can PHPUnit test Laravel applications? A: Yes. Laravel ships with PHPUnit pre-configured and provides helper traits for HTTP, database, and queue testing.

Q: Does PHPUnit support snapshot testing? A: Not natively. Use the spatie/phpunit-snapshot-assertions package for snapshot comparisons.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产