菜单

测试框架

相关源文件

本文档描述了 reveal.js 中使用的测试框架,包括测试的组织、结构和执行方式。有关构建系统的信息,请参阅 构建系统,有关依赖管理的信息,请参阅 依赖管理

测试架构

reveal.js 测试框架基于 QUnit 构建,这是一个 JavaScript 单元测试框架。测试被组织成 HTML 文件,这些文件会实例化实际的 reveal.js 演示文稿,并针对它们运行断言,从而为全面测试提供真实的执行环境。

来源: test/test.html1-897 test/test-auto-animate.html1-152 test/test-pdf.html1-97 test/test-grid-navigation.html1-75 test/test-state.html1-134 test/test-iframes.html1-102

测试文件结构

每个测试文件都遵循一个标准结构,包括 QUnit 组件、reveal.js 演示实例和测试脚本。

来源: test/test.html1-20 test/test.html83-100

HTML 结构

典型的测试文件 HTML 结构包括:

来源: test/test.html1-20 test/test.html83-100

测试组织

测试被组织成逻辑模块,将相关功能分组。

模块目的
DOM测试正确的 DOM 结构和 CSS 类。
API测试公共 API 方法和属性。
URL测试 URL 处理和导航。
键盘测试键盘导航和快捷键。
片段测试片段(fragment)功能。
自动播放测试自动播放幻灯片。
配置测试配置选项。
延迟加载测试资源的延迟加载。
事件测试事件触发和处理。

来源: test/test.html110-781

特定功能测试文件

单独的测试文件专门用于特定功能。

测试文件目的
test.html核心功能的主测试套件。
test-auto-animate.html测试幻灯片之间的自动动画。
test-pdf.html测试 PDF 导出模式。
test-grid-navigation.html测试网格导航模式。
test-iframe-backgrounds.html测试 iframe 背景。
test-state.html测试状态管理。
test-iframes.html测试 iframe 加载。
test-dependencies.html测试依赖加载。
test-dependencies-async.html测试异步依赖加载。

来源: test/test.html test/test-auto-animate.html test/test-pdf.html test/test-grid-navigation.html test/test-state.html test/test-iframes.html test/test-dependencies.html test/test-dependencies-async.html

编写测试

reveal.js 中的测试遵循标准的 QUnit 模式,并带有一些 reveal.js 特有的方法。

测试模块和用例

测试被组织成模块和单独的测试用例。

来源: test/test.html110-129 test/test.html130-197

常见的断言模式

测试使用了几种常见的断言模式。

值测试

来源: test/test.html158-167 test/test.html269-271

对象比较

来源: test/test.html201-217 test/test.html287-296

DOM 元素测试

来源: test/test.html220-227 test/test.html112-123

异步测试

该框架支持使用 QUnit 的 assert.async() 方法进行异步测试。

来源: test/test.html591-618 test/test.html862-894

Promise-based async testing is also supported

来源: test/test.html380-397

运行测试

要运行 reveal.js 测试

  1. 构建项目 (npm run build)
  2. 在浏览器中打开任何测试 HTML 文件
  3. 在 QUnit 界面中查看测试结果

QUnit 在浏览器中报告测试结果,显示

  • 运行的总测试数
  • 通过和失败的断言
  • 测试执行时间
  • 失败测试的详细错误信息

来源: test/test.html9-11

测试辅助函数

测试套件包含辅助函数以简化测试

来源: test/test.html102-105

常见测试场景

API 方法测试

大多数 API 测试遵循调用方法并验证其返回值的一个模式

来源: test/test.html158-167

事件测试

事件测试验证事件是否在适当的时间正确触发

来源: test/test.html813-832

片段测试

片段测试验证幻灯片片段的行为

来源: test/test.html520-546

结论

reveal.js 测试框架提供了一套全面的测试,可在真实环境中验证库的功能。通过使用 QUnit 和基于 HTML 的测试文件模式,该框架可确保演示行为在各种浏览器和环境中按预期工作。

来源: test/test.html test/test-auto-animate.html test/test-pdf.html test/test-grid-navigation.html test/test-state.html test/test-iframes.html test/test-dependencies.html test/test-dependencies-async.html