Don't abort tests due to a LogError

I’m writing nice little unit tests like this:

var result = DoSomething();

Assert.IsTrue(result, well_formed_error_message);

But a Debug.LogError in DoSomething causes the entire test to abort and prints: “Unhandled log message: ‘[Error] blah blah blah’”.

Yeah, no. I want to run my test, please. That’s why I wrote my test. I can turn it off on a case-by-case basis with LogAssert.ignoreFailingMessages = true, but I have to add that to every unit test as adding it to [SetUp] doesn’t work.

It drives me UP THE WALLS when a Unit testing framework suddenly has opinions about how our code should be written. Tests should only fail in three instances:

  • An assertion I wrote fails.
  • An exception is thrown during execution
  • No assertions are hit

When I do a Debug.LogError instead of throwing an exception, it’s because I want the program flow to continue. Don’t turn error logs into exceptions behind my back.

12 Likes

Had the same situation and agree completely with what you say.
I just discovered that from Unity 2017 you can use:

LogAssert.ignoreFailingMessages = true;

Grabbed from: c# - PlayMode tests stopping by default if errors are logged in scripts - Stack Overflow