简体   繁体   English

代码覆盖范围不包括Visual Studio中的属性

[英]Code coverage not including properties in Visual Studio

I have a C# project in Visual Studio 2010 for which I am writing unit tests using the unit testing framework. 我在Visual Studio 2010中有一个C#项目,我正在使用单元测试框架编写单元测试。 When I view the code coverage results for a test run, the coverage does not include properties. 当我查看测试运行的代码覆盖率结果时,覆盖率不包括属性。 It doesn't show the properties as being either tested or non-tested, as if they aren't important at all. 它没有将属性显示为已测试或未测试,就好像它们根本不重要一样。 Is there a setting I need to flip to turn on code coverage for properties? 是否需要翻转设置以打开属性的代码覆盖率?

Also note that I have already checked the .testsettings file and nothing is set to be excluded from code coverage, nor have I added any attributes to the classes/properties that would exclude them from coverage. 还要注意,我已经检查了.testsettings文件,并且没有将任何内容设置为不包括在代码覆盖范围内,也没有将任何属性添加到类/属性中以将它们排除在覆盖范围之外。

Automatic Properties do not appear to get added to Code Coverage, so I would check the implementation of your own properties. 自动属性似乎没有添加到代码覆盖率中,因此我将检查您自己的属性的实现。

For example, the following code produces 100% code coverage; 例如,以下代码产生100%的代码覆盖率;

namespace ClassLibrary1
{
    public class Class1
    {
        public int Property1 { get; set; }
    }
}


[TestMethod]
public void TestMethod1()
{
    var test = new Class1();
    Assert.IsNotNull(test);
}

Whereas the same test with the following changes to the Class give 40% coverage; 而对类别进行以下更改的相同测试覆盖率达到40%;

 public class Class1
    {
        private int _property1;

        public int Property1
        {
            get { return _property1; } 
            set { _property1 = value; }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM