MS 测试代码覆盖率返回错误结果
我已经用 MVVM 模式编写了一个 .Net 4 WPF 应用程序,并为该应用程序编写了几个单元测试。我在 local.testsettings 中启用了代码覆盖率并启用了检测。我的问题是代码覆盖率似乎没有返回正确的结果。它表明我的代码的 86% 已被分析。然而,一些它说没有被命中的代码却被命中了。为了证明这一点,我在代码上放置了断点并对单元测试进行了调试。断点被击中,因此代码肯定被覆盖。为什么代码覆盖率看不到代码被命中?
我正在尝试使用 Visual Studio 2010 SP1 进行此操作,并从“测试”视图运行测试。
有什么想法吗?
根据请求,这里有一些代码。这是整个班级。代码覆盖率说构造函数没有被命中,但它确实被命中了。 “Channel = item;”上的断点调试单元测试时会命中该行。
public class ChannelEventArgs : EventArgs
{
public ChannelEventArgs(IChannel item)
{
Channel = item;
}
public IChannel Channel { get; set; }
}
I have written a .Net 4 WPF app in the MVVM pattern and have written several unit tests for the app. I enabled code coverage and enabled instrumentation in the local.testsettings. My issue is that the Code Coverage does not appear to be returning correct results. It shows that 86% of my code is analyzed. However, some of the code that it says is not being hit is hit. To prove it, I put breakpoints on the code and did a Debug of the unit tests. The breakpoints were hit, so the code is definitely being covered. Why would code coverage not see that the code is hit?
I am attempting this with Visual Studio 2010 SP1 and running the tests from the Test view.
Any ideas?
Per request, here is some code. This is the entire class. Code Coverage says the constructor is not hit, but it is. A breakpoint on the "Channel = item;" line is hit when debugging the unit tests.
public class ChannelEventArgs : EventArgs
{
public ChannelEventArgs(IChannel item)
{
Channel = item;
}
public IChannel Channel { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅鉴于这么多信息,我有根据的猜测是,您在检测程序集时遇到了问题。
检查我的回答中的项目列表:
在 Visual Studio 2010 中运行 NUnit 测试代码覆盖率
特别是第一项(“仪器就位”)、第三项(构建到单个文件夹中)和最后一项(命令行选项/noshadow)将具有您所描述的效果。
我还可以想到一种奇怪的情况,其中您的测试代码加载相应的程序集,但您不会编写单元测试。
Given only that much information, my educated guess is, that you have a problem with instrumenting the assemblies.
Check the list of items in my answer to:
Running NUnit tests in Visual Studio 2010 with code coverage
Especially the first ("instrument in place"), third item (build into a single folder) and last item (command line option /noshadow) would have the effect you described.
I can also think up an odd case in which your tested code loads the respective assembly but then you wouldn't have written a unit test.