This skill helps you run and manage GUT unit tests for Godot projects, speeding validation during development and CI workflows.
npx playbooks add skill chen19007/my_skills --skill godot-test
本 Skill 提供 GUT (Godot Unit Testing) 框架的使用指南,用于运行和编写测试。
安装 GUT 插件:
addons/gut/ 目录或通过 git:
cd your-project/addons
git clone https://github.com/bitwes/Gut.git gut
// 运行所有测试
{ "project": "D:/path/to/godot-project" }
// 运行特定测试文件
{ "project": "D:/path/to/godot-project", "file": "res://test/test_my_feature.gd" }
// 按模式运行测试
{ "project": "D:/path/to/godot-project", "pattern": "test_player" }
godot --path <project> -s addons/gut/gut_cmdln.gd [options]
| 选项 | 描述 |
|-----|------|
| -gdir=<path> | 测试目录(默认:res://test/) |
| -gtest=<path> | 特定测试文件路径 |
| -gselect=<pattern> | 按名称模式匹配测试文件 |
| -gunit_test_name=<name> | 运行特定测试方法 |
| -gexit | 测试完成后退出 |
| -glog=<0-3> | 日志级别(0=最少,3=详细) |
| -gprefix=<prefix> | 测试文件前缀(默认:test_) |
| -gsuffix=<suffix> | 测试文件后缀(默认:.gd) |
# 运行所有测试
godot --path . -s addons/gut/gut_cmdln.gd -gexit
# 运行特定测试文件
godot --path . -s addons/gut/gut_cmdln.gd -gtest=res://test/test_player.gd -gexit
# 按模式运行测试
godot --path . -s addons/gut/gut_cmdln.gd -gselect=player -gexit
# 运行并输出详细日志
godot --path . -s addons/gut/gut_cmdln.gd -glog=3 -gexit
返回码:
0 = 所有测试通过1 = 部分测试失败输出格式:
res://test/test_example.gd
* test_should_pass
* test_another_pass
[1/2 passed]
res://test/test_failing.gd
* test_will_fail
[FAILED]: Expected value did not match
[1/3 passed]
Totals
------
Scripts: 2
Tests: 5
Passing: 4
Failing: 1
extends GutTest
func before_all():
# 所有测试前执行一次
pass
func test_my_functionality():
# 测试代码
assert_eq(result, expected, "描述")
func before_each():
# 每个测试前执行
func after_each():
# 每个测试后执行
| 规则 | 说明 |
|-----|------|
| 脚本继承 | extends GutTest |
| 方法前缀 | test_(不可配置) |
| 文件前缀 | test_(可配置) |
| 内部类分组 | class TestXXX extends GutTest |
assert_eq(actual, expected, message)
assert_ne(a, b, message)
assert_true(condition, message)
assert_false(condition, message)
assert_null(value, message)
assert_not_null(value, message)
assert_between(value, min_val, max_val, message)
# 文件命名
test_player.gd
test_math_utils.gd
# 方法命名
func test_player_take_damage_reduces_health():
...
func test_math_utils_add_two_plus_three_equals_five():
...
# 使用内部类分组
class TestHealth:
extends GutTest
func test_take_damage_reduces_health():
...
class TestMovement:
extends GutTest
func test_move_to_changes_position():
...
func get_damage_test_params():
return [
["damage_10", 10, 90],
["damage_50", 50, 50],
["damage_overkill", 1000, 0],
]
func test_params_damage(data = get_damage_test_params()):
var amount = data[1]
var expected = data[2]
var player = Player.new()
player.take_damage(amount)
assert_eq(player.get_health(), expected)
func test_with_stub():
var player = partial_double(Player.new())
stub(player).to_receive("take_damage").and_return(true)
var result = player.take_damage(10)
assert_true(result)
func test_physics_simulation():
var player = Player.new()
# 模拟 1 秒的 _process
simulate(player._process, 1.0)
# 模拟 10 帧
for i in range(10):
player._process(get_process_delta_time())
test_player_jump_calculation 而不是 test1queue_free()is_equal_approx() 避免精度问题当脚本存在循环依赖时:
# 问题:循环依赖导致 GUT 加载失败
func register_character(character: Character2D5D, height_x: float):
# 解决:使用基类参数
func register_character(character: Node, height_x: float):
# 运行测试,失败时退出
godot --path . -s addons/gut/gut_cmdln.gd -gexit
if [ $? -ne 0 ]; then
echo "Tests failed!"
exit 1
fi
| 错误 | 解决方案 |
|-----|---------|
| File not found: addons/gut/gut_cmdln.gd | 安装 GUT 插件 |
| Nonexistent function 'is_above' | 修复脚本循环依赖 |
| Cannot call non-static function on class | 检查 autoload 配置 |
| GUT class_names not imported | 先运行 godot --import |
| 测试未找到 | 检查 -gdir 路径和 -gprefix 设置 |
| overlaps_body 类型错误 | PhysicsBody 用 overlaps_body,Area2D 用 overlaps_area |
Weekly roundup of top Claude Code skills, MCP servers, and AI coding tips.