1

On test failure Nunit will only show evaluated expected and actual conditions. There is no way to tell which Assert failed if you have multiple asserts

Expected: True
But was: False

I know that we can pass messages to the assert, which will be shown on failure. But this means that every time I write an assert I need to write a unique message as well, which is time consuming and will require additional maintenance when updating tests.

Is there an easier way to show which assert failed by including the Assert statement within the failed statement?

for example, something like this:

Expected: True
But was: False

On: Assert.That(User.HasProducts, Is.True)
3

Personally I would spit out an output log as well as the general test value. Then you can see the failure and go check the log. You can clear the log each time you run the test. Remember it's a unit test framework, not a test automation framework. Unit tests usually are very specific and not running tons of assertions all at once. The goal would be 1 assertion per unit test or only grouped assertions if any of them cause the failure of a specific function.

You could throw a custom message for each one as you mention if you like, but if you spit a standard log function out for each step and append the file it with specific data from each assertion, you can include the content of the assertion to get each failure of step and just look at the log when you need to.

like call the log to append a line and include text like "[unit test name]_Assertion: [variable.tostring()]" with the variable being what it is you were trying to assert against. Literally spit out the text, it should mean something to you with the unit test name reference in front of it so you can see if 30 assertions exist and 3 fail you should get an idea of which ones from that.

Replace the standard assert function with your custom one that logs and then returns the true/false value so it works the same for every assertion. Naturally you would need to write your own assert class that ties back into the framework though.

Ask this on regular Stack Overflow as this is now a dev question based on comments:

How do I customize NUnit in order to write my own Assert class method that has more information in it and then tie that back into the existing NUnit framework?

Has anyone done this before and can anyone post examples to help me get started on this?

P.S. Search on there first as that may already be there in various format as some of the questions overlap. You can then delete this question from here as it's now "off-topic".

| improve this answer | |
  • In my case, I am writing automation tests using selenium and nunit. But even if I don't consider that, adding a log is double the work. Why do it when nunit3 is already doing it for me. Plus I would also like the same results in xml generated at the end of test by nunit-console. I need that to generate html report. – H.D. Apr 24 '17 at 17:28
  • Because you said it's results were insufficient. You mentioned that you needed some custom details in the results that were not default with nunit3. If that is not the case please update your question with clarification. – mutt Apr 24 '17 at 17:55
  • I am sorry if my question was not clear. What I am looking for is something in the line of customizing either Assert or Nunit results to output some additional information about the assert that failed. Creating a separate log is not very useful in my case as I need this info in nunit-console result xml. – H.D. Apr 24 '17 at 18:32
  • You can always write your own assert function that integrates with nunit and call that assert instead of the default nunit assert. It's basically the same thing as above, but instead of logging you would spit out a modified assert pass/fail message that flows through the nunit framework. You will need to look into how it's coded and tie into existing nunit structures to do it that way though. – mutt Apr 24 '17 at 20:05
  • That's exactly what I want to achieve, but I am not sure how to go about it. Can you give an example? – H.D. Apr 26 '17 at 22:44
2
use   Assert.That(User.HasProducts, Is.True, "I am failed because User.HasProducts not true")

You will see the message I am failed because User.HasProducts not true if the test failed on this assert.

| improve this answer | |
  • This would be hard to maintain. – H.D. Dec 4 '19 at 2:28
1

So, I ended up using FluentAssertion for assertions. FluentAssertion has this feature built-in.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.