Mohammed Thabet
Mohammed Thabet

Reputation: 21677

Unit Testing show output

I searched for unit testing tool and I found the suitable one is NUnit and I think it good but my problem that this tool only show test method result only (pass or fail) and I need to show not only pass or fail also the output .How can I show the output using NUnit or if there another unit testing tool its also good ?If its not supported please suggest me how can I solve it.

All ideas are welcomed

Upvotes: 13

Views: 16619

Answers (4)

Kershaw
Kershaw

Reputation: 1254

This post is loooooong after the question was asked, but wanted to chime in. Yes, you can accomplish a lot in unit/integration tests and probably do most of what you need. So, I agree, do as much as you can in your test methods.

But sometimes, providing some output is useful. Especially if you need to further verify the results and that verification cannot be accomplished via your unit test. Think of an external system that your dev/test environment has no or limited access to.

As an example, let's say you are hitting a webapi to CREATE a claim and the response is the new claim number. But the api does not expose methods to GET a claim, and you need to verify some other data that was created when you made the webapi call. In this case, you could use the outputted claim numbers to manually check the remote system.

FWIW

Upvotes: 0

sll
sll

Reputation: 62544

It depends where are you want to output the data from a test. I believe you mentioned something another from File, Log, Console, Debug output. As an alternative NUnit allow to output any message in the regular tests output stream, just use following utility methods:

For successful test

Assert.Pass( string message, object[] parms );

For failed test

Assert.Fail( string message, object[] parms );

More details see here

Upvotes: 0

bryanbcook
bryanbcook

Reputation: 18303

Piping the output of System.Console will work for NUnit, but it's not your best option.

For passing tests, you shouldn't need to review the console output to verify that the tests have passed. If you are, you're doing it wrong. Tests should be automated and repeatable without human intervention. Verifying by hand doesn't scale and creates false positives.

On the other hand, having console output for failing tests is helpful, but it will only provide information that could otherwise be inferred from attaching a debugger. That's a lot of extra effort to add console logging to your application for little benefit.

Instead, make sure that your error messages are meaningful. When writing your tests make sure your assertions are explicit. Always try to use the assertion that closely fits the object you are asserting and provide a failure message that explains why the test is important.

For example:

// very bad
Assert.IsTrue( collection.Count == 23 );

The above assertion doesn't really provide much help when the test fails. As NUnit formats the output of the assertions, this assertion won't help you as it will state something like "expecting <True> but was <False>".

A more appropriate assert will provide more meaningful test failures.

// much better
Assert.AreEqual(23, collection.Count, 
               "There should be a minimum of 23 items by default.");

This provides a much more meaningful failure message: "Expecting <23> but was <0>: There should be a minimum of 23 items by default."

Upvotes: 5

Jesus Ramos
Jesus Ramos

Reputation: 23266

On the bottom bar of NUnit you can click Text Output and that shows all debug and console output.

Upvotes: 4

Related Questions