har07 har07 - 1 month ago 8
C# Question

Why Asserts equals 0 (zero) in NUnit Android Test Runner?

As mentioned in the title, why is it displayed

Asserts: 0
in my NUnit Android Test Runner, despite there are two
Assert
s being called in the corresponding test method? Just curious if I was doing assertion the wrong way, or maybe the information displayed means completely different from what I think it does.

enter image description here

The test method is as posted below. Breakpoints on the two
Assert
lines are hit as expected.

[Test]
public async Task TestDeleteHistoryShouldEmptyTable()
{
var newBookmark = new Bookmark
{
Id = "foo",
Page = 100,
CreatedBy = "john doe",
CreatedDate = DateTime.Now
};
await _mainVM.Repo.SaveAsync(newBookmark);
var historyBefore = await _mainVM.Repo.GetLatestBookmarksAsync(Helpers.HistorySize);
Assert.True(historyBefore.Any());

await _mainVM.Repo.ClearTable("Bookmark");
var historyAfter = await _mainVM.Repo.GetLatestBookmarksAsync(Helpers.HistorySize);
Assert.True(!historyAfter.Any());
}


UPDATE :

The problem can also be reproduced using the following test method :

[Test]
public async Task TestAsyncCounter()
{
await FakeAsyncMethod();
Assert.True(true);
}

public async Task FakeAsyncMethod()
{
await Task.Delay(1000);
}

Answer

This turned out to be a framework bug, as confirmed by @CharliePoole, one of the NUnit owner in their issue tracker:

"OK, I have to take back that last comment. It works correctly when the awaited method doesn't contain an await itself and is therefore processed synchronously. I'm seeing odd results when I include await Task.Delay(1) in the awaited method. So there is definitely a framework bug and it can even be seen in .NET 4.5."

Let's just wait for updates in the issue tracker for now.


Updated issue tracker link : https://github.com/nunit/nunit/issues/1499