18

我有一個演示項目,它創建一個程序集並使用它。我也可以調試注入的代碼。但是,如果我運行覆蓋,分析或分析,它會被計算,但我想測量它。動態生成代碼的代碼覆蓋率,分析和性能分析

代碼:

CSharpCodeProvider codeProvider = new CSharpCodeProvider(); 
ICodeCompiler icc = codeProvider.CreateCompiler(); 
CompilerParameters parameters = new CompilerParameters(); 
parameters.GenerateExecutable = false; 
parameters.GenerateInMemory = false; // debug enabled     
parameters.OutputAssembly = "DynamicCode.dll"; // if specified creates the DLL 
parameters.IncludeDebugInformation = true; 
CompilerResults results = icc.CompileAssemblyFromFile(parameters, "InjectedCode.cs.txt"); 

我創建DLL來檢查所生成的IL代碼。我可以在VS中調試代碼。但是當我運行覆蓋時,如果我使用TEMP目錄,或者如果我輸出DLL(如上所述),那麼生成的程序集就會錯過,而且覆蓋範圍內沒有FILE(甚至不包括主程序集)。

當我運行分析時,我只能看到調用(反射),但沒有關於生成的代碼。當我進行分析時(我在注入的代碼中有一些錯誤,例如沒有使用過的本地代碼,以及對所有東西都進行了分析),注入的代碼沒有報告任何問題。注入的代碼:

namespace CodeInjection 
{ 
    public static class DynConcatenateString 
    { 
     public static string Concatenate(string s1, string s2){ 
      // System.Diagnostics.Debugger.Break(); // break here for debugger and also test comment output 
      int a = 1+2+3+4+5; // complicated math 
      int b = a+2; 
      int c = 0; 

      return s1 + " !"+b+"! " + s2; 
     } 
    } 
} 

我想對生成的代碼(主要是覆蓋率)使用coverage,分析和分析。

回答

1

在VS 2012中的好的代碼覆蓋率並沒有完全支持動態生成的代碼。

您可以run our analyze command看到你的模塊已跳過這個同樣的原因,還是因爲缺少符號/ P

+0

也跳過了(這是我跳過代碼覆蓋後的第一次嘗試)。此外jetbrains封面和其他工具錯過。現在我正在更深入地討論這個話題,希望下週會有結果,所以我會寄到這裏。請評價我的問題,不管你認爲它是否有用。 – cseppento

0

在你的項目中,添加一個新的XML文件,並將其命名爲dynamic.runsettings

它應包含以下內容:

<?xml version="1.0" encoding="utf-8" ?> 
<RunSettings> 
    <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> 
<!-- 
About include/exclude lists: 
Empty "Include" clauses imply all; empty "Exclude" clauses imply none. 
Each element in the list is a regular expression (ECMAScript syntax). 
An item must first match at least one entry in the include list to be included. 
Included items must then not match any entries in the exclude list to remain included. 
It is considered an error to exclude all items from instrumentation as no data would be collected. 
--> 
       <Include> 
       <!-- <ModulePath>.*\\UnitTestProject1\.dll</ModulePath> --> 
       </Include> 
       <Exclude> 
       <ModulePath>.*CPPUnitTestFramework.*</ModulePath> 
       </Exclude> 
      </ModulePaths> 
      <UseVerifiableInstrumentation>True</UseVerifiableInstrumentation> 
      <AllowLowIntegrityProcesses>True</AllowLowIntegrityProcesses> 
      <CollectFromChildProcesses>True</CollectFromChildProcesses> 
      <CollectAspDotNet>False</CollectAspDotNet> 
<!-- 
Additional paths to search for symbol files. Symbols must be found for modules to be instrumented. 
If symbols are alongside the binaries, they are automatically picked up. Otherwise specify the here. 
Note that searching for symbols increases code coverage runtime. So keep this small and local. 
<SymbolSearchPaths> 
    <Path>C:\Users\User\Documents\Visual Studio 11\Projects\ProjectX\bin\Debug</Path> 
    <Path>\\mybuildshare\builds\ProjectX</Path> 
</SymbolSearchPaths> 
--> 
      <Functions> 
       <Exclude> 
       <Function>^std::.*</Function> 
       <Function>^ATL::.*</Function> 
       <Function>.*::__GetTestMethodInfo.*</Function> 
       <Function>^Microsoft::VisualStudio::CppCodeCoverageFramework::.*</Function> 
       <Function>^Microsoft::VisualStudio::CppUnitTestFramework::.*</Function> 
       <Function>.*::YOU_CAN_ONLY_DESIGNATE_ONE_.*</Function> 
       </Exclude> 
      </Functions> 
      <Attributes> 
       <Exclude> 
       <Attribute>^System.Diagnostics.DebuggerHiddenAttribute$</Attribute> 
       <Attribute>^System.Diagnostics.DebuggerNonUserCodeAttribute$</Attribute> 
       <Attribute>^System.Runtime.CompilerServices.CompilerGeneratedAttribute$</Attribute> 
       <Attribute>^System.CodeDom.Compiler.GeneratedCodeAttribute$</Attribute> 
       <Attribute>^System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute$</Attribute> 
       </Exclude> 
      </Attributes> 
      <Sources> 
       <Exclude> 
       <Source>.*\\atlmfc\\.*</Source> 
       <Source>.*\\vctools\\.*</Source> 
       <Source>.*\\public\\sdk\\.*</Source> 
       <Source>.*\\microsoft sdks\\.*</Source> 
       <Source>.*\\vc\\include\\.*</Source> 
       </Exclude> 
      </Sources> 
      <CompanyNames> 
       <Exclude> 
       <CompanyName>.*microsoft.*</CompanyName> 
       </Exclude> 
      </CompanyNames> 
      <PublicKeyTokens> 
       <Exclude> 
       <PublicKeyToken>^B77A5C561934E089$</PublicKeyToken> 
       <PublicKeyToken>^B03F5F7F11D50A3A$</PublicKeyToken> 
       <PublicKeyToken>^31BF3856AD364E35$</PublicKeyToken> 
       <PublicKeyToken>^89845DCD8080CC91$</PublicKeyToken> 
       <PublicKeyToken>^71E9BCE111E9429C$</PublicKeyToken> 
       <PublicKeyToken>^8F50407C4E9E73B6$</PublicKeyToken> 
       <PublicKeyToken>^E361AF139669C375$</PublicKeyToken> 
       </Exclude> 
      </PublicKeyTokens> 
      </CodeCoverage> 
     </Configuration> 
     </DataCollector> 
    </DataCollectors> 
    </DataCollectionRunSettings> 
</RunSettings> 

保存這一點。該文件的特殊之處在於CodeCoverage \ ModulePaths \ Include部分將其ModulePath子註釋掉了。這將包括它在代碼覆蓋範圍內找到的任何dll。

現在點擊測試 - >測試設置 - >選擇測試設置文件並指定dynamic.runsettings文件。您的代碼覆蓋範圍現在將使用此自定義設置文件。

點擊分析代碼覆蓋率並運行任何你喜歡的測試。您應該看到您DynamicCode.dll在你的覆蓋率結果顯示(我模糊了我的工作站名稱如下):

enter image description here

請注意,我添加了一個新的方法將.cs.txt文件名爲BONK並且它(正確)顯示爲100%被發現。