18

Tôi có một dự án demo, tạo ra một assembly và sử dụng nó. Tôi cũng có thể gỡ lỗi mã được tiêm. Nhưng nếu tôi chạy bảo hiểm, phân tích hoặc lược tả, nó được tính, nhưng tôi muốn đo lường nó.Phạm vi, phân tích và lược tả mã cho mã được tạo động

Code:

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"); 

tôi tạo ra các DLL để kiểm tra mã IL được tạo ra. Tôi có thể gỡ lỗi mã trong VS. Nhưng khi tôi chạy vùng phủ sóng, lắp ráp được tạo ra chỉ đơn giản là bỏ qua, nếu tôi sử dụng thư mục TEMP, hoặc nếu tôi xuất DLL (như trên) NO FILE được bao gồm trong vùng phủ sóng (vì vậy thậm chí không lắp ráp chính).

Khi tôi chạy hồ sơ, tôi chỉ có thể thấy lời gọi (phản chiếu), nhưng không có gì về mã được tạo. Khi tôi phân tích (tôi có một số lỗi trong mã được tiêm, ví dụ: không được sử dụng địa phương và phân tích ofc cho mọi thứ), không có sự cố nào được báo cáo từ mã được tiêm. Mã được tiêm:

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; 
     } 
    } 
} 

Tôi muốn sử dụng bảo hiểm, lược tả và phân tích về mã được tạo (chủ yếu là mức độ phù hợp).

Trả lời

1

Bảo hành mã Ok trong VS 2012 không có hỗ trợ đầy đủ cho mã được tạo động.

Bạn có thể run our analyze command để thấy rằng mô-đun của bạn đã được bỏ qua vì lý do này rất giống nhau hoặc là nó vì một biểu tượng mất tích/p

+0

Cũng bị bỏ qua (là lần thử đầu tiên của tôi sau khi mã bị bỏ qua). Ngoài ra jetbrains bao gồm và các công cụ khác bỏ lỡ. Bây giờ tôi sẽ đi sâu hơn trong chủ đề, hy vọng với kết quả trong tuần tới, vì vậy tôi sẽ gửi ở đây. Vui lòng đánh giá câu hỏi của tôi, cho dù bạn cho rằng nó hữu ích hay không. – cseppento

0

Trong dự án của bạn, thêm một file xml mới và đặt tên nó dynamic.runsettings.

Nó nên chứa những điều sau đây:

<?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> 

Save đó. Có gì đặc biệt về tệp này là phần CodeCoverage \ ModulePaths \ Include có con ModulePath của nó đã nhận xét. Điều này bây giờ sẽ bao gồm bất kỳ dlls nó tìm thấy trong phạm vi mã.

Bây giờ hãy nhấp vào Kiểm tra -> Cài đặt kiểm tra -> Chọn Tệp cài đặt thử nghiệm và chỉ định tệp dynamic.runsettings. Phạm vi mã của bạn bây giờ sẽ sử dụng tệp cài đặt tùy chỉnh này.

Nhấp vào Phân tích phạm vi mã và chạy bất kỳ thử nghiệm nào bạn muốn. Bạn sẽ thấy DynamicCode.dll của bạn hiển thị trong kết quả bảo hiểm của bạn (Tôi đã che khuất tên trạm làm việc của tôi dưới đây):

enter image description here

Lưu ý rằng tôi đã thêm một phương pháp mới để các tập tin .cs.txt gọi BONK và nó (chính xác) xuất hiện như 100% phát hiện.