Exclude auto properties from Cover Coverage in Visual Studio 2015

I just updated a bunch of projects to VS2015 / C # 6.

MSTest Code Coverage analysis now reports that some automatic properties are not covered by unit tests. This was not the case in Visual Studio 2013, and I suspect that this may be due to new autoproperty features in C # 6.

Dealing with all the false positives that this generates is more likely to hit the purpose of the Code Coverage tool, since it is almost impossible to identify actual code that does not have testing coverage. We don’t want to write unit tests for all of our DTOs, and I really wouldn’t have to go through the project annotating every automatic property with ExcludeFromCodeCoverage .

I created a working MCVE at https://github.com/iaingalloway/VisualStudioCodeCoverageIssue


  • Open VisualStudio2013.sln in Visual Studio 2013 Premium or Ultimate.
  • Click Test β†’ Code Coverage Analysis β†’ All Tests.
  • Note that in the "Code Coverage Results" window, 0 "Not Covered" blocks are reported.

  • Open VisualStudio2015.sln in Visual Studio 2015 Enterprise.
  • Click Test β†’ Code Coverage Analysis β†’ All Tests.
  • Please note that in the "Code Coverage Results" window, 1 "Not Covered" block is displayed (recipient for example).

Can I customize the built-in Cover Coverage tool in Visual Studio 2015 to ignore automatic properties like Visual Studio 2013?

+5
source share
3 answers

As a workaround, you can add the following to the .runsettings file: -

 <RunSettings> <DataCollectionRunSettings> <DataCollector ...> <Configuration> <CodeCoverage> <Functions> <Exclude> <Function>.*get_.*</Function> <Function>.*set_.*</Function> </Exclude> ... 

This is not a very difficult solution, but until you use any functions with "get_" or "set_" in the names, you should get the behavior you need.

+4
source

I think [ExcludeFromCodeCoverage] is your only option. This is just the one time you will need to do. I personally write unit tests for getter / seters properties, especially those in WPF, where I want to make sure that property change notifications occur.

0
source

I did not like to filter all get / set methods, especially since I sometimes write get and set the logic that needs to be checked. For me, just for basic coverage of relatively simple models, the following pairs of xUnit tests worked well:

 public class ModelsGetSetTest { [ClassData(typeof(ModelTestDataGenerator))] [Theory] public void GettersGetWithoutError<T>(T model) where T: BaseEntity { PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); for (var i = 0; i < properties.Length; i++) { var prop = properties[i]; prop.GetValue(model); } } [ClassData(typeof(ModelTestDataGenerator))] [Theory] public void SettersSetWithoutError<T>(T model) where T : BaseEntity { PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); for (var i = 0; i < properties.Length; i++) { var prop = properties[i]; prop.SetValue(model, null); } } public class ModelTestDataGenerator : IEnumerable<object[]> { private readonly List<object[]> _data = new List<object[]> { new object[] { new UserRole() }, new object[] { new User() }, new object[] { new Role() } }; public IEnumerator<object[]> GetEnumerator() => _data.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } } 
0
source

All Articles