如何在命令行中使用MS代码覆盖工具?

pro*_*eek 6 c++ profiling code-coverage visual-studio-2010

我有以下C++代码.

#include <iostream>
using namespace std;

int testfunction(int input)
{
    if (input > 0) {
        return 1;
    }
    else {
        return 0;
    }
}

int main()
{
    testfunction(-1);
    testfunction(1);
}
Run Code Online (Sandbox Code Playgroud)

我编译它来获得执行

cl /Zi hello.cpp -link /Profile
Run Code Online (Sandbox Code Playgroud)

然后,我检测执行并生成.coverage二进制文件.

vsinstr -coverage hello.exe
start vsperfmon -coverage -output:mytestrun.coverage
vsperfcmd -shutdown
Run Code Online (Sandbox Code Playgroud)

当我在VS2010中打开覆盖文件时,我的结果没有任何结果.

在此输入图像描述

可能有什么问题?我按照这篇文章中的说明进行操作.

Chr*_*ich 12

您需要在监视器启动后运行程序:

  1. > vsinstr /coverage hello.exe
  2. > start vsperfmon /coverage /output:mytestrun.coverage
  3. > hello.exe
  4. > vsperfcmd /shutdown

当您运行步骤3时,您应该在vsperfmon.exe中看到hello.exe已启动的一些通知.

如果您计划进行多次测试运行,则只需执行步骤2-4.换句话说,您只需要在构建二进制文件后对其进行一次检测(步骤1).

  • 对于未来的读者........这里是创建多个.coverage文件的替代解决方案:http://stackoverflow.com/questions/415562/mstest-code-coverage/37005493#37005493 (2认同)