跳转到主内容

测试

我们的目的是保持 Electron 代码的高覆盖率。 我们要求所有的pull request不仅要通过所有现有的测试,还要增加新的测试来覆盖变更的代码和新的场景。 我们要尽可能多地覆盖Electron应用中的代码路径和用例,以确保我们发布的应用程序都能拥有更少的bug。

这个仓库为 JavaScript 和 C ++ 提供了linting 规则 - 以及单元和集成测试。 通过 coding-style文档来学习更多的Electron's编码风格

提示

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.

单元测试

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中定义。

若要通过匹配模式运行单个测试,可以执行 npm run test -- -g=PATTERN,把PATTERN 替换为匹配某个测试的正则表达式。 例:如果想运行IPC测试,你可以执行npm run test -- -g ipc

Node.js Smoke Tests

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.

To run all of the Node.js tests:

$ node script/node-spec-runner.js

To run a single Node.js test:

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

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

在Windows10上测试

Extra steps to run the unit test:

  1. Visual Studio 2019 must be installed.

  2. Node headers have to be compiled for your configuration.

    ninja -C out\Testing electron:node_headers
  3. 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 设备上没有默认安装Meriryo字体,这会导致字体回退测试的失败。 要安装Meiryo字体:

  1. 按下Windows key并搜索 管理可选功能
  2. 单击添加功能
  3. 选择Japanese Supplemental Fonts并单击 安装

Pixel measurements

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

要配置缩放比:

  1. 按下Windows key并搜索显示设置.
  2. Scale and layout下,确保缩放比为100%。