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

The best solution I’ve found so far is this:

        [Test]
        public void TestCase ()
        {
            int numHerb = 10;

            //fails, and in the failure, I can see the numHerb value
            Assert.AreEqual("Whatever",numHerb.ToString());

           // outputs nothing, or anywhere I can see
            Debug.WriteLine("Herbivores start {0}", numHerb.ToString());
        }

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!

Debug.Log(“Text”);

Which namespace is that Debug class under?
My version of System.Diagnostics.Debug only has ‘WriteLine()’, and that’s not showing up anywhere.

Oh, ‘unit test’. I was referring to the UnityEngine namespace for regular stuff… my bad.

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.

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.

3 Likes

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…