Inconsistent test results when using dotCover

白昼怎懂夜的黑 提交于 2019-12-12 15:17:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!