Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Nov 10, 2016 at 07:06 PM by Graphics_Dev for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Graphics_Dev · Nov 10, 2016 at 06:41 PM · testingloggingautomationunittestprinting

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
2
Best Answer

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
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Graphics_Dev · Nov 10, 2016 at 07:03 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Graphics_Dev · Nov 10, 2016 at 07:00 PM 0
Share

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

Answers Answers and Comments

57 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges