Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Unit Testing in .NET : Page 3

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.


advertisement
The NUnit Framework
The example above shows the features that you can use to handle testing simple business objects, but it will not be sufficient to test business objects used in today's robust applications. The NUnit framework has additional Assert methods and Attributes that you can use for more advanced testing situations.

Assertions
There are two categories of Assert methods; Comparisons and Condition Tests. In addition, there is a method that a developer can call at any point to generate a failure based on custom logic. I'll get into this a little later.

Comparisons
Comparisons consist of the AreEqual and AreSame methods. As stated earlier, there are multiple overloads for the AreEqual methods. The overloads are fairly self-explanatory. Here they are:

//Compare Integer values Assert.AreEqual(int expected, int actual); Assert.AreEqual(int expected, int actual, string message); //Compare Decimal values Assert.AreEqual(decimal expected, decimal actual); Assert.AreEqual(decimal expected, decimal actual, string message); //Compare Float values Assert.AreEqual(float expected, float actual, float tolerance); Assert.AreEqual(float expected, float actual, float tolerance, string message); //Compare Double values Assert.AreEqual(double expected, double actual, double tolerance); Assert.AreEqual(double expected, double actual, double tolerance, string message); //Compare objects Assert.AreEqual(object expected, object actual); Assert.AreEqual(object expected, object actual, string message);

As you might have noticed, the overloads handle the most common value types. However, you probably also noticed that there is not an overload for strings. String values are handled in the overload that accepts object parameters. In fact, the object overloads allow you to test values of different types. For example, you can compare the integer 1 with the decimal 1.0 so that Assert.AreEqual(1.0, 1) passes and Assert.AreEqual(1.1, 1) fails. The next version of NUnit (currently in Beta) allows comparing arrays using the AreEqual method. Two arrays test equal if they are of the same length and each corresponding element is equal. This only applies to arrays, not to other collection types, such as the ArrayList.

Two arrays test equal if they are of the same length and each corresponding element is equal. This only applies to arrays, not to other collection types, such as the ArrayList.
The AreSame method tests whether the same object is referenced by both arguments. Here are the overloads for the AreSame method:

Assert.AreSame(object expected, object actual); Assert.AreSame(object expected, object actual, string message);

The following code shows the use of the AreSame method:



[Test] public void TestAreSame() { Account source = new Account(); Account dest = new Account(); // This evaluates to false because // source and dest are two different objects Assert.AreSame(source, dest, "First Test"); dest = source; // This evaluates to true because // source and dest are now the same object Assert.AreSame(source, dest, "Second Test"); }

Note that if your method code contains several asserts and one fails, the Test method stops executing at that point and no more assertions in that method are tested. Any remaining Test methods or TestFixture classes are executed.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

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