Visual Studio 如何测量测试单元中覆盖的块百分比?

How does Visual Studio measure the % Blocks coveraged in test unit?

当我单击 运行 具有代码覆盖率的单元测试时,它有一列显示覆盖块的百分比。

我也用TeamCity来衡量,我注意到他们有不同的指标。我想知道 Visual Studio 是如何测量的。我做了一些计算,但我没有想出来。

根据Using Code Coverage to Determine How Much Code is being Tested

Code coverage is counted in blocks. A block is a piece of code with exactly one entry and exit point. If the program’s control flow passes through a block during a test run, that block is counted as covered. The number of times the block is used has no effect on the result.

注意: 代码块在到达决策点时结束,例如新的条件语句块、函数调用、异常抛出、enter、leave、try、catch 或 finally 构造。

所以基本上 % 是使用的代码块数除以不包括代码块的总块数 returns。

您可以在此处阅读有关块定义的信息: https://en.wikipedia.org/wiki/Basic_block

希望能给你想要的信息。