I wrote some code that looks like the following, to unit-test the implementation of a C# finalizer method.
In TeamCity version 2017.1.3, I use a build step with the NUnit runner type to run this test using NUnit.ConsoleRunner.3.7.0.
The test fails if I enable the JetBrains dotCover .NET Coverage tool.
The test passes if I disable .NET Coverage.
What can dotCover possibly be doing to influence test outcome?
using System;
using System.Threading;
using NUnit.Framework;
using Telerik.JustMock;
namespace MyNamespace
{
public interface IWin32Api
{
void FreeResource();
}
public class Disposable
{
private readonly IWin32Api _win32;
public Disposable(IWin32Api win32)
{
_win32 = win32;
}
~Disposable()
{
_win32.FreeResource();
}
}
[TestFixture]
public class TestFixture
{
[Test]
public void Test()
{
using (var signal = new ManualResetEvent(false))
{
var win32 = Mock.Create<IWin32Api>();
Mock.Arrange(() => win32.FreeResource())
.DoInstead(() => { signal.Set(); });
var subjectUnderTest = new Disposable(win32);
subjectUnderTest = null;
GC.Collect();
if (!signal.WaitOne(TimeSpan.FromMinutes(1)))
{
Assert.Fail("IWin32Api.FreeResource never called");
}
}
}
}
}
nunit-agent.exe
executable. When I turn off code coverage, onlynunit3-console.exe
is run;nunit-agent.exe
is not run. Couldnunit-agent.exe
have something to do with finalizers not being run during garbage collection? – hwaien