Nunit and unit testing of ASP.NET application [modified]
-
Hi all, Need your suggestions How should i approach testing functions in a ASP.NET application. Its not integration testing but a unit testing(functions) But the problem is It is nearly impossible to isolate a function due to dependencies Many funcition are expecting other objects like Page Class context, Configuration settings many more complex objects like session context... etc now how can i test this methods using Nunit framework how should i initialized these objects ? should i use mock object concept ? is Nunit framework appropriate tool for writing the test cases ? Please suggest
Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... " Check My Blog
modified on Tuesday, August 4, 2009 3:55 PM
-
Hi all, Need your suggestions How should i approach testing functions in a ASP.NET application. Its not integration testing but a unit testing(functions) But the problem is It is nearly impossible to isolate a function due to dependencies Many funcition are expecting other objects like Page Class context, Configuration settings many more complex objects like session context... etc now how can i test this methods using Nunit framework how should i initialized these objects ? should i use mock object concept ? is Nunit framework appropriate tool for writing the test cases ? Please suggest
Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... " Check My Blog
modified on Tuesday, August 4, 2009 3:55 PM
If you write good code, then your business tasks will be isolated from UI, and can be tested.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Hi all, Need your suggestions How should i approach testing functions in a ASP.NET application. Its not integration testing but a unit testing(functions) But the problem is It is nearly impossible to isolate a function due to dependencies Many funcition are expecting other objects like Page Class context, Configuration settings many more complex objects like session context... etc now how can i test this methods using Nunit framework how should i initialized these objects ? should i use mock object concept ? is Nunit framework appropriate tool for writing the test cases ? Please suggest
Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... " Check My Blog
modified on Tuesday, August 4, 2009 3:55 PM
Sandeep Akhare wrote:
now how can i test this methods using Nunit framework
You need to isolate the dependencies. Separate the code into several projects and have a single project for all your ASPX files. Now you can write tests for the other projects. Other projects should not get a reference to
System.Web
.Sandeep Akhare wrote:
how should i initialized these objects ? should i use mock object concept ?
Probably yes. You need to create an interface which wraps
HttpContext
. Inject this interface as a dependency to your business classes. Read more about dependency injection[^]. In your test cases, you can mock this interface and set expectations. I suggest Rhino Mocks[^] for mocking. It is a wise idea to program to interfaces than implementations.Sandeep Akhare wrote:
is Nunit framework appropriate tool for writing the test cases ?
Yes. Its a great piece of work. :)
Navaneeth How to use google | Ask smart questions
-
If you write good code, then your business tasks will be isolated from UI, and can be tested.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
Thanks Chris for your reply Thats the problem UI layer is having business rules implemented making quite hard to test the application. We are going to rewrite the application using MVC architecture but before to that we need to write Nunit test cases for existing application.
Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... " Check My Blog
-
Sandeep Akhare wrote:
now how can i test this methods using Nunit framework
You need to isolate the dependencies. Separate the code into several projects and have a single project for all your ASPX files. Now you can write tests for the other projects. Other projects should not get a reference to
System.Web
.Sandeep Akhare wrote:
how should i initialized these objects ? should i use mock object concept ?
Probably yes. You need to create an interface which wraps
HttpContext
. Inject this interface as a dependency to your business classes. Read more about dependency injection[^]. In your test cases, you can mock this interface and set expectations. I suggest Rhino Mocks[^] for mocking. It is a wise idea to program to interfaces than implementations.Sandeep Akhare wrote:
is Nunit framework appropriate tool for writing the test cases ?
Yes. Its a great piece of work. :)
Navaneeth How to use google | Ask smart questions
Hey Navaneeth Congrates for becoming MVP!
N a v a n e e t h wrote:
You need to isolate the dependencies. Separate the code into several projects and have a single project for all your ASPX files. Now you can write tests for the other projects. Other projects should not get a reference to System.Web.
You mean to seperate the business layer from UI only for Testing purpose ? for other suggestions i need to look in to them. Thanks for reply
Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... " Check My Blog
-
Hey Navaneeth Congrates for becoming MVP!
N a v a n e e t h wrote:
You need to isolate the dependencies. Separate the code into several projects and have a single project for all your ASPX files. Now you can write tests for the other projects. Other projects should not get a reference to System.Web.
You mean to seperate the business layer from UI only for Testing purpose ? for other suggestions i need to look in to them. Thanks for reply
Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... " Check My Blog
Thanks :)
Sandeep Akhare wrote:
You mean to seperate the business layer from UI only for Testing purpose ?
Separating business logic from UI is good not only for testing but also for improved maintainability. If you have business logic tied close to UI, it will be hard to write automated test cases. So the first step in your case would be to re-factor code to support automated testing.
Navaneeth How to use google | Ask smart questions
-
Thanks :)
Sandeep Akhare wrote:
You mean to seperate the business layer from UI only for Testing purpose ?
Separating business logic from UI is good not only for testing but also for improved maintainability. If you have business logic tied close to UI, it will be hard to write automated test cases. So the first step in your case would be to re-factor code to support automated testing.
Navaneeth How to use google | Ask smart questions
Hi Navneeth, We are planning to re-factor the application for next release. But for current application i have to write the test cases for such complex application. I read the links you have mentioned but still i am not able to conclude anything 1. Wraping of httpContext :- does it mean creating the app domain for your application ? 2. inserting the dependency object still i am not sure how i will insert it ? Problem is Page is derived from one base class which is derived from another base class nad interfaces There are many properties and objects some of these properties are readonly like sessionContext
Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... " Check My Blog