Silence Debug.Log for unit test?


I am learning how to use the NUnit unit testing framework for Unity. I have found that when all of the tests get run (with the built-in editor testing tool), all of the error/logging that my code has gets poured into the console which is not necessarily what was desired. For example, if I have a piece of code that recognizes that an array is the wrong size and displays an error, I will want to have a test case for it. I do not have it actually print the error to the console every time the tests get run.

The best way (if the tests run in a menial amount of time) to unit test is to run all tests whenever the code changes. Unity does give this as an option (right click on “Editor Tests” tab → enable “Run on recompilation”). But then the console gets dumped on…


Is there any way to disable the logging and error printouts for the code that the unit tester executes? Perhaps a decorator for a unit testing class or method?

Just turn it off, Debug.logger returns the current logger and has field/property for that:

Debug.logger.logEnabled = false;

Optionally you can create your own attribute to enable or disable and decorate your classes/methods to change whether it’s enabled.

using System;
using UnityEngine;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)]
public class DebugLoggerEnablement : Attribute
	public DebugLoggerEnablement(bool enableLogger)
		Debug.logger.logEnabled = enableLogger;


public void SomeUnitTest()
	//Testing stuff

Simply disable the logger at the beginning of the unit testing code:

Debug.logger.logEnabled = false;

Setting logEnabled will do the trick.