godot-testing
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGodot Testing
Godot 测试
This skill covers test-driven development (TDD) for Godot 4.3+ projects using GUT (Godot Unit Testing) and gdUnit4. It includes framework selection, full RED-GREEN-REFACTOR examples, test structure, running tests in CI, and common testing patterns.
Related skills: godot-code-review for review checklists, dependency-injection for test-friendly architecture, export-pipeline for CI/CD test automation.
本 skill 介绍如何在 Godot 4.3+ 项目中使用 GUT(Godot Unit Testing)和 gdUnit4 进行测试驱动开发(TDD)。内容包括框架选择、完整的 RED-GREEN-REFACTOR 示例、测试结构、在 CI 中运行测试,以及常见的测试模式。
相关 skill: godot-code-review 提供审查清单,dependency-injection 介绍便于测试的架构,export-pipeline 介绍 CI/CD 测试自动化。
Framework Selection
框架选择
| Feature | GUT | gdUnit4 |
|---|---|---|
| Language | GDScript-first, limited C# | GDScript + C# (first-class) |
| Install | AssetLib or git submodule | AssetLib or git submodule |
| Editor integration | Built-in GUT panel | Built-in inspector + panel |
| Mocking | | |
| Scene testing | | |
| CI support | | |
| C# support | Minimal (GDScript wrappers only) | Native C# assertions + lifecycle |
| Maturity | Established (Godot 3 + 4) | Godot 4 focused, actively updated |
| Best for | Pure GDScript projects | Mixed GDScript/C# or C#-only |
Rule of thumb: Use GUT for GDScript-only projects. Use gdUnit4 for C# projects or when you need first-class C# support and scene runner utilities.
| 特性 | GUT | gdUnit4 |
|---|---|---|
| 语言 | 以 GDScript 为主,仅有限支持 C# | GDScript + C#(一等支持) |
| 安装 | AssetLib 或 git submodule | AssetLib 或 git submodule |
| 编辑器集成 | 内置 GUT panel | 内置 inspector + panel |
| Mock | | |
| 场景测试 | | |
| CI 支持 | | |
| C# 支持 | 最低限度支持(仅 GDScript 包装器) | 原生 C# 断言 + 生命周期 |
| 成熟度 | 成熟(支持 Godot 3 + 4) | 专注 Godot 4,持续更新 |
| 最适合 | 纯 GDScript 项目 | 混合 GDScript/C# 或仅 C# 项目 |
经验法则: 对于仅使用 GDScript 的项目,选择 GUT。对于 C# 项目,或需要一等 C# 支持和 scene runner 工具的项目,选择 gdUnit4。
TDD Workflow: RED-GREEN-REFACTOR
TDD 工作流:RED-GREEN-REFACTOR
The standard Test-Driven Development cycle: write a failing test (RED), write minimal code to pass (GREEN), then refactor without breaking the test. Each step has its own discipline — don't skip RED (you'll write tests that pass trivially), and don't skip REFACTOR (technical debt compounds).
See references/tdd-workflow.md for a worked GDScript + C# example walking through all three steps on a HealthComponent.
标准的测试驱动开发循环:先编写一个失败的测试(RED),再编写最少量的代码使测试通过(GREEN),最后在不破坏测试的前提下重构。每个步骤都有其对应的实践要求——不要跳过 RED(否则测试会毫无意义地直接通过),也不要跳过 REFACTOR(否则技术债务会不断累积)。
有关 references/tdd-workflow.md 的完整 GDScript + C# 示例,请参阅其中对 HealthComponent 三个步骤的详细演示。
Test Directory Structure
测试目录结构
res://
├── .gutconfig.json # GUT default config file (optional)
├── src/
│ └── components/
│ ├── health_component.gd
│ └── HealthComponent.cs
└── tests/
├── unit/
│ ├── test_health_component.gd # GUT: test_ prefix (default -gprefix)
│ └── HealthComponentTest.cs # gdUnit4 C#: [TestSuite] attribute
└── integration/
├── test_player_scene.gd
└── PlayerSceneTest.csres://
├── .gutconfig.json # GUT default config file (optional)
├── src/
│ └── components/
│ ├── health_component.gd
│ └── HealthComponent.cs
└── tests/
├── unit/
│ ├── test_health_component.gd # GUT: test_ prefix (default -gprefix)
│ └── HealthComponentTest.cs # gdUnit4 C#: [TestSuite] attribute
└── integration/
├── test_player_scene.gd
└── PlayerSceneTest.csNaming conventions
命名约定
| Framework | GDScript file | C# file | Test method prefix/attribute |
|---|---|---|---|
| GUT | | N/A | |
| gdUnit4 | any name ( | | |
| 框架 | GDScript 文件 | C# 文件 | 测试方法前缀或属性 |
|---|---|---|---|
| GUT | | 不适用 | |
| gdUnit4 | 任意名称(生成时为 | | |
Running Tests
运行测试
Both frameworks ship a CLI runner. GUT: invoked via (without the run never quits). gdUnit4: (wraps ; under it also needs ), or the editor's GdUnit dock. CI: tag-triggered or PR-triggered GitHub Action that installs Godot, runs the suite, exits non-zero on failure.
addons/gut/gut_cmdln.gdgodot --headless --path . -s addons/gut/gut_cmdln.gd -gexit-gexitaddons/gdUnit4/runtest.sh -a res://testsbin/GdUnitCmdTool.gd--headless--ignoreHeadlessModeSee references/running-tests.md for full GUT and gdUnit4 CLI invocations + a copy-pasteable GitHub Actions workflow.
两个框架都提供 CLI 运行器。GUT: 通过 调用 (不使用 时,运行永远不会退出)。gdUnit4: 使用 (它封装了 ;在 模式下还需要 ),也可以使用编辑器中的 GdUnit dock。CI:可使用由 tag 或 PR 触发的 GitHub Action,安装 Godot、运行测试套件,并在失败时以非零状态退出。
godot --headless --path . -s addons/gut/gut_cmdln.gd -gexitaddons/gut/gut_cmdln.gd-gexitaddons/gdUnit4/runtest.sh -a res://testsbin/GdUnitCmdTool.gd--headless--ignoreHeadlessMode有关完整的 GUT 和 gdUnit4 CLI 调用方式,以及可直接复制使用的 GitHub Actions 工作流,请参阅 references/running-tests.md。
Testing Patterns
测试模式
Four common patterns: scenes with nodes (instantiate via in , free in ), signal testing (assert that emitting works and connect-then-emit fires), mocking/doubling (gdUnit4 in GDScript; in C#, hand-rolled fakes via injection), async (await yields, signals, frames in tests).
add_childbefore_eachafter_eachmock()@exportSee references/testing-patterns.md for full code on each pattern (GDScript + C# where applicable).
常见的四种模式:包含节点的场景(在 中通过 实例化,在 中释放)、信号测试(断言信号能够发出,并验证连接后触发信号时会执行对应逻辑)、Mock/Double(GDScript 中使用 gdUnit4 的 ;在 C# 中通过 注入自行实现的 fake)、异步测试(在测试中 await yield、信号和帧)。
before_eachadd_childafter_eachmock()@export有关每种模式的完整代码(GDScript + C#,如适用),请参阅 references/testing-patterns.md。
Common Assertions
常用断言
GUT assertions
GUT 断言
| Assertion | Description |
|---|---|
| Equality |
| Not equal |
| Is truthy |
| Is falsy |
| Is null |
| Is not null |
| Greater than |
| Less than |
| Greater than or equal |
| Less than or equal |
| Collection contains item |
| Collection does not contain item |
| String contains substring |
| Float equality within margin |
| Signal was emitted |
| Signal was not emitted |
| 断言 | 说明 |
|---|---|
| 相等 |
| 不相等 |
| 为真值 |
| 为假值 |
| 为 null |
| 不为 null |
| 大于 |
| 小于 |
| 大于或等于 |
| 小于或等于 |
| 集合包含该项 |
| 集合不包含该项 |
| 字符串包含子字符串 |
| 浮点数在误差范围内相等 |
| 信号已发出 |
| 信号未发出 |
gdUnit4 assertions (GDScript + C#)
gdUnit4 断言(GDScript + C#)
| GDScript | C# | Description |
|---|---|---|
| | Equality |
| | Not equal |
| | Is true |
| | Is false |
| | Is null |
| | Is not null |
| | Greater than |
| | Less than |
| | In range (inclusive) |
| | Array contains elements |
| | String contains substring |
| | Float within margin |
| | Signal emitted |
| GDScript | C# | 说明 |
|---|---|---|
| | 相等 |
| | 不相等 |
| | 为 true |
| | 为 false |
| | 为 null |
| | 不为 null |
| | 大于 |
| | 小于 |
| | 在范围内(含边界) |
| | 数组包含元素 |
| | 字符串包含子字符串 |
| | 浮点数在误差范围内 |
| | 信号已发出 |
What NOT to Test
不应测试的内容
Avoid testing things that add noise without catching real bugs:
- Godot engine internals — do not assert that works or that
Node.add_child()variables show up in the editor@export - Private implementation details — test behavior through the public API; if a refactor breaks a test that covers only private state, the test is wrong
- Visual/rendering output — pixel-level rendering results are brittle; test the data driving the visuals instead
- Timing-sensitive floats without margins — use /
assert_almost_eqfor physics valuesIsEqualApprox - One-liners that wrap a built-in — a property getter that just returns a field needs no test
- Every possible invalid input — test the documented contract, not every imaginable misuse
避免测试那些只会增加噪声、却无法发现实际缺陷的内容:
- Godot 引擎内部机制——不要断言 能正常工作,或
Node.add_child()变量会显示在编辑器中@export - 私有实现细节——通过公共 API 测试行为;如果重构破坏了一个仅覆盖私有状态的测试,那么该测试本身就是错误的
- 视觉和渲染输出——像素级渲染结果容易变得不稳定;应改为测试驱动视觉表现的数据
- 没有误差范围的时序敏感浮点数——对于物理数值,使用 /
assert_almost_eqIsEqualApprox - 仅包装内置功能的一行代码——如果属性 getter 只是返回某个字段,则无需测试
- 每一种可能的无效输入——应测试已记录的契约,而不是每一种可以想象的误用方式
Checklist
检查清单
- Each test file matches the naming convention for the chosen framework (GUT ; gdUnit4
test_*.gd/*_test.gd)*Test.cs - Tests extend the correct base class (/
GutTest); C# gdUnit4 suites are plainGdUnitTestSuiteclasses[TestSuite] - C#: pinned to 4.3.x on Godot 4.3; on 5.x (Godot 4.4+), suites that touch nodes or scenes carry
gdUnit4.api[RequireGodotRuntime] - Nodes added to the scene tree use or
add_child_autofree— never manualauto_freequeue_free() - Signals are watched before the action that triggers them
- Mocks/doubles are used for external dependencies, not for the unit under test
- Each test covers exactly one behavior (one logical assertion per test)
- CI workflow runs tests headlessly on every push and PR
- Flaky async tests use explicit timeouts, not arbitrary sleep durations
- Tests pass before merging (RED is only acceptable while actively implementing)
- 每个测试文件都符合所选框架的命名约定(GUT 使用 ;gdUnit4 使用
test_*.gd/*_test.gd)*Test.cs - 测试继承正确的基类(/
GutTest);C# gdUnit4 测试套件是普通的GdUnitTestSuite类[TestSuite] - C#:在 Godot 4.3 上将 固定为 4.3.x;在 5.x(Godot 4.4+)上,涉及节点或场景的测试套件需要添加
gdUnit4.api[RequireGodotRuntime] - 添加到场景树的节点使用 或
add_child_autofree,绝不手动调用auto_freequeue_free() - 在执行触发信号的操作之前监听信号
- Mock/Double 用于外部依赖,而不是被测试单元本身
- 每个测试只覆盖一种行为(每个测试只有一个逻辑断言)
- CI 工作流在每次 push 和 PR 时以 headless 模式运行测试
- 不稳定的异步测试使用明确的超时时间,而不是任意设置 sleep 时长
- 合并前测试必须通过(只有在 actively implementing 期间才允许处于 RED 状态)