Exclude auto properties from Code Coverage in Visual Studio 2015

后端 未结 3 1003
没有蜡笔的小新
没有蜡笔的小新 2020-12-06 06:38

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 wa

相关标签:
3条回答
  • 2020-12-06 07:13

    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.

    0 讨论(0)
  • 2020-12-06 07:17

    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)
            {
                var properties =
                    typeof(T).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                for (var i = 0; i < properties.Length; i++)
                {
                    var prop = properties[i];
                    if (prop.GetGetMethod(true) != null)
                        prop.GetValue(model);
                }
            }
    
            [ClassData(typeof(ModelTestDataGenerator))]
            [Theory]
            public void SettersSetWithoutError<T>(T model)
            {
                var properties =
                    typeof(T).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                for (var i = 0; i < properties.Length; i++)
                {
                    var prop = properties[i];
                    if (prop.GetSetMethod(true) != null)
                        prop.SetValue(model, null);
                }
            }
    
            public class ModelTestDataGenerator : IEnumerable<object[]>
            {
                private readonly List<object[]> _data = new List<object[]>();
    
                public ModelTestDataGenerator()
                {
                    var assembly = typeof(Program).Assembly;
                    var nsprefix = $"{typeof(Program).Namespace}.{nameof(Models)}";
                    var modelTypes = assembly.GetTypes()
                        .Where(t => t.IsClass && !t.IsGenericType) // can instantiate without much hubbub
                        .Where(t => t.Namespace.StartsWith(nsprefix)) // is a model
                        .Where(t => t.GetConstructor(Type.EmptyTypes) != null) // has parameterless constructor
                        .ToList();
                    foreach (var modelType in modelTypes) _data.Add(new[] {Activator.CreateInstance(modelType)});
                }
    
                public IEnumerator<object[]> GetEnumerator()
                {
                    return _data.GetEnumerator();
                }
    
                IEnumerator IEnumerable.GetEnumerator()
                {
                    return GetEnumerator();
                }
            }
        }
    

    Updated 2020-03-18: This version uses reflection to find your models under a particular namespace.

    0 讨论(0)
  • 2020-12-06 07:18

    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.

    0 讨论(0)
提交回复
热议问题