简体   繁体   中英

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. 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.

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;

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;

 public class Class1
    {
        private int _property1;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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