代码覆盖率

Code Coverage

代码覆盖率

With the Angular CLI we can run unit tests as well as create code coverage reports. Code coverage reports allow us to see any parts of our code base that may not be properly tested by our unit tests.
用Angular CLI我们能运行单元测试并创建代码覆盖率报告。代码覆盖率报告允许我们看到我们的代码库中的任何部分可能无法通过我们的单元测试。

To generate a coverage report run the following command in the root of your project
生成一个覆盖率报告运行下面的命令在项目的根

ng test --watch=false --code-coverage

Once the tests complete a new /coverage folder will appear in the project. In your Finder or Windows Explorer open the index.html file. You should see a report with your source code and code coverage values.
一次测试完成一个新的 /coverage 文件夹将出现在项目。 在你的检测器或资源管理器中打开 index.html 文件,你应该看到一个报告里面是你的源代码和代码覆盖值。

Using the code coverage percentages we can estimate how much of our code is tested. It is up to your team to determine how much code should be unit tested.
使用代码覆盖检测器我们能估算多少代是被测试过的。

Code Coverage Enforcement

代码覆盖率实施

If your team decides on a set minimum amount to be unit tested you can enforce this minimum with the Angular CLI. For example our team would like the code base to have a minimum of 80% code coverage. To enable this open the karma.conf.js and add the following in the coverageIstanbulReporter: key
如果你的团队决定在设置一个最小限度账户为单元测试,你能强制这个最小限度。 列如我们的团队将基于代码有一个百分之八十的修小代码覆盖率。要启用此操作,打开 karma.conf.js 添加如下在coverageIstanbulReporter:

coverageIstanbulReporter: {
  reports: [ 'html', 'lcovonly' ],
  fixWebpackSourcePaths: true,
  thresholds: {
    statements: 80,
    lines: 80,
    branches: 80,
    functions: 80
  }
}

The thresholds property will enforce a minimum of 80% code coverage when the unit tests are run in the project.
thresholds 属性将强制百分之八十代码覆盖率的一个小值当单元测试运行在这个项目

你可能感兴趣的:(代码覆盖率)