Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit, is it possible to continue executing test after Assert fails?

In a test that contains some asserts, for example:

Assert.AreEqual(1,1); Assert.AreEqual(2,1); Assert.AreEqual(2,2); 

is it possible to let the test keep running after it fails at some point? In the example, first condition is true, second fails and the test stops. I'd like to evaluate also the following condition.

like image 748
ccalboni Avatar asked May 14 '10 14:05

ccalboni


People also ask

How do you continue the test even if the assert fails?

They don't throw an exception when an assert fails. The execution will continue with the next step after the assert statement. If you need/want to throw an exception (if such occurs) then you need to use assertAll() method as a last statement in the @Test and test suite again continue with next @Test as it is.

What will happen if assert command fails?

When an “assert” command fails, the test execution will be aborted.

What is assert fail?

The Assert. Fail method provides you with the ability to generate a failure based on tests that are not encapsulated by the other methods. It is also useful in developing your own project-specific assertions.


1 Answers

I prefer to be practical and put several related assertions in one method.

I have a helper class which enables the following syntax (I use):

AssertAll.Succeed(     () => Assert.AreEqual("bb", id.Context),     () => Assert.AreEqual("abc", id.FullName),     () => Assert.AreEqual("b", id.SessionID)); 

which gives me error messages like this:

Assert.AreEqual failed. Expected:<bb>. Actual:<b\c>.  Assert.AreEqual failed. Expected:<abc>. Actual:<[b\c]a{103}>.  at FXP_COM.Tests.EnumToStringConverterterTests.<>c__DisplayClass3.<ShouldConvert>b__0() in UnitTest1.cs: line 31 at FXP_COM.Tests.AssertAll.Succeed(Action[] assertions) in UnitTest1.cs: line 46 at FXP_COM.Tests.AssertAll.Succeed(Action[] assertions) in UnitTest1.cs: line 62 at FXP_COM.Tests.EnumToStringConverterterTests.ShouldConvert() in UnitTest1.cs: line 30 

and the helper class is the following:

using System; using NUnit.Framework; using System.Collections.Generic; using System.Linq; using System.Reflection;  public static class AssertAll {     public static void Succeed(params Action[] assertions)     {         var errors = new List<Exception>();          foreach (var assertion in assertions)             try             {                 assertion();             }             catch (Exception ex)             {                 errors.Add(ex);             }          if (errors.Any())         {             var ex = new AssertionException(                 string.Join(Environment.NewLine, errors.Select(e => e.Message)),                 errors.First());              // Use stack trace from the first exception to ensure first             // failed Assert is one click away             ReplaceStackTrace(ex, errors.First().StackTrace);              throw ex;         }     }      static void ReplaceStackTrace(Exception exception, string stackTrace)     {         var remoteStackTraceString = typeof(Exception)             .GetField("_remoteStackTraceString",                 BindingFlags.Instance | BindingFlags.NonPublic);          remoteStackTraceString.SetValue(exception, stackTrace);     } } 
like image 75
Konstantin Spirin Avatar answered Sep 23 '22 02:09

Konstantin Spirin