Creating Custom NUnit Assertions
page 1 of 5
Published: 03 Aug 2007
Abstract
This article discusses how to create assertions in the NUnit framework. NUnit has added some assertion classes to its framework; this article will show you how to extend it to meet your testing needs.
by Brian Mains
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 32660/ 33

Introduction

NUnit is an excellent utility in supporting application development. Based on JUnit, NUnit tests .NET applications by making assertions about the code, allowing you to compare properties within your objects for certain values. For the unit test to succeed, all of the assertions must evaluate to true, or the test fails. There are complexities with unit tests; not every object has public properties, which makes it harder to test.  It is possible to do through the Reflection API objects which inspect the metadata and get the value of the property.

Some complexities with application code exist, such as classes marked with the friend/internal class modifier.  If the unit test exists outside of that project, it is not possible to test these types, except through the classes that use them, with one exception: adding the InternalsVisibleTo assembly attribute for the project that has the internal types.  InternalsVisibleTo references an assembly that permits its internal types to be visible to that class by designating the assembly information for that project. Though MSDN documentation states that the projects it references should be strongly-typed, I have read blog entries that use local assemblies and have done it in one of my projects as well.

I wrote an article about using a product called TypeMock, which is a powerful mocking library capable of performing complex unit tests.  All of these are features that are very helpful when creating unit tests.  And lastly, the newest version of NUnit contains additional assert objects for comparing collections, files, and strings, all of which are very useful and handy to have.

That got me curious, as I like the ability to have additional assertions than the standard set.  Additional assertion objects have the ability to add a powerful set of assertion features that can make some of the unit testing easier.  For instance, it would be nice to have assertions that reflected against an object, or assertions that poll a data table or row.  This article is going to go further into that, examining some of the essential objects, such as the various types of constraint objects that perform the matching of the objects.


View Entire Article

User Comments

Title: NUnit Test Generator   
Name: Thinzar
Date: 2012-07-02 1:55:11 AM
Comment:
I want to write NUnit Test generator tool. Pls tell me what features would be useful to add.
Title: NUnit Test Generator   
Name: Greg Finzer
Date: 2007-09-28 12:21:06 PM
Comment:
Brian, Thanks for the great article. I recently created a tool to create NUnit Tests. What other features would be useful to add?

http://www.kellermansoftware.com/p-30-nunit-test-generator.aspx

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 7:33:47 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search