UNIT-TESTING in .NET and a small but interesting issue with Nunit and EnterpriseLibrary

Posted: November 23, 2006 in Uncategorized
Last week i found an issue which was quite interesting with Nunit and the EnterpriseLibrary.The issue was my dev team reports that they are not being able to setup Nunit with enterpriseLibrary to successfully test the BLL.Although i can sidelined the issue being an architect i could have ask them its a dev issue but i couldn’t & I Can’t .Upon looking at the process i found that they are getting an issue where by the configuration is not recognised by enterpriselibrary when called from a unittest routine.
In this short post i ll try to cover the unittest approaches,Process and few principles you might follow or you are planning to follow in future:-I ll try to keep it short and simple .
Before delving further let me first provide the process and steps adopted so that if you are planning to go for unittesting your BLL which you must often found very useful would be a little easier:-
For unittesting you  can follow different approaches but TDD is  the most common that is the basic idea behind the process is to desgin your TestCases  before writing Code .Often i found it useful to add a Unittest Layer and a different Class Library based TestProject which help you test the necesary methods.However the critical issue came here is how do i test the UI and often people recommend the use of MVC pattern so that you can test  your logics [good one most of the time but not always although i strongly believe Martin Fowlers principles of keeping the presentation tiers free of almost any code] since you have a separation between your view and presentation tiers and since your logic is seprated from the presentation tiers you can now unittest your logics which  you always should test afterall that is the part where the core functionality lies and is most vulenerable to defects and changes.I am not again delving into the details of testing UI layer that i can cover someothertime or you can see many post on the same in web.
So we design  our DAL,BLL,UI or presentation Layers,BE[back end] as well as we have a TestLayer[Often a class Library project ] which ll contain the testcases and methods which will reference the other layer so that we can test method/function in those layers.We have written our Test methods or Testcases which we need to test and the necessary Test  and TestFixture attributes[it does not matter as this ll be change with tools] and we ll fire some unittest tools and run our tests.we might display success,failure info as well as assertions.
Here my dev team had also similar approach ,they have the MVC but we are not supposed to test the UI[we followed martin fowlers principle here:)]but the BLL and DAL calls primitively.The BLL and DAL were using the EnterPriseLibrary for dataaccess,Logging and ExceptionHandling purposes.In the Unit Test Project [class library] one they have the testcases or testmethods which is calling the BLL and DAL and EntLib methods.Now all these setup-ed and configured they fired the Nunit[VsUnit or any other you might ve ] and they load the test assembly and firedup the unittest cases[run the unit tests].
 Here comes the gotcha while testing the results the methods which were not reference to the Enterpriselibrary were sucess but all methods which used the enterpriselibrary failed with exceptions like "unrecognised configuration section dataconfiguration".if you ve used nunit with similar purposes you might alreday pointed it out that where the config created or copied for enterpriselibrary configuration to the UnitTEST project’ bin folder.Yes you need to create a copy of the same configuration in your UNittest project for the enterpriselibrary and even the documentaion of nunit says the same because at runtime the Enterpriselibrary will lookfor the config in ur app and when it not found one it ll through the exception config not found .you can drop one in the BIN folder of the unittest project but for us we already have the config created but it was still failing with unrecognised configuration Section .I crossed chcek the same creating a demo app and it works fine with theabove appoach but there was still the same error.I cross check the Xml configuration to findout if any erorr was there due to CASE
sensitiveness in xml but no the problem was somewhere else.The configuration file added to the test project was not at all recognised by enterpriselibrary and hence the problem.The moment i opened up the configuration file in NeterpriseLibrary configuration tool and defined the necessary configuration for DataAccess,ExceptionHandling and Logging error gone.
So if you have similar problem you know what to do?if you rae planning for unittesting and is not aware of much processed and hope this clarifies few of your doubts and provides an oerview of concepts from where you cab start looking into Unittesting.I am providing few more links to unittest frameworks and posts which helps you further:-

NUnit http://www.nunit.org/

MbUnit vs. NUnit Vs. Team System Unit Testing – Choosing a unit test framework

series of posts from Roy on unittesting :-http://weblogs.asp.net/rosherove/archive/tags/Unit+Testing/default.aspx

Refactoring NUnit Unit Tests to VSUnit

unitetsting in  .NET by justin

Testing with NMOCK in msdnmag

TestDriven.Net-  http://testdriven.net/

VSTS unitesting walkThrough http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/vstsunittesting.asp

NunitASP http://nunitasp.sourceforge.net/

HowtoCreate Asp.Net Unittest http://msdn2.microsoft.com/en-us/ms182526.aspx

intro article in informit http://www.informit.com/articles/article.asp?p=30034

How to: Author a Unit Test  :- http://msdn2.microsoft.com/en-us/library/ms182525(VS.80).aspx
Simplify Data Layer Unit Testing using Enterprise Services :- http://msdn.microsoft.com/msdnmag/issues/05/06/UnitTesting/default.aspx
Strengthening Visual Studio Unit Tests :- http://msdn.microsoft.com/msdnmag/issues/06/03/Bugslayer/default.aspx
Overview of Data-Driven Unit Tests  :- http://msdn2.microsoft.com/en-us/library/ms182519(VS.80).aspx
unittesting framework http://msdn2.microsoft.com/en-us/library/ms243147(VS.80).aspx

There are lot  more and i ll provide more info if anyone interested.However this ll hopefully give a start.I ll update this post soon for more update into this arena and soon post few more into the process of unitest. 


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s