使用 dotCover 时测试结果不一致
我有一些带有单元测试的代码,这些代码在调试版本中通过,但在发布版本中失败,这是正确的。但是,当使用 JetBrains dotCover 运行时,相同的测试可以在调试和发布模式下通过。
为了提供一些背景知识,这里是有问题的测试代码,只是为了让您了解为什么它在发布版本中失败 - 这基本上是因为代码优化导致堆栈信息减少。
using System.Diagnostics;
using NUnit.Framework;
namespace DotCoverTest
{
[TestFixture]
public class TestLogger
{
[Test]
public void GetCurrentClassLoggerReturnsLoggerWithOwningTypeName()
{
Assert.AreEqual(Logger.GetCurrentClassLogger(), GetType().Name);
}
}
public class Logger
{
public static string GetCurrentClassLogger()
{
return new StackFrame(1, false).GetMethod().DeclaringType.Name;
}
}
}
编辑: 我有什么想法可以设置我的构建,以便在使用或不使用覆盖工具的情况下获得相同的测试结果吗?
注意:这个问题最初发布时认为这是 TeamCity 的问题,但事实并非如此。
I have some code with unit tests that pass in a Debug build but fail in a Release build which is correct. However, the same tests pass in both Debug and Release mode when run using JetBrains dotCover.
To give a bit of background, here is the offending test code, just to give you in idea of why it's failing for a Release build - it's basically because of reduced stack information due to code optimization.
using System.Diagnostics;
using NUnit.Framework;
namespace DotCoverTest
{
[TestFixture]
public class TestLogger
{
[Test]
public void GetCurrentClassLoggerReturnsLoggerWithOwningTypeName()
{
Assert.AreEqual(Logger.GetCurrentClassLogger(), GetType().Name);
}
}
public class Logger
{
public static string GetCurrentClassLogger()
{
return new StackFrame(1, false).GetMethod().DeclaringType.Name;
}
}
}
EDIT:
Any ideas how I can set up my build so that I get the same test results with or without a coverage tool ?
NOTE: This question was initially posted believing that it was a problem with TeamCity but it is not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信这里的主要问题是尾调用优化,编译器折叠堆栈帧以提高性能。这种情况仅发生在发布模式下。
dotCover(与其他 .net 分析器一样)禁用一些 CLR 的优化,防止结果受损 - 如果方法不执行,则很难计算方法的执行次数...
我无法告诉您 NCover 是否无法克服优化,或以另一种方式解决这个问题,但我对 dotCover 很确定。
I believe that the main issue here is Tail call optimization, Where the compiler collapses stack frames to boost performance. This happens only in Release mode.
dotCover (as other .net profilers) disables some of the CLR's optimizations, preventing compromised results - It would be hard to count method executions if they don't execute...
I can't tell you if NCover fails to overcome the optimizations, or solves this in another way, but i'm quite sure about dotCover.