xp-framework/unittest
最新稳定版本:v11.4.1
Composer 安装命令:
composer require xp-framework/unittest
包简介
Unittests for the XP Framework
README 文档
README
Unittests for the XP Framework
Writing a test
Tests reside inside a class and are annotated with the @test attribute.
use unittest\{Assert, Test}; class CalculatorTest { #[Test] public function addition() { Assert::equals(2, 1 + 1); } }
To run the test, use the test subcommand:
$ xp test CalculatorTest [.] ♥: 1/1 run (0 skipped), 1 succeeded, 0 failed Memory used: 1672.58 kB (1719.17 kB peak) Time taken: 0.000 seconds Assertion methods
The unittest package provides the following six assertion methods:
public abstract class unittest.Assert { public static void equals(var $expected, var $actual, string $error) public static void notEquals(var $expected, var $actual, string $error) public static void true(var $actual, string $error) public static void false(var $actual, string $error) public static void null(var $actual, string $error) public static void instance(string|lang.Type $type, var $actual, string $error) public static void throws(string|lang.Type $type, callable $block) }
If you need more than that, you can use xp-forge/assert on top of this library.
Setup and teardown
In order to run a method before and after the tests are run, annotate methods with the @before and @after attributes:
use unittest\{Assert, Before, After, Test}; class CalculatorTest { private $fixture; #[Before] public function newFixture() { $this->fixture= new Calculator(); } #[After] public function cleanUp() { unset($this->fixture); } #[Test] public function addition() { Assert::equals(2, $this->fixture->add(1, 1)); } }
Note: All test methods are run with the same instance of CalculatorTest!
Expected exceptions
The Expect attribute is a shorthand for catching exceptions and verifying their type manually.
use lang\IllegalArgumentException; use unittest\{Test, Expect}; class CalculatorTest { #[Test, Expect(IllegalArgumentException::class)] public function cannot_divide_by_zero() { (new Calculator())->divide(1, 0); } }
Ignoring tests
The Ignore attribute can be used to ignore tests. This can be necessary as a temporary measure or when overriding a test base class and not wanting to run one of its methods.
use unittest\{Test, Ignore}; class EncodingTest { #[Test, Ignore('Does not work with all iconv implementations')] public function transliteration() { /* ... */ } }
Parameterization
The Values attribute can be used to run a test with a variety of values which are passed as parameters.
use lang\IllegalArgumentException; use unittest\{Test, Expect, Values}; class CalculatorTest { #[Test, Expect(IllegalArgumentException::class), Values([1, 0, -1])] public function cannot_divide_by_zero($dividend) { (new Calculator())->divide($dividend, 0); } }
Actions
To execute code before and after tests, test actions can be used. The unittest library comes with the following built-in actions:
unittest.actions.ExtensionAvailable(string $extension)- Verifies a given PHP extension is loaded.unittest.actions.IsPlatform(string $platform)- Verifies tests are running on a given platform via case-insensitive match onPHP_OS. Prefix with!to invert.unittest.actions.RuntimeVersion(string $version)- Verifies tests are running on a given PHP runtime. See version_compare for valid syntax.unittest.actions.VerifyThat(function(): var|string $callable)- Runs the given function, verifying it neither raises an exception nor return a false value.
use unittest\actions\{IsPlatform, VerifyThat}; use unittest\{Test, Action}; class FileSystemTest { #[Test, Action(eval: 'new IsPlatform("!WIN")')] public function not_run_on_windows() { // ... } #[Test, Action(eval: 'new VerifyThat(fn() => file_exists("/\$Recycle.Bin");')] public function run_when_recycle_bin_exists() { // ... } }
Multiple actions can be run around a test by passing an array to the @action attribute.
Further reading
- XP RFC #0283: Unittest closure actions
- XP RFC #0272: Unittest actions
- XP RFC #0267: Unittest parameterization
- XP RFC #0188: Test outcome
- XP RFC #0187: @expect withMessage
- XP RFC #0150: Before and after methods for test cases
- XP RFC #0145: Make unittests strict
- XP RFC #0059: Timeouts for unittests
- XP RFC #0032: Add attributes for Unittest API
- XP RFC #0020: Metadata for unittests
xp-framework/unittest 适用场景与选型建议
xp-framework/unittest 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 300.3k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「module」 「xp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xp-framework/unittest 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xp-framework/unittest 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xp-framework/unittest 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
bootstrap select field module implementing http://silviomoreto.github.io/bootstrap-select/
Yii2 module to manage website content. Kind of a CMS...
Codeurx Modular is simply a package for Laravel to help you create and manage modules.
User Module for the Attogram Framework
Info Module for the Attogram Framework
Contact Form Module for the Attogram Framework
统计信息
- 总下载量: 300.3k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 25
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2026-01-04

