使用 Visual Studio 2010 以编程方式生成 .coverage 文件
我需要以编程方式生成 .coverage 文件。 这篇文章解释了执行此操作的 C# 代码如下。
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Microsoft.VisualStudio.Coverage;
using Microsoft.VisualStudio.Coverage.Analysis;
// You must add a reference to Microsoft.VisualStudio.Coverage.Monitor.dll
namespace Microsoft.VisualStudio
{
class DumpProgram
{
static void Main(string[] args)
{
Process p = new Process();
StringBuilder sb = new StringBuilder("/COVERAGE ");
sb.Append("hello.exe");
p.StartInfo.FileName = "vsinstr.exe";
p.StartInfo.Arguments = sb.ToString();
p.Start();
p.WaitForExit();
// TODO: Look at return code – 0 for success
// A guid is used to keep track of the run
Guid myrunguid = Guid.NewGuid();
Monitor m = new Monitor();
m.StartRunCoverage(myrunguid, "hello.coverage");
// Complete the run
m.FinishRunCoverage(myrunguid);
不幸的是,当我编译这段代码时,出现以下错误。
bin2xml.cs(26,22): error CS0246: The type or namespace name 'Monitor' could not be found (are you
missing a using directive or an assembly reference?)
bin2xml.cs(26,38): error CS0246: The type or namespace name 'Monitor' could not be found (are you
missing a using directive or an assembly reference?)
作为 这篇文章说,VS2008和VS2010之间有一些变化,我认为Monitor类位于一些不同的命名空间中。
可能出了什么问题?如何使用 Visual Studio 2010 以编程方式生成 .coverage 文件?
已解决
从 Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\DataCollectors\x86 复制 Microsoft.VisualStudio.Coverage.Monitor.dll
在源代码中添加
using Microsoft.VisualStudio.CodeCoverage;
- 运行
csc bin2xml.cs /r:Microsoft.VisualStudio.Coverage.Analysis.dll /r:Microsoft.VisualStudio.Coverage.Monitor.dll
。
I need to generate .coverage file programmatic way. This post explains a C# code to do it as follows.
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Microsoft.VisualStudio.Coverage;
using Microsoft.VisualStudio.Coverage.Analysis;
// You must add a reference to Microsoft.VisualStudio.Coverage.Monitor.dll
namespace Microsoft.VisualStudio
{
class DumpProgram
{
static void Main(string[] args)
{
Process p = new Process();
StringBuilder sb = new StringBuilder("/COVERAGE ");
sb.Append("hello.exe");
p.StartInfo.FileName = "vsinstr.exe";
p.StartInfo.Arguments = sb.ToString();
p.Start();
p.WaitForExit();
// TODO: Look at return code – 0 for success
// A guid is used to keep track of the run
Guid myrunguid = Guid.NewGuid();
Monitor m = new Monitor();
m.StartRunCoverage(myrunguid, "hello.coverage");
// Complete the run
m.FinishRunCoverage(myrunguid);
Unfortunately, when I compile this code, I get the following error.
bin2xml.cs(26,22): error CS0246: The type or namespace name 'Monitor' could not be found (are you
missing a using directive or an assembly reference?)
bin2xml.cs(26,38): error CS0246: The type or namespace name 'Monitor' could not be found (are you
missing a using directive or an assembly reference?)
As this post says, there are some changes between VS2008 and VS2010, I think the Monitor class is in some different namespace.
What might be wrong? How can I generate the .coverage file programmatically with Visual Studio 2010?
SOLVED
Copy the Microsoft.VisualStudio.Coverage.Monitor.dll from Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\DataCollectors\x86
Add
using Microsoft.VisualStudio.CodeCoverage;
in the source code- Run
csc bin2xml.cs /r:Microsoft.VisualStudio.Coverage.Analysis.dll /r:Microsoft.VisualStudio.Coverage.Monitor.dll
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
覆盖率监视器 DLL (
Microsoft.VisualStudio.Coverage.Monitor.dll
) 实际上只是 vsperfmon.exe 的一个美化的包装器。从字面上看,您传入的参数只是成为进程的命令行参数。最简单的解决方案是仅使用
Process
类来自己运行 vsperfmon.exe(类似于您对 vsinstr.exe 所做的操作)。如果要使用覆盖率监视器DLL,则需要添加对它的引用。有 32 位和 64 位 vsperfmon.exe(分别用于收集 32 位和 64 位进程的代码覆盖率),因此也有 32 位和 64 位版本的覆盖率监视器 DLL。
对于 VS2010,32 位覆盖率监视器 DLL 位于 Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\DataCollectors\x86 中。 64 位覆盖率监视器 DLL 位于 Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\DataCollectors\x64 中。
如果要支持 32 位和 64 位进程上的收集,并且还想使用覆盖率监视器 DLL(因为覆盖率监视器 DLL),则需要有 32 位和 64 位版本的收集程序不是与系统无关的 MSIL)。如果您自己创建 vsperfmon.exe 进程,则只需要一个版本的收集程序即可支持 32 位和 64 位进程。
The coverage monitor DLL (
Microsoft.VisualStudio.Coverage.Monitor.dll
) is really just a glorified wrapper around vsperfmon.exe. Literally, the arguments you pass in just become command-line arguments to the process.The easiest solution is to just use the
Process
class to run vsperfmon.exe yourself (similar to what you're doing for vsinstr.exe).If you want to use the coverage monitor DLL, you need to add a reference to it. There is a 32- and 64-bit vsperfmon.exe (for collecting code coverage against 32- and 64-bit processes, respectively), so there is also a 32- and 64-bit version of the coverage monitor DLL.
For VS2010, the 32-bit coverage monitor DLL lives in
Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\DataCollectors\x86
. The 64-bit coverage monitor DLL lives inMicrosoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\DataCollectors\x64
.If you want to support collection on both 32- and 64-bit processes, you'll need to have a 32- and 64-bit version of your collection program if you also want to use the coverage monitor DLL (since the coverage monitor DLL is not system-agnostic MSIL). If you just create the vsperfmon.exe process yourself, you'd only need to have one version of your collection program to support both 32- and 64-bit processes.
它应该是
System.Threading
命名空间的一部分,但不是你的情况更新:这解释了一切http://blogs.msdn.com/b/phuene/archive/2009/12/01/programmatic -覆盖率分析-in-visual-studio-2010.aspx
it should be part of
System.Threading
namespace, but not in your caseUpdate: this explains it all http://blogs.msdn.com/b/phuene/archive/2009/12/01/programmatic-coverage-analysis-in-visual-studio-2010.aspx