找出你要测试多少代码
Find out how much code you're testing
CLI 可以运行单元测试并创建代码覆盖率报告。代码覆盖率报告会向你展示代码库中可能无法通过单元测试进行正确测试的任意部位。
The CLI can run unit tests and create code coverage reports. Code coverage reports show you any parts of your code base that may not be properly tested by your unit tests.
对于本测试指南中描述的范例应用,参阅
For the sample app that the testing guides describe, see the
要了解本测试指南中涉及的测试特性,请参阅
For the tests features in the testing guides, see
要生成覆盖率报告,请在项目的根目录下运行以下命令。
To generate a coverage report run the following command in the root of your project.
ng test --no-watch --code-coverage
测试完成后,该命令会在项目中创建一个 /coverage
目录。打开 index.html
文件,可以查看带有源代码和代码覆盖率值的报表。
When the tests are complete, the command creates a new /coverage
folder in the project. Open the index.html
file to see a report with your source code and code coverage values.
如果要在每次测试时都创建代码覆盖率报告,可以在 CLI 配置文件 angular.json
中设置以下选项:
If you want to create code-coverage reports every time you test, you can set the following option in the CLI configuration file, angular.json
:
"test": {
"options": {
"codeCoverage": true
}
}
代码覆盖的实施
Code coverage enforcement
代码覆盖率可以让你估算出你的代码测试了多少。如果你的团队确定要设置单元测试的最小覆盖率,可以使用 Angular CLI 来强制实施这个最低要求。
The code coverage percentages let you estimate how much of your code is tested. If your team decides on a set minimum amount to be unit tested, you can enforce this minimum with the Angular CLI.
例如,假设你希望代码库的代码覆盖率至少达到 80%。要启用此功能,请打开 Karma 测试平台的配置文件 karma.conf.js
,并在 coverageReporter:
键下添加 check
属性。
For example, suppose you want the code base to have a minimum of 80% code coverage. To enable this, open the Karma test platform configuration file, karma.conf.js
, and add the check
property in the coverageReporter:
key.
coverageReporter: {
dir: require('path').join(__dirname, './coverage/<project-name>'),
subdir: '.',
reporters: [
{ type: 'html' },
{ type: 'text-summary' }
],
check: {
global: {
statements: 80,
branches: 80,
functions: 80,
lines: 80
}
}
}
check
属性会让该工具在项目中运行单元测试时强制要求至少 80%的代码覆盖率。
The check
property causes the tool to enforce a minimum of 80% code coverage when the unit tests are run in the project.
你可以在此处找到关于其它覆盖率配置项的更多信息。
You can find more info about the different coverage configuration options here.