集成测试

本文档提供有关本项目中使用的集成测试框架的信息。

概述

集成测试旨在验证 Gemini CLI 的端到端功能。它们在受控环境中执行构建的二进制文件,并验证其在与文件系统交互时是否按预期运行。

这些测试位于 integration-tests 目录中,并使用自定义测试运行器运行。

运行测试

集成测试不作为默认 npm run test 命令的一部分运行。它们必须使用 npm run test:integration:all 脚本显式运行。

运行特定测试集

要运行部分测试文件,可以使用 npm run <integration test command> <file_name1> ....

npm run test:e2e list_directory write_file

按名称运行单个测试

使用 --test-name-pattern 标志按名称运行单个测试:

npm run test:e2e -- --test-name-pattern "reads a file"

运行所有测试

要运行整个集成测试套件,请使用以下命令:

npm run test:integration:all

沙盒矩阵

all 命令将针对无沙盒dockerpodman 运行测试。每种类型都可以使用以下命令单独运行:

  • npm run test:integration:sandbox:none
  • npm run test:integration:sandbox:docker
  • npm run test:integration:sandbox:podman

诊断

集成测试运行器提供了几个诊断选项,以帮助追踪测试失败。

保留测试输出

您可以保留在测试运行期间创建的临时文件以供检查。这对于调试文件系统操作的问题很有用。

# 使用标志
npm run test:integration:sandbox:none -- --keep-output

# 使用环境变量
KEEP_OUTPUT=true npm run test:integration:sandbox:none

详细输出

为了进行更详细的调试,--verbose 标志会将 gemini 命令的实时输出流式传输到控制台。

npm run test:integration:sandbox:none -- --verbose

代码检查与格式化

为确保代码质量和一致性,集成测试文件作为主要构建过程的一部分进行代码检查。您也可以手动运行 linter 和自动修复程序。

# 运行 linter
npm run lint

# 自动修复
npm run lint --fix

目录结构

集成测试为每次测试运行在 .integration-tests 目录内创建一个唯一的目录。该结构使查找特定测试运行、文件或案例的工件变得容易。

.integration-tests/
└── <run-id>/
    └── <test-file-name>.test.js/
        └── <test-case-name>/
            ├── output.log
            └── ...其他测试工件...

持续集成

为确保集成测试始终运行,在 .github/workflows/e2e.yml 中定义了一个 GitHub Actions 工作流。此工作流在每次拉取请求和推送到 main 分支时自动运行集成测试。

该工作流在不同的沙盒环境中运行测试,以确保 Gemini CLI 在每种环境中都得到测试:

  • sandbox:none: 无沙盒运行测试。
  • sandbox:docker: 在 Docker 容器中运行测试。
  • sandbox:podman: 在 Podman 容器中运行测试。