适用场景
当 Godot 项目未安装任何测试插件,需要一种从终端或 CI 流水线快速验证 GDScript 逻辑(纯函数、资源加载、autoload 状态)的方式时使用。
当需要接线一个 CI 任务,要求某个 .gd 测试失败时让构建失败时使用。
当需要排查 godot --headless 调用为何挂起、打开窗口,或在断言失败的情况下仍以 0 退出时使用。
不适用场景:GDScript 语法或语言特性本身的问题应参考 godot-gdscript;导出/构建流水线与平台模板应参考 godot-export(那是另一种 --headless 用途,产出二进制而非运行测试)。
工作流程
先确认二进制能以无头方式解析。Godot 4.x 内置 --headless(无需导出模板);运行 godot --headless --version,确认它打印版本字符串而不是打开 GUI 窗口。
在全新检出时,先导入再运行测试。.godot/ 通常不会被提交,因此干净检出没有导入缓存:class_name 类型无法解析(报 Identifier "X" not declared in the current scope),导入的资源也无法加载(报 No loader found for resource: res://...)。在 CI 与本地都需先运行一次 godot --headless --path <project_dir> --import。
把 runner 写成 SceneTree 脚本,而不是 Node 场景。SceneTree 脚本的 _initialize() 在任何帧之前运行一次,足以完成纯逻辑测试,且无需 .tscn 即可启动。
自行统计通过/失败数,并显式调用 quit(<code>)。不要用裸 assert() 来使测试失败。Godot 不会因为 push_error() 就自动让进程退出码变为非零——runner 必须统计失败数并调用 quit(1)。更糟的是,在 _initialize() 中失败的 assert()(官方/调试构建)会打印 SCRIPT ERROR: Assertion failed 并在 quit() 执行前停止执行,导致进程永不退出、CI 一直挂起到自身超时。应改用 assert_eq() 风格的辅助函数记录失败并继续执行。
调用方式为 godot --headless --path <project_dir> --script res://<runner>.gd,并在 shell 或 CI 步骤中读取进程退出码,而不只是读取 stdout。--script 同时接受 res:// 相对路径和绝对文件系统路径(例如位于项目文件夹之外的 runner),两者都可用。
当从包装 shell(PowerShell、部分 CI runner)脚本化调用时,把 stdout 与 stderr 重定向到文件。push_error() 输出进入 stderr,若只实时捕获 stdout,可能被丢弃或乱序。
在 CI 中添加步骤超时。即使避开了 assert() 陷阱,一个永不完成的 await(见模式 2)也会让 runner 永久挂起;CI 步骤上的 timeout-minutes 是 CI 侧的兜底,不能替代为每个 await 配备超时节点。
模式
- 带真实退出码的最小 SceneTree 测试 runner
res://test_runner.gd — 运行方式:
godot --headless --path . --script res://test_runner.gd
extends SceneTree
var passed := 0 var failed := 0
func _initialize() -> void: test_add() print("Results: %d passed, %d failed" % [passed, failed]) quit(1 if failed > 0 else 0) # 非零退出会让 CI 步骤失败
func assert_eq(actual, expected, label: String) -> void: if actual == expected: passed += 1 else: failed += 1 push_error("FAIL %s: expected %s, got %s" % [label, expected, actual])
func test_add() -> void: assert_eq(2 + 2, 4, "test_add")
已在 Godot 4.7.2 验证:godot --headless --path . --script res://test_runner.gd 会把 Results: N passed, M failed 打印到 stdout,把 push_error 行路由到 stderr,并在 failed == 0 时返回进程退出码 0,否则返回 1。
- 测试需要一帧、定时器或信号的对象
extends SceneTree
var passed := 0 var failed := 0
func _initialize() -> void: await run_tests() print("Results: %d passed, %d failed" % [passed, failed]) quit(1 if failed > 0 else 0) # 这里同样要统计并上报失败
func assert_eq(actual, expected, label: String) -> void: if actual == expected: passed += 1 else: failed += 1 push_error("FAIL %s: expected %s, got %s" % [label, expected, actual])
func run_tests() -> void:
# 在 _initialize() 期间 root 尚未进入树:此时启动的 Timer 会报错
# ("not inside the tree"),其 timeout 永不触发。需等待一帧。
await process_frame
var timer_node := Timer.new()
timer_node.one_shot = true # 默认 Timer 在超时后会重新启动
root.add_child(timer_node)
timer_node.start(0.1)
await timer_node.timeout
# 此处断言可以依赖该节点已在树中存在了一帧
assert_eq(timer_node.is_stopped(), true, "timer_fires_once")
timer_node.queue_free()
_initialize() 可以 await,这正是该模式能应对任何需要节点真正进入树、定时器触发或信号发射的场景的原因——在引擎处理至少一帧之前,这些都不会发生。使用与模式 1 相同的 passed/failed 计数器和 assert_eq() 辅助函数;某个版本若始终调用 quit(0),则永远无法让构建失败。
- 依据退出码把关的 CI 步骤(GitHub Actions)
- name: Import project (populates .godot/ on a fresh checkout) run: godot --headless --path . --import
- name: Run GDScript tests timeout-minutes: 5 run: godot --headless --path . --script res://test_runner.gd
在干净检出上导入步骤是必需的——没有它,class_name 类型和导入资源将无法解析。测试步骤本身无需额外参数:runner 已经会让 run: 的非零退出码导致任务失败;这一约束来自 runner 脚本的 quit() 调用,而不是 CI 配置。timeout-minutes 是针对挂起 await 的兜底(见陷阱),不能替代为每个 await 配备超时节点。
陷阱
脚本“什么都不做”或打开了编辑器窗口 → 缺少 --headless,或脚本路径错误。--script 接受相对于 --path <project_dir> 解析的 res:// 相对路径,也接受绝对文件系统路径——两者都可用。
仅在全新检出时出现 Identifier "X" not declared in the current scope,或资源加载失败 → .godot/(导入缓存)通常不被提交,因此 class_name 类型与导入资源尚未解析。请在测试步骤前先运行一次 godot --headless --path <project_dir> --import。
失败的 assert() 导致挂起而非测试失败 → 在官方/调试构建中,_initialize() 内失败的 assert() 会打印 SCRIPT ERROR: Assertion failed 并在该函数到达 quit() 之前停止执行——进程永不退出,CI 会一直等到自身超时。在测试 runner 中应使用 assert_eq() 计数器(模式 1)而不是裸 assert()。
尽管断言失败,退出码仍为 0 → runner 从未调用 quit(1),或(模式 2 的情况)无论是否失败都调用 quit(0)。自行统计失败并显式调用能反映失败的 quit() 代码;不要仅依赖 assert() 或 push_error() 来改变退出码。
_initialize() 在节点、定时器或信号存在之前运行 → 需要先处理一帧的逻辑,必须在断言前 await 一个信号或定时器;见模式 2。root 自身尚未进入树,因此在那里添加并启动的 Timer 会报错且其 timeout 永不触发(runner 挂起)——应先 await process_frame。
从包装 shell 看到的输出为空或乱序 → 某些 shell(尤其是 PowerShell)会重排或丢弃原生进程的实时 stdout/stderr。应把两个流都重定向到文件,等进程退出后再读取文件,而不要信任实时控制台。
Runner 永不终止 → SceneTree 脚本会一直运行,直到有东西调用 quit()。某个测试 await 一个永不触发的信号会让任务永久挂起——务必为 await 配备超时节点作为兜底,并在 CI 步骤上设置 timeout-minutes 作为后备。
相关技能
godot-gdscript —— 该模式 runner 脚本自身使用的语言语法与节点生命周期。
godot-export —— 无头 CLI 导出/构建,另一种 --headless 用途。