- Home /
The question is answered, right answer was accepted
Silence Debug.Log for unit test?
Background
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...
Question
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?
Answer by Landern · Nov 10, 2016 at 06:52 PM
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;
}
}
Usage:
[DebugLoggerEnablement(false)]
public void SomeUnitTest()
{
//Testing stuff
}
Thanks, I had just figured it out as you can see in my answer. I'll accept your answer though so we both get "points", and for the sake of the attribute example.
Answer by Graphics_Dev · Nov 10, 2016 at 06:51 PM
Simply disable the logger at the beginning of the unit testing code:
Debug.logger.logEnabled = false;
Setting logEnabled will do the trick.
Of course this disables the logger across the board (not just for the section of code), so you will likely want to set it to true
when the test cases have finished.
Follow this Question
Related Questions
Unit Testing Rigidbody2D without polluting scene with gameobjects 1 Answer
Load scene/prefab when running a test through the test runner 1 Answer
Monobehaviour.Start() is called when Playmode tests are executed 0 Answers
MSTools instead of NUnit 0 Answers
How to load a scene in PlayMode tests 2 Answers