14.3 扩展的自动化测试
测试扩展需要一个最小宿主:安装、激活、调用 API、拆除。安装只管登记,激活才返回它注册的能力;调用要求已激活;teardown 清空所有状态,保证用例之间不串味——扩展测试失败最常见的原因就是上一条用例的残留。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 扩展框架 | 本卷 1.1 | 1.1 管扩展注册;本节把注册包装成可测试宿主 |
| 脚本沙箱 | 本卷 5.3 | 5.3 管运行时权限;本节管测试期隔离 |
| 全卷脉络 | 本卷目录 | 本节收口自动化测试章 |
从零实现:扩展的自动化测试
宿主维护两个集合:已安装与已激活。安装去重;激活要求已安装,重复激活返回标记而不是报错;调用要求已激活。teardown 把两个集合清空并返回清理条目数,调用方可以据此确认登记记录已清除。
两个集合仅验证生命周期状态转换。调用返回占位结果。接入真实宿主后,应重复激活与卸载,并断言菜单、事件订阅和后台任务均已释放。集合计数只能证明本例中的登记状态已清除。
flowchart TD
A["安装扩展 ID"] --> B["激活已安装 ID"]
B --> C["调用已激活 ID"]
C --> D["清空两个集合"]
D --> E["验证登记状态已清除"]
对标 Unity / Unreal / Godot / Cocos:扩展自动化测试
| 引擎 | 机制 | 取舍 |
|---|---|---|
| Unity | Test Framework 的 PlayMode/EditMode | 扩展可用编辑器测试框架验证 |
| Unreal | Automation Test | 插件测试接入引擎自动化 |
| Godot | GUT 插件测试 | 社区框架模拟编辑器环境 |
| Cocos Creator | 扩展测试工具 | 在扩展宿主中运行用例 |
| helio(本节) | ExtensionTestHarness | 安装/激活/调用/拆除四步 |
可运行产物:6 条用例
下面是 src/extension-test-harness.ts,层级 L1,完整可编译。运行命令 node --test test/14-3-extension-automation-test.test.ts:
// file: src/extension-test-harness.ts
// tier: L1
// verify: node --test test/14-3-extension-automation-test.test.ts
//
// 卷四(第十四章):扩展的自动化测试。
// 测试扩展需要一个最小宿主:安装、激活、调用 API、拆除。安装只管登记,
// 激活才返回它注册的能力;调用要求已激活;teardown 清空所有状态,保证
// 用例之间不串味——扩展测试失败最常见的原因就是上一条用例的残留。
export class ExtensionTestHarness {
#installed = new Set<string>();
#active = new Set<string>();
install(id: string): boolean {
if (this.#installed.has(id)) return false;
this.#installed.add(id);
return true;
}
activate(id: string): string[] {
if (!this.#installed.has(id)) throw new Error(`扩展 ${id} 未安装`);
if (this.#active.has(id)) return [`${id}:already-active`];
this.#active.add(id);
return [`${id}:menu`, `${id}:command`];
}
call(id: string, api: string, arg = ''): string {
if (!this.#active.has(id)) throw new Error(`扩展 ${id} 未激活`);
return `${id}.${api}(${arg})`;
}
isActive(id: string): boolean {
return this.#active.has(id);
}
/** 拆除全部安装与激活状态,返回清理的条目数。 */
teardown(): number {
const count = this.#installed.size + this.#active.size;
this.#installed.clear();
this.#active.clear();
return count;
}
}
「安装」与「激活」拆开,是为了测试安装失败、激活失败两类问题;如果合成一步,用例只能看到最终结果。teardown 返回清理数量而不是静默清空,让用例能断言隔离确实发生了。
下面是 test/14-3-extension-automation-test.test.ts,层级 L1:
// file: test/14-3-extension-automation-test.test.ts
// tier: L1
// verify: node --test test/14-3-extension-automation-test.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { ExtensionTestHarness } from '../src/extension-test-harness.ts';
test('安装后激活返回注册能力', () => {
const h = new ExtensionTestHarness();
assert.equal(h.install('git'), true);
assert.deepEqual(h.activate('git'), ['git:menu', 'git:command']);
assert.equal(h.isActive('git'), true);
});
test('未安装的扩展无法激活', () => {
const h = new ExtensionTestHarness();
assert.throws(() => h.activate('ghost'), /未安装/);
});
test('调用已激活扩展的 API', () => {
const h = new ExtensionTestHarness();
h.install('git');
h.activate('git');
assert.equal(h.call('git', 'commit', 'init'), 'git.commit(init)');
});
test('未激活时调用报错', () => {
const h = new ExtensionTestHarness();
h.install('git');
assert.throws(() => h.call('git', 'commit'), /未激活/);
});
test('重复安装与重复激活的处理', () => {
const h = new ExtensionTestHarness();
h.install('git');
assert.equal(h.install('git'), false);
h.activate('git');
assert.deepEqual(h.activate('git'), ['git:already-active']);
});
test('teardown 清理全部状态', () => {
const h = new ExtensionTestHarness();
h.install('git');
h.install('lsp');
h.activate('git');
assert.equal(h.teardown(), 3);
assert.equal(h.isActive('git'), false);
assert.throws(() => h.call('git', 'commit'), /未激活/);
assert.equal(h.install('git'), true);
});
本机实测(Node v26.5.0,node --test test/14-3-extension-automation-test.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉激活与未安装;第三、四条钉调用与未激活;第五条钉重复操作;第六条钉 teardown 清理。
小结
- 扩展测试宿主提供四步:安装、激活、调用、拆除。
- 安装与激活分离,便于分别测试失败路径。
- 调用要求已激活,未激活直接报错。
- teardown 清空全部状态并返回清理数量。
阅读导航:上一节:14.2 命令回放与截图回归 · 下一节:15.1 工业级编辑器全景回顾 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。