首页 > 解决方案 > 不存在的行包含在 Visual Studio 2017 中的动态代码覆盖率分析中

问题描述

在这篇文章之后,我正在尝试在 Visual Studio 2017 for C++ 中测试动态代码覆盖率。但是,覆盖范围似乎正在执行根本不在程序中的行。这是我用来测试覆盖率的简单程序:

ConsoleApplication3.cpp

#include <iostream>

using namespace std;

int main()
{
    cout << "Running..." << endl;

    if (false) {
        cout << "shouldn't print" << endl;
    }

#ifdef _WIN32
    cout << "Running in WIN" << endl;
#else
    cout << "Running in LIN" << endl;
#endif 

}

首先我使用/PROFILE标志(在解决方案资源管理器中,然后右键单击项目Properties > Linker > Advanced > Profile > set to "Yes (/PROFILE)"。要运行我的测试,我只需要执行ConsoleApplication3.exe,所以按照说明进行操作:

  1. codecoverage collect /session:qwerty /output:mytest.coverage .\Debug\ConsoleApplication3.exe
  2. codecoverage shutdown /session:qwerty
  3. codecoverage analyze /output:mytestCoverage.xml .\mytest.coverage

注意:根据我上面分享的帖子,没有提到您需要包含.exe作为参数。这意味着您可以开始收集,然后在收集开始后运行您的测试。然而,这只是给我留下了一个空的报告,我发现这实际上是生成一个真实的报告。接下来,我将生成的mytest.coverage文件转换为.xml格式。打开mytestCoverage.xml我看到以下内容:

mytestCoverage.xml

<?xml version="1.0" encoding="UTF-8"?>
<results>
   <modules>
      <module lines_not_covered="1" lines_partially_covered="0" lines_covered="6" blocks_not_covered="3" blocks_covered="10" line_coverage="85.71" block_coverage="76.92" id="A62F1CFD667E27408B95EED542B0A03801000000" path="consoleapplication3.exe" name="consoleapplication3.exe">
         <functions>
            <function lines_not_covered="1" lines_partially_covered="0" lines_covered="6" blocks_not_covered="3" blocks_covered="10" line_coverage="85.71" block_coverage="76.92" id="6832" name="_main" type_name="">
               <ranges>
                  <range end_column="0" end_line="6" start_column="0" start_line="6" covered="yes" source_id="0" />
                  <range end_column="0" end_line="7" start_column="0" start_line="7" covered="yes" source_id="0" />
                  <range end_column="0" end_line="9" start_column="0" start_line="9" covered="yes" source_id="0" />
                  <range end_column="0" end_line="10" start_column="0" start_line="10" covered="no" source_id="0" />
                  <range end_column="0" end_line="14" start_column="0" start_line="14" covered="yes" source_id="0" />
                  <range end_column="0" end_line="19" start_column="0" start_line="19" covered="yes" source_id="0" />
                  <range end_column="0" end_line="15732480" start_column="0" start_line="15732480" covered="yes" source_id="0"/>
               </ranges>
            </function>
         </functions>
         -
         <source_files>
            <source_file id="0" path="c:\users\sakeeb_hossain\source\repos\consoleapplication3\consoleapplication3.cpp" checksum="87CCE2DD760287BB1A6A3CCF2BF8B930" checksum_type="MD5" />
         </source_files>
      </module>
   </modules>
</results>

看看最后一个<range>标签。我的问题是它来自哪里以及如何摆脱它(当然除了手动编辑)?

标签: c++visual-studiovisual-studio-2017code-coverage

解决方案


推荐阅读