ns-backend-tests
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCreate Backend Tests
创建后端测试
Execution-phase PHPUnit implementation. Planning-phase: .
ns-spec-driven/references/unit-test-task-generator.md执行阶段的PHPUnit实现。规划阶段请参考:。
ns-spec-driven/references/unit-test-task-generator.mdSession boot
会话启动
Load and when present. See for layout, run contract, and bootstrap summary.
.nextstage-harness/rules/backend-tests-rules.mdcbackend-rules.mdcreferences/backend-testing-architecture.md若存在和文件,请加载。关于布局、运行约定和初始化概要,请查看。
.nextstage-harness/rules/backend-tests-rules.mdcbackend-rules.mdcreferences/backend-testing-architecture.mdPhase 0 — Resolve or bootstrap backend test root
阶段0 — 确定或初始化后端测试根目录
Before any test class code, locate the PHPUnit project or create minimal structure.
在编写任何测试类代码之前,定位PHPUnit项目或创建最小化结构。
0.1 — Detect existing tests (brownfield)
0.1 — 检测现有测试(遗留项目)
Search the repo in this order:
backend/phpunit.xml- at repo root
phpunit.xml - or
tests/Unit/under the directory that containstests/Feature/with PHPUnitcomposer.json
Record the directory containing as . All paths below are relative to .
phpunit.xml{backend_root}{backend_root}按以下顺序搜索仓库:
backend/phpunit.xml- 仓库根目录下的
phpunit.xml - 包含带有PHPUnit依赖的的目录下的
composer.json或tests/Unit/tests/Feature/
将包含的目录记录为****。以下所有路径均相对于。
phpunit.xml{backend_root}{backend_root}0.2 — Bootstrap when nothing exists (greenfield)
0.2 — 无测试时初始化(全新项目)
If backend application code exists (, , or equivalent) but no and no :
app/src/phpunit.xmltests/| Rule | Detail |
|---|---|
| Location | |
| Tree | Per |
| Dependencies | PHPUnit via existing |
Then set to that directory.
{backend_root}Forbidden on greenfield: assuming tests exist, running PHPUnit on the host, or scaffolding tests outside the backend package.
若存在后端应用代码(、或等效目录)但无和目录:
app/src/phpunit.xmltests/| 规则 | 详情 |
|---|---|
| 位置 | 若存在 |
| 目录结构 | 遵循 |
| 依赖 | 通过已有的 |
随后将设置为该目录。
{backend_root}全新项目禁止操作: 假设测试已存在、在宿主机上运行PHPUnit、或在后端包外搭建测试架构。
0.3 — Post-bootstrap documentation
0.3 — 初始化后文档更新
When or exists, add or update the backend test row: , docker test container, and the PHPUnit run command from Phase 5.
docs/context/stack-confirmed.mdarchitecture-rules.md{backend_root}Do not proceed to Phase 1 until is confirmed and is readable.
{backend_root}phpunit.xml若存在或文件,添加或更新后端测试行:、Docker测试容器,以及阶段5中的PHPUnit运行命令。
docs/context/stack-confirmed.mdarchitecture-rules.md{backend_root}在确认且可读之前,请勿进入阶段1。
{backend_root}phpunit.xmlPhase 1 — Discovery (before feature code)
阶段1 — 调研(功能代码编写前)
- Code under test — read implementation files (Service, FormRequest, Controller, etc.)
- Existing tests — read matching files under and
tests/Unit/; extend, do not duplicatetests/Feature/ - Project patterns — factories, , response envelope, multitenancy helpers from harness rules
RefreshDatabase - Task contract — when implementing a unit-test planning task, follow its scenario checklist
- 被测代码 — 阅读实现文件(Service、FormRequest、Controller等)
- 现有测试 — 阅读和
tests/Unit/下的匹配文件;扩展而非重复编写tests/Feature/ - 项目模式 — 从测试框架规则中获取工厂类、、响应包、多租户助手等内容
RefreshDatabase - 任务约定 — 若实现单元测试规划任务,请遵循其场景检查清单
Phase 2 — Write tests
阶段2 — 编写测试
- Unit: mock dependencies — no real DB/queue/cache I/O
- Feature: HTTP tests with auth, 401/403, cross-tenant P0 when applicable
- Naming: , descriptive snake_case method names
{Component}Test.php - Match module paths: ,
tests/Unit/Modules/{Domain}/tests/Feature/Modules/{Domain}/
- 单元测试:模拟依赖 — 不进行真实的数据库/队列/缓存I/O操作
- 功能测试:带认证的HTTP测试,必要时覆盖401/403、跨租户P0场景
- 命名规则:,方法名使用描述性蛇形命名(snake_case)
{Component}Test.php - 模块路径匹配:、
tests/Unit/Modules/{Domain}/tests/Feature/Modules/{Domain}/
Phase 3 — Run (mandatory contract)
阶段3 — 运行(强制约定)
See also .
../../ns-harness/references/docker-and-testing.md- Run — confirm the documented test container (ask once if undocumented)
docker ps - Execute inside the test container at workdir:
{backend_root}
bash
timeout 120 docker exec -w {workdir} {test_container} \
vendor/bin/phpunit --testdox --stop-on-failure --stop-on-error {optional_filter}- Timeout: if the command is killed at 120s (e.g. exit 124), treat the run as dead/hung — abort, report blocker, do not silently retry
- Never run or
vendor/bin/phpuniton the hostphpunit.sh
Filtered runs (single class or method) use the same flags and timeout.
另请参考。
../../ns-harness/references/docker-and-testing.md- 运行— 确认文档中记录的测试容器(若未记录则询问一次)
docker ps - 在工作目录下的测试容器内执行:
{backend_root}
bash
timeout 120 docker exec -w {workdir} {test_container} \
vendor/bin/phpunit --testdox --stop-on-failure --stop-on-error {optional_filter}- 超时处理: 若命令在120秒时被终止(例如退出码124),则判定运行无响应/挂起 — 终止操作,上报阻塞问题,请勿静默重试
- 禁止在宿主机上运行或
vendor/bin/phpunitphpunit.sh
过滤运行(单个类或方法)需使用相同的参数和超时设置。
Phase 4 — Report
阶段4 — 报告
- Pass: list files added/changed
- Fail: first failing test name + assertion from output
--testdox - Hung/timeout: state 120s limit exceeded; invoke if root cause unclear
ns-investigator
- 通过:列出新增/修改的文件
- 失败:第一个失败测试的名称 + 输出中的断言信息
--testdox - 无响应/超时:说明已超过120秒限制;若根因不明确则调用
ns-investigator
References
参考文档
| File | When |
|---|---|
| Layout, bootstrap, run contract |
| Task contract source |
| 文件 | 使用场景 |
|---|---|
| 布局、初始化、运行约定 |
| 任务约定来源 |
Related skills
相关技能
ns-spec-driven— planning tasksreferences/unit-test-task-generator.md- — failing or hung test debugging
ns-investigator - — ad-hoc fixes outside planned unit-test tasks
ns-coder
ns-spec-driven— 规划任务references/unit-test-task-generator.md- — 失败或挂起测试的调试
ns-investigator - — 计划外单元测试任务的临时修复
ns-coder