Exclude auto properties from Code Coverage in Visual Studio 2015

两盒软妹~` 提交于 2019-12-28 15:50:10

问题


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

Now MSTest's Code Coverage analysis is reporting that some auto properties aren't covered by unit tests. This wasn't the case in Visual Studio 2013, and I suspect it may be something to do with the new autoproperty features in C#6.

Dealing with all the false-positives this generates rather defeats the purpose of the Code Coverage tool as it makes it practically impossible to identify actual code lacking test coverage. We don't want to write unit tests for all our DTOs, and I'd really rather not have to go through the project annotating every single auto-property with ExcludeFromCodeCoverage.

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


  • Open VisualStudio2013.sln in Visual Studio 2013 Premium or Ultimate.
  • Click Test -> Analyze Code Coverage -> All Tests.
  • Observe that the "Code Coverage Results" window reports 0 Blocks "Not Covered".

  • Open VisualStudio2015.sln in Visual Studio 2015 Enterprise.
  • Click Test -> Analyze Code Coverage -> All Tests.
  • Observe that the "Code Coverage Results" window reports 1 Block "Not Covered" (the getter for ExampleDto.Value)

Is it possible to configure the built-in Code Coverage tool in Visual Studio 2015 to ignore auto-properties like Visual Studio 2013 does?


回答1:


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

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

It's not a great workaround, but as long as you aren't using any functions with "get_" or "set_" in the names it should get you the behaviour you need.




回答2:


I didn't like filtering all get/set methods, especially since I sometimes write get and set logic that needs to be tested. For me, for just basic coverage of relatively simple models, the following pair of xUnit tests has 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();
    }
}



回答3:


I think [ExcludeFromCodeCoverage] is your only option. It is just a one-time thing you are going to have to do. I, personally, DO write unit tests on the property getter/setters, especially ones such as in WPF, where I want to be sure the property change notifications occur.



来源:https://stackoverflow.com/questions/32329430/exclude-auto-properties-from-code-coverage-in-visual-studio-2015

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!