Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code coverage, analysis and profiling for dynamically generated code

I have a demo project, which creates an assembly and uses it. I also can debug the injected code. But if I run coverage, analysis or profiling, it is counted, but I want to measure it.

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

I create the DLL to check the generated IL code. I can debug the code in VS. But when I run coverage, the generated assembly is simply missed, if I use TEMP directory, or if I output the DLL (like above) NO FILE is included in the coverage (so not even the main assembly).

When I run profiling, I can only see the invoke (reflection), but nothing about the generated code. When I do analysis (I have some errors in the injected code, e.g not used locals, and ofc analysis for everything), no problems are reported from the injected code. The injected code:

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

I would like to use coverage, profiling and analysis on the generated code (mainly coverage).

like image 666
lajos.cseppento Avatar asked Jun 04 '13 17:06

lajos.cseppento


People also ask

What is automated code coverage analysis?

Code Coverage is a measurement of how many lines, statements, or blocks of your code are tested using your suite of automated tests. It's an essential metric to understand the quality of your QA efforts.

What is code coverage and code analysis?

Q: What is Code Coverage Analysis? Code Coverage Analysis is the process of discovering code within a program that is not being exercised by test cases. This information can then be used to improve the test suite, either by adding tests or modifying existing tests to increase coverage.

Which technique is used to measure code coverage?

Code Coverage Analysis is simply a structural testing technique to measure how many lines/blocks/arcs of your implemented code are executed while the automated tests are running. Its analysis gives you a quick, automated and accurate quality & coverage measurement for test plans.

How do you analyze a code coverage report?

On the Test menu, select Analyze Code Coverage for All Tests. You can also run code coverage from the Test Explorer tool window. Show Code Coverage Coloring in the Code Coverage Results window. By default, code that is covered by tests is highlighted in light blue.


1 Answers

Ok code coverage in VS 2012 does not have full support for dynamically generated code.

You can run our analyze command to see that your module has been skipped for this very same reason or is it because of a missing symbol/p

like image 51
allen Avatar answered Sep 28 '22 00:09

allen