Search Unity

How do I print output to the console in a Unity Unit test?

Discussion in 'Scripting' started by eurasian, Nov 23, 2014.

  1. eurasian

    eurasian

    Joined:
    Oct 27, 2014
    Posts:
    3
    The best solution I've found so far is this:

    Code (CSharp):
    1.         [Test]
    2.         public void TestCase ()
    3.         {
    4.             int numHerb = 10;
    5.  
    6.             //fails, and in the failure, I can see the numHerb value
    7.             Assert.AreEqual("Whatever",numHerb.ToString());
    8.  
    9.            // outputs nothing, or anywhere I can see
    10.             Debug.WriteLine("Herbivores start {0}", numHerb.ToString());
    11.         }
    12.  
    I'm running the unit tests through "Unity Test Runner".

    The last line still doesn't show output even if it's run by itself (the exception being thrown by the failed test isn't the culprit for the output not showing to console).

    Is there a better solution? Thanks!
     
    Last edited: Nov 24, 2014
  2. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,137
    Debug.Log("Text");
     
  3. eurasian

    eurasian

    Joined:
    Oct 27, 2014
    Posts:
    3
    Which namespace is that Debug class under?
    My version of System.Diagnostics.Debug only has 'WriteLine()', and that's not showing up anywhere.
     
  4. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,137
    Oh, 'unit test'. I was referring to the UnityEngine namespace for regular stuff... my bad.
     
  5. eurasian

    eurasian

    Joined:
    Oct 27, 2014
    Posts:
    3
    Another hack I'm using is debugging the unit test, just attach monodevelop to the unity editor then run the unit test in Unity, and i can look at all the objects that way. Would be nice to just see output though, without an explicit fail.
     
  6. mweldon

    mweldon

    Joined:
    Apr 19, 2010
    Posts:
    109
    Assert.AreEqual("Whatever",numHerb.ToString(), "OMG! Whatever is not equal to numHerb!" );

    You can add a third parameter that prints to the console if the Assert fails. I don't know if this is what you are looking for, but I find it pretty useful.
     
    Ash-Blue, AdmiralThrawn and Mikael-H like this.