0

I am looking for a solid way to keep track of the number of assertions made in a unit test for reporting purposes in automation. I know that with Nunit one is able to keep track of the assertions that are being made for a test with TestContext.CurrentContext.AssertCount. The problem is that my team has our own set of assertion methods using fluent assertions and am looking to find a way to increment that value anytime one is made. Below is a stripped version of one of our unit tests so that you can get an idea of how they read.

    [Test]
    public void ServiceHistoryButton_Asset(Dictionary<string, string> data)
    {
        Threaded<Session>.CurrentBlock<HomePage>()
            ...
            .VerifyThat(form => form.Containerservice.Should().Contain(data["Containerservice"]))
            .VerifyThat(form => form.Account.IsDisplayed().Should().BeTrue())
            .VerifyThat(form => form.ContainerGroup.IsDisplayed().Should().BeTrue())
            .StoreValue<AssetServiceHistoryEdit, DateTime>(form => form.Service.First().ServiceDate, out DateTime firstDate)
            .VerifyThat(form => form.Service.ElementAt(2).ServiceDate.Should().BeBefore(firstDate))
    }

I looked into the NUnit framework a bit and if I'm seeing it correctly it looks like they are extending the Assert.That method and adding an incrementer to keep track of when it is called. I thought about doing something similar, but it doesn't look like I can access the TestContext outside of the test itself, nor the setup or teardown methods. Let me know if I'm not explaining myself clearly or if I can provide anymore info. Thanks.

Jand
  • 406
  • 2
  • 7
  • 22
  • What does `VerifyThat` do? If it calls `Assert.That` or any other assert, then an increment of the count will occur. – Charlie Feb 19 '18 at 23:05

1 Answers1

0

Unfortunately there's no real hook or extension point that you can use for that. All assertions go through the current AssertionScope. You can wrap all calls in another scope, but you have no way to provide an alternative implementation of IAssertionStrategy. You could provide me with a PR to make that possible though.

Dennis Doomen
  • 8,368
  • 1
  • 32
  • 44