Browse DevX
Sign up for e-mail newsletters from DevX


Unit Testing in .NET : Page 4

You have been given the task of creating some business objects for a new .NET project. The UI has not been created (or designed) yet, so you start coding right away. After creating the first few objects, you decide that maybe you should do some unit testing. This article shows you how.

Condition Tests
The Assert Condition test methods test for a specific condition. Each method accepts a parameter (of the appropriate type) and an optional message. It is advisable to provide a message when using these methods because no default message is displayed in the GUI if the test fails. Also, when one of these assertions fails, the test method aborts and any code in the same method following the failed assertion is not executed. Any remaining Test methods or TestFixture classes are executed.

Here are the Condition Test assertion methods:

   // Tests for a true boolean expression 
   Assert.IsTrue(bool condition);
   Assert.IsTrue(bool condition, string message);
   // Tests for a false boolean expression
   Assert.IsFalse(bool condition);
   Assert.IsFalse(bool condition, string message);
   // Tests the object to see if it is null
   Assert.IsNull(object anObject);
   Assert.IsNull(object anObject, string message);
   // Tests the object to see if it is not null
   Assert.IsNotNull(object anObject);
   Assert.IsNotNull(object anObject, string message);
If the above assertion tests are not sufficient, the Assert.Fail method provides the ability to generate a failure based on your own logic. At any time in a test method, you can call the Assert.Fail() method to cause the test method to fail. Again, it is recommended that you pass a message to the Fail method to be displayed in the GUI. The Assert.Fail method call causes the Test method to abort and any remaining code in that method will not execute. Any remaining Test methods or TestFixture classes are executed.

Thanks for your registration, follow us on our social networks to keep up-to-date