---
title: "统计dll中代码测试覆盖率  （VS+GTEST）"
author: "cutecoot"
date: "2018-05-22T14:55:06Z"
tags: []
categories: ["行业应用"]
slug: "dll-vs-gtest"
draft: false
lastmod: "2018-05-22T14:55:06Z"
id: 2423289
url: "https://cutecoot.iteye.com/blog/2423289"
---

1. 在VS里，根据VS版本安装支持GTest测试的插件，微软官方在VS2017版插件上进行了支持，但VS2015版的插件依然还是个人维护。
   
   1. VS2015: Google Test Adapter [marketplace下载地址](https://marketplace.visualstudio.com/items?itemName=ChristianSoltenborn.GoogleTestAdapter)
   2. VS2017: Test Adapter for Google Test [marketplace下载地址](https://marketplace.visualstudio.com/items?itemName=VisualCPPTeam.TestAdapterforGoogleTest) (**Visual Studio 2017 15.5 now offers Google Test support by default**)
2. 准备好待测试的dll。需要debug版本，包括对应的pdb文件。放在测试架构输出目录中。
3. 在VS工程目录中，创建后缀为“.runsettings”的配置文件，如下样例：（未添加此文件，可能收集不到第三方dll的信息。&lt;ModulePaths&gt;&lt;Include&gt;可特别指明欲收集信息的目标）
   
   ```
   <?xml version="1.0" encoding="utf-8"?>  
   <RunSettings>  
     <!-- Configurations that affect the Test Framework -->  
     <RunConfiguration>  
       <MaxCpuCount>1</MaxCpuCount>  
       <!-- Path relative to solution directory -->  
       <ResultsDirectory>.\TestResults</ResultsDirectory>  
     
       <!-- [x86] | x64    
         - You can also change it from menu Test, Test Settings, Default Processor Architecture -->  
       <TargetPlatform>x64</TargetPlatform>  
     
       <!-- Framework35 | [Framework40] | Framework45 -->  
       <TargetFrameworkVersion>Framework40</TargetFrameworkVersion>  
     
       <!-- Path to Test Adapters -->  
       <TestAdaptersPaths>%SystemDrive%\Temp\foo;%SystemDrive%\Temp\bar</TestAdaptersPaths>  
     </RunConfiguration>  
     
     <!-- Configurations for data collectors -->  
     <DataCollectionRunSettings>  
       <DataCollectors>  
         <DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">  
           <Configuration>  
             <CodeCoverage>  
               <ModulePaths>
   
                 <Exclude>  
                   <ModulePath>.*CPPUnitTestFramework.*</ModulePath>  
                 </Exclude>  
               </ModulePaths>  
     
               <!-- We recommend you do not change the following values: -->  
               <UseVerifiableInstrumentation>True</UseVerifiableInstrumentation>  
               <AllowLowIntegrityProcesses>True</AllowLowIntegrityProcesses>  
               <CollectFromChildProcesses>True</CollectFromChildProcesses>  
               <CollectAspDotNet>False</CollectAspDotNet>  
     
             </CodeCoverage>  
           </Configuration>  
         </DataCollector>  
       </DataCollectors>  
     </DataCollectionRunSettings>  
     
   </RunSettings>  
   ```
4. 打开VS，调出测试资源窗口。测试→ 窗口→测试资源管理器
5. 编译GTest架构的测试框架，所有的测试会被自动识别并显示在测试资源管理器中（被识别的是编译出的可执行的测试例，比如使用了GTEST\_FLAG(filter)，筛选出的测试例才会被识别。）
6. 在测试资源管理器中选择测试例进行覆盖率测试
7. 统计结果会自动显示在下方代码覆盖率结果窗口，同时结果文件会生成在&lt;ResultsDirectory&gt;

PS: 需要Visual Studio Enterprise版本， Community和Professional版本不支持