Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log errors, messages in a NUnit unit test?

There are several tabs in GUI runner of NUnit: alt text

I understand that using Console.WriteLine (...) shows the messages in "Console.Out" tab. My question is what other tabs are for and how can I log messages to them?

[EDIT]

I apologise as I realise that my original question wasn't clear enough. What I intend to do is that create an extensive result report once all unit test cases are executed. So I was exploring the way various messages that can be logged while a test is run. I am looking forward to creating a result report like:

==== TEST1 starts ====
Start Time: 2009-03-26 11:15:13 AM
Checking operation 1....OK
Checking value of variable "X": 52.56....OK
End Time: 2009-03-26 11:15:19 AM
Time taken to execute test: 0.00:00:06.000

==== TEST2 starts ====
.
.

Any suggestion how can I achieve this?

like image 759
Hemant Avatar asked Mar 26 '09 04:03

Hemant


2 Answers

Settings for the tabs are in the NUnit options - see the docs here.

For instance, if you check Display Console Error Output then that tab will display text written to Console.Error by your tests.

like image 68
Gary.Ray Avatar answered Oct 02 '22 03:10

Gary.Ray


Sample:

    Assert.AreNotEqual(0, result.Count);
     [exec]
     [exec] Tests run: 11, Failures: 1, Not run: 0, Time: 50.422 seconds
     [exec]
     [exec] Failures:
     [exec] 1) Domain.UnitTest.ManagerTest.TestEmbeddedIndex
 :   Expected: not 0
     [exec]   But was:  0
     [exec]

Is this what you are looking for?
Assert is very extensive. On fail of one of the conditions Nunit throws an error.

Assert.AreEqual(),Assert.AreSame(), Assert.Contains(),Assert.Fail();

Additionally Nunit has things like

[ExpectedException] 

Dig into the documentation to learn more..


Based on the Edit update:
If I were you I would do this as part of the build mechanism. In nant I’d do something like this…

<target name="setup" description="Initializes test properties">
    <echo message="Sucessfully initialized tests" />
</target>

Showing console messages after every [Test] is a bad idea IMHO. It delays the tests which counts when you run 1000’s of them. Tests are meant to be blazingly fast.

like image 28
Cherian Avatar answered Oct 02 '22 01:10

Cherian