简体   繁体   English

如何使用Visual Studio检测并获得Windows服务的代码覆盖率?

[英]How to instrument and get code coverage of a Windows service with Visual Studio?

I am trying to get the code coverage for a Windows service written in C++ (let's call it hello.exe) with Visual Studio 2012 Premium tools. 我正在尝试使用Visual Studio 2012 Premium工具以C ++(让我们称之为hello.exe)编写Windows服务的代码。 So far I have attempted to use the method outlined in the answer to this post: How to use MS code coverage tool in command line? 到目前为止,我已经尝试使用本文答案中概述的方法: 如何在命令行中使用MS代码覆盖率工具?

For your convenience, the steps given were to run: 为了您的方便,给出的步骤是运行:

  1. vsinstr /coverage hello.exe vsinstr / coverage hello.exe
  2. start vsperfmon /coverage /output:mytestrun.coverage 启动vsperfmon / coverage /output:mytestrun.coverage
  3. hello.exe 用hello.exe
  4. vsperfcmd /shutdown vsperfcmd /关闭

I am able to instrument, test and get the .coverage file for hello.exe when I start it as an executable in the command prompt no problem. 当我在命令提示符下将其作为可执行文件启动时,我能够检测,测试并获取hello.exe的.coverage文件。 But if I instead try to start it as a service in step 3 (either through services.msc manually or win32serviceutil.StartService in Python), nothing registers and the coverage file is empty. 但是,如果我改为尝试在步骤3中将其作为服务启动(通过手动的services.msc或Python中的win32serviceutil.StartService),则没有任何注册,并且coverage文件为空。 Is there something i'm missing here? 我在这里想念什么吗?

Extra info: In an effort to solve this I came across what seems to be another code coverage utility called CodeCoverage.exe (Microsoft Visual Studio 11.0\\Team Tools\\Dynamic Code Coverage Tools). 额外的信息:为了解决这个问题,我遇到了另一个似乎叫做CodeCoverage.exe的代码覆盖实用程序(Microsoft Visual Studio 11.0 \\ Team Tools \\ Dynamic Code Coverage Tools)。 Is that what I'm supposed to be using for this task? 那是我应该用于此任务的东西吗? Also, I found something seemingly related here https://msdn.microsoft.com/en-us/library/dd255408.aspx , but (forgive me for my ignorance) am not sure whether a .NET service is the same thing as a Windows service. 此外,我在这里发现了一些看似相关的东西https://msdn.microsoft.com/en-us/library/dd255408.aspx ,但是(请原谅我的无知)不确定.NET服务是否与Windows服务。

Answering my own question here, after a lot of trying and pouring through documents I found this: 经过大量尝试并仔细阅读文档后,我在这里回答了我自己的问题:

https://blogs.msdn.microsoft.com/hilliao/2008/08/19/code-coverage-using-visual-studio-2008s-code-coverage-tools/ https://blogs.msdn.microsoft.com/hilliao/2008/08/19/code-coverage-using-visual-studio-2008s-code-coverage-tools/

Basically, a windows Service is different from a process so we have to enable the cross session flag and specify the user when monitoring. 基本上,Windows Service与进程不同,因此我们必须启用跨会话标志并在监视时指定用户。

start vsperfmon /coverage /output:mytestrun.coverage /cs /user:”Everyone”

So just instrument the exe as usual, start monitoring with above command, start the service, do testing, stop the service, and shutdown the monitoring. 因此,只需像往常一样安装exe,使用上述命令开始监视,启动服务,进行测试,停止服务并关闭监视即可。

Hope it helps someone in the future. 希望它对将来的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM