Skip to main content

测试

¥Testing

我们的目标是保持 Electron 的高代码覆盖率。我们要求所有拉取请求不仅通过所有现有测试,而且理想情况下还添加新测试以覆盖更改的代码和新场景。确保我们捕获尽可能多的 Electron 代码路径和用例,确保我们交付的应用具有更少的错误。

¥We aim to keep the code coverage of Electron high. We ask that all pull request not only pass all existing tests, but ideally also add new tests to cover changed code and new scenarios. Ensuring that we capture as many code paths and use cases of Electron as possible ensures that we all ship apps with fewer bugs.

该存储库附带 JavaScript 和 C++ 的 linting 规则以及单元和集成测试。要了解更多有关 Electron 的编码风格,请参阅 coding-style 文档。

¥This repository comes with linting rules for both JavaScript and C++ – as well as unit and integration tests. To learn more about Electron's coding style, please see the coding-style document.

代码检查

¥Linting

为了确保你的更改符合 Electron 编码风格,请运行 npm run lint,它将根据你的更改所涉及的代码区域运行各种 linting 检查。

¥To ensure that your changes are in compliance with the Electron coding style, run npm run lint, which will run a variety of linting checks against your changes depending on which areas of the code they touch.

其中许多检查都作为预提交钩子包含在内,因此你的错误很可能会在提交时被捕获。

¥Many of these checks are included as precommit hooks, so it's likely you error would be caught at commit time.

单元测试

¥Unit Tests

如果你不使用 build-tools,请确保你为本地 Electron 版本配置的名称是 TestingReleaseDefault 之一,或者你已设置 process.env.ELECTRON_OUT_DIR。如果没有这些设置,Electron 将无法执行一些预测试步骤。

¥If you are not using build-tools, ensure that the name you have configured for your local build of Electron is one of Testing, Release, Default, or you have set process.env.ELECTRON_OUT_DIR. Without these set, Electron will fail to perform some pre-testing steps.

要运行所有单元测试,请运行 npm run test。单元测试是一个 Electron 应用(惊喜!),可以在 spec 文件夹中找到。请注意,它有自己的 package.json,因此其依赖未在顶层 package.json 中定义。

¥To run all unit tests, run npm run test. The unit tests are an Electron app (surprise!) that can be found in the spec folder. Note that it has its own package.json and that its dependencies are therefore not defined in the top-level package.json.

要仅运行与模式匹配的特定测试,请运行 npm run test -- -g=PATTERN,将 PATTERN 替换为与你要运行的测试匹配的正则表达式。举个例子:如果你只想运行 IPC 测试,则可以运行 npm run test -- -g ipc

¥To run only specific tests matching a pattern, run npm run test -- -g=PATTERN, replacing the PATTERN with a regex that matches the tests you would like to run. As an example: If you want to run only IPC tests, you would run npm run test -- -g ipc.

Node.js 冒烟测试

¥Node.js Smoke Tests

如果你所做的更改可能会影响 Node.js 嵌入 Electron 的方式,我们有一个测试运行程序,可以使用 Electron 的 Node.js 自定义分支运行 Node.js 中的所有测试。

¥If you've made changes that might affect the way Node.js is embedded into Electron, we have a test runner that runs all of the tests from Node.js, using Electron's custom fork of Node.js.

要运行所有 Node.js 测试:

¥To run all of the Node.js tests:

$ node script/node-spec-runner.js

要运行单个 Node.js 测试:

¥To run a single Node.js test:

$ node script/node-spec-runner.js parallel/test-crypto-keygen

其中传递给运行器的参数是 Node.js 源树中测试的路径。

¥where the argument passed to the runner is the path to the test in the Node.js source tree.

在 Windows 10 设备上测试

¥Testing on Windows 10 devices

运行单元测试的额外步骤:

¥Extra steps to run the unit test:

  1. 必须安装 Visual Studio 2019。

    ¥Visual Studio 2019 must be installed.

  2. 必须根据你的配置编译节点标头。

    ¥Node headers have to be compiled for your configuration.

    ninja -C out\Testing electron:node_headers
  3. Electron.lib 必须复制为 node.lib。

    ¥The electron.lib has to be copied as node.lib.

    cd out\Testing
    mkdir gen\node_headers\Release
    copy electron.lib gen\node_headers\Release\node.lib

缺少字体

¥Missing fonts

某些 Windows 10 设备 未附带安装 Meiryo 字体,这可能会导致字体回退测试失败。安装 Meiryo:

¥Some Windows 10 devices do not ship with the Meiryo font installed, which may cause a font fallback test to fail. To install Meiryo:

  1. 按 Windows 键并搜索管理可选功能。

    ¥Push the Windows key and search for Manage optional features.

  2. 单击添加功能。

    ¥Click Add a feature.

  3. 选择日语补充字体并单击安装。

    ¥Select Japanese Supplemental Fonts and click Install.

像素测量

¥Pixel measurements

由于浮点精度误差,一些依赖于精确像素测量的测试可能无法在具有 Hi-DPI 屏幕设置的设备上正常工作。要正确运行这些测试,请确保设备设置为 100% 缩放。

¥Some tests which rely on precise pixel measurements may not work correctly on devices with Hi-DPI screen settings due to floating point precision errors. To run these tests correctly, make sure the device is set to 100% scaling.

要配置显示缩放:

¥To configure display scaling:

  1. 按 Windows 键并搜索显示设置。

    ¥Push the Windows key and search for Display settings.

  2. 在“比例和布局”下,确保设备设置为 100%。

    ¥Under Scale and layout, make sure that the device is set to 100%.