I am using Visual Stdio 2010 and its testing and code coverage features, and I have a bunch of generated code that I don't want to be included in code coverage results. These are generated as partial classes.
I've read in a few places (example: How to ignore generated code from code coverage data) how I can have the code coverage tools ignore the entire class or specific members using attributes. So I've modified the generator to include the [ExcludeFromCodeCoverage] attribute on all of the classes it generates, but that prevents the code I write in partials from being tracked for code coverage as well.
How can I set things up such that code I manually write in partials are included in the code coverage results, but the generated code is not? The only option I am seeing now is to dig into the generator (a long, ugly T4 template) and add the [ExcludeFromCodeCoverage] attribute at the property/method level instead of at the class level. Is there another way? I haven't seen anything like [IncludeFromCodeCoverage] that I could add to my handful of properties/methods hand-written in partials to include those while ignoring the rest of the auto-generated class.
You are not alone in requesting this as this entry on ExcludeFromCodeCoverage and other How to ignore generated code from code coverage data shows.
The other way I have seen this done with some other (eg ncover, partcover) coverage tools is to post process the results ie if XML use an xslt transform the xml to remove the coverage data related to the generated partials and then generate the coverage report.
You may be able to apply this approach or something similar as it looks like you can get access to the coverage data in XML.
OpenCover has a file filter that allows you to exclude all methods in a file that matches the filters, created for just this scenario as the names of generated files tend to follow known patterns, it also allows more than one filter.
Related
I have created a VS Load Test (.webtest) from scratch by recording from browser. Then I generated a coded version in C# and made some changes to customize it. My question is: Is there any way to update the original visual .webtest from the updated coded version to be that code changes reflected?
No.
You might be successful with an iterative approach. Take the original ".webtest" file and convert it to C# code. Compare that code against your modified C# code. Adjust the ".webtest" so is more like the modified code. Convert again and repeat. The "adjustments" could include adding or deleting requests until the request names match in the generated code. Then adding or removing other items. You might also move parts of your modified code into methods that you can then rewrite as plugins, validation rules or extraction rules.
See also the last paragraph of this answer. Although it refers to VB it applies equally well to C#.
I'm trying to create a T4 template that will save our developers from creating a lot of boilerplate code that's necessary in our framework. Let's say the developer creates an interface and marks it with our custom attribute. I would like it so that any interface marked with that custom attribute is enhanced by additional methods, which means my T4 template would have to generate partial classes on the fly. However, I would like it so that this automatic generation happens on the fly and seamlessly, preferably when the internal automatic compilation that's used for intellisense happens. You know how when you create a new class in Visual Studio and you switch to another source file and start using that class you didn't have to save or compile it, Intellisense was able to see the new class you created right away? I'd like the same automatic behavior with the code generated from my T4 template. Any thoughts?
You cannot do what you want to do easily, but here are some options ordered from easiest to most likely what you want (hardest).
Create code
snippets
Create a Visual Studio Item Template
Use Castle DynamicProxy to create the extra bits at run time.
Create a separate project to hold the T4 generated classes as described in my answer here
As a pre-step to your project build (modify .csproj file to do this), you can compile the source code from which you want to generated code and then reflect on that, generate the code and then add it to the project before the real compile step. This is what the MSR Orleans project does. You can read their source code here. This is really cool! :-)
My boss wants to start using SpecFlow, and one of our concerns is that we developers do not cleanup any non-used steps. I've found that you can execute stepflow.exe with the 'stepdefinitionreport' argument to generate a report on the defined steps, which even includes steps that aren't used anymore. The output is an html page, which highlights orphan steps with a red color. Those steps are listed using plain English and not the name of the method.
(1) Is there a way to get the name of these orphan methods using specflow.exe? I am not really interested in Visual Studio addins since we will have 50-100 SpecFlow projects, and thus I prefer to use the command line.
(2) Also, can I somehow specify the output format, e.g. xml / json instead of html?
I've been searching on the web for this, and maybe I'm just using the wrong keywords or something? I could use some help.
My problem is simple - we have a bunch of reference.cs files in our solution, which were auto-generated by VS2010 when adding services. These files don't add XML comments by default, so when we build the project, I get 800 or so messages in the build list. This doesn't break anything, but it does make the build take (significantly) longer, and mucks up the output screen.
I "fixed" this by adding the appropriate #pragma statments to the beginning and end of each reference.cs file, but if those ever get regenerated, they will have to be re-added by hand. I'd like to streamline that process and just add them to whatever T4 template VS2010 is using in the first place. The problem is, I don't know where that is, or if VS2010 is using something else to build these files?
Can this be done? Is there a better solution? I don't necessarily want to turn off XML comments for the entire project.
Visual Studio does not use T4 templates to generate the service reference proxy classes (Reference.cs). Instead Visual Studio is most likely using the WsdlImporter and ServiceContractGenerator classes to generate this code.
There is a stackoverflow post on using either a custom wsdl exporter or WCFExtras to add xml comments to the generated code. Both of these assume you have access to the code for the services you are referencing.
I want to generate some code from my dbml(Linq to Sql) file,the dbml file is placed in many part of my project So I wrote a custom tool for this purpose
But the problem is that dbml already has contained MSLinqToSQLGenerator custom tool ,
So do you know any way to set two custom tools for one file, If no, Let me know your idea about that
Visual Studio will only support a single "Custom Tool" per file, but you can add a pre-compilation step to run other tools against anything you want. For instance, I have the following pre-compile step set on the "Build Events" tab of one of my projects.
"$(DevEnvDir)..\..\..\Common Files\Microsoft Shared\TextTemplating\10.0\TextTransform" "$(ProjectDir)DataContext\Northwind.proxy.tt"
There's a lot of relative pathing going on here in order to find the T4 command-line tool, but you get the idea. This particular T4 file counts on being in the same directory as the .dbml file that it reads to generate its output.
Before the project is compiled, you can run whatever external tool you want. Just make sure that after the first run, you include the tool's output in the project. After that, since the file gets changed as part of a PRE-compile step, it will always be updated in each build.
You'd get proper control on the T4 if you include the LINQ to SQL T4 generator in your template's responsibility.
If I understood properly, you want to keep the default behavior of .dbml generator, but also add your own.
This seemed a bit "old", and I haven't personally used LINQ to SQL for some time, but I did use this as-is replacement of T4 generator, that produced the equivalent of the standard .dbml generator.
https://github.com/damieng/L2ST4
Not sure if that's up to date with VS 2010 version, but you can always compare the standard .dbml generated code and this T4 output and make proper changes to achieve identical outcome.
Of course you can simply have multiple different generators, and simply run them with "Transform All Templates", but based on your question, you'd want the generator to be attached to the file specific custom tool.
You might want to check out (unless its already familiar to you) also T4 Toolbox https://github.com/olegsych/T4Toolbox that adds "T4ScriptFileGenerator" custom tool to a file. It effectively runs the T4 code when the file changes.