Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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,148
    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,148
    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.
  7. SorenBHansen

    SorenBHansen

    Joined:
    Apr 27, 2022
    Posts:
    1
    I succeded in getting printf in unity to work by making a multi device test.

    TEST_CASE_MULTIPLE_DEVICES("gpio multiple devices test example", "[driver]", gpio_master_test, gpio_slave_test);

    In the two functions printf will work:

    void gpio_master_test()
    {
    printf("gpio_master_test\n");
    }

    void gpio_slave_test()
    {
    printf("gpio_slave_test\n");
    }

    Be aware that after any TEST_ASSERT_ macro is called printf will not work any more..