问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/9017132/inconsistent-test-results-when-using-dotcover