NUnit Issues...

Everyone talks about Test driven development and Unit testing these days. And theoretically it isn’t quite bad actually. I have been reading about it since ages and the last time I finished my project, I had some time to delve over it and thought of actually implementing it in practice.

 

That’s when I got this opportunity to put this into practice. There was this on going internal project which was ongoing for quite some time and little bits and pieces were left out. I was told by a smart colleague of mine to work on it if I have time.

 

Well the 2 most important problems in working with NUnit is:

  1. You can’t access a configuration file to get information. If you are, since the configuration file is not loaded in memory during the running of the test cases, the information is lost and there is an exception. The classic example, accessing the database by picking up the connection string from web.config. The web.config is not loaded hence cannot be accessed. This ends up in failure to create the connection object and a NullReferenceException. That test fails and all related tests fail. Even though the tests are otherwise not supposed to fail.
  2. You can’t test the code on the UI layer. Event handling code is not supported by NUnit because when the tests run, the page is not loaded in memory.

 

I did some research on how to get around the problem. They say there is this NUnitAsp that can help you solve both the problems. It is an NUnit Add-in and lets you load the form and generate UI related tests. The only problem with it is that it access the IIS anonymously.

 

Now if you are using Windows authentication in your web application,  you have to disallow anonymous access to the web application in a case like this you cannot use NUnitAsp because you will always get a 401 error.

 

Usually all the web applications developed at my work place use windows authenticated security. The company I work for work in the finance domain and security is an important issue. So the conclusion is NUnitAsp is out.

 

I think the NUnitAsp needs a little change. It should allow windows authentication. Should be able to pick up the current user login credentials and log in to the windows authenticated web application and run the test cases.

Published Friday, February 17, 2006 6:17 PM by Nilotpal Das

Comments

Friday, February 17, 2006 6:36 PM by BigJim in STL

# re: NUnit Issues...

Hey, I have something which might help regarding the NUnitASP and security. In the NUnitASP test case class, you can impersonate yourself (i.e. run NUNitASP and have the pages react as if it was your windows login):

' this sets up the authentication to be the same as the
' current windows user,
' rather than an empty / un-logged-on session:
browser.Credentials = System.Net.CredentialCache.DefaultCredentials

I think you can also use this to impersonate another user, but I'm not 100% sure of the syntax for that.

So when I run my NUnitASP tests using this, the "screens" that it accesses think it's my login, not an anonymous one.

Hope this helps, NUnitASP is a pretty nice tool to use for regression testing.

Jim G
St. Louis
jgilbert (at) chartercom.com
Friday, February 17, 2006 7:10 PM by AndrewSeven

# re: NUnit Issues...

If you search a bit, you should find the how-to for config files, there is a setting in nUnit.

I doon't remember the details of using the credentials with nUnit Asp, but I've used it and it works.
Friday, February 17, 2006 7:15 PM by Steve Hurcombe

# re: NUnit Issues...

This is a useful tool for doing UI testing.

http://www.codeproject.com/csharp/automatinginternetexplore.asp

It drives IE through it's object model from the unit test. Hence you are using the application through the browser and IIS just as a normal user would.

Best regards
Steve
Friday, February 17, 2006 7:32 PM by Duncan

# re: NUnit Issues...

Not strictly TTD, but I get around the problems of configuration files etc, by removing as much code as possible to their own classes, away from the page code behind, to the point sometimes of removing to its own dll, these classes can be either static or not as you decide, this allows you to use NUnit, or TestDriven to test these classes and functions, you simply pass in the values you would normally get from the configuration file, text boxes etc.

Testing the page is then a case of simply running it, any errors in functionality are at the interface between your class and the web page, usually a case of


protected void btnDoSomething_Click(object sender, ImageClickEventArgs e)
{
DoSomethingClass.DoSomething(parm1,….)
}

Because you know that the DoSomethingClass works (youv’e unit tested it) the problem is what is going in!

This also creates a small disconnect between the interface and the logic which I think is always a good thing.

Remember there is nothing wrong with passing in an HTML control into the class, somethimes its handy to pass a list box etc, and use it like a code collection. When unit testing this sort of thing you simply create the control on the fly, fill it with values and pass it in, just as the page will.

If you are planning on using Visual Studio have a good look at TestDriven, as this includes code coverage, as well, it says beta but its very stable and well supported.

http://www.testdriven.net/
Friday, February 17, 2006 9:03 PM by Jeff

# re: NUnit Issues...

You can test with config files. Name it after the same thing as your .nunit file with a .config on the end. So project1.nunit becomes project1.config. You might have to adjust it in the GUI. I know it works because I've tested programmatically changing the config files from within unit tests.
Sunday, February 19, 2006 1:12 AM by Justin

# re: NUnit Issues...

It is very very easy to have a config file for your test. Nunit already had this functionality built into it and have for at least the last 2 years since I started using it.

Here is what you need to do:

If your compiled test dll is called MyProjectTest.dll, if you make a copy of your web.config and name the copy MyProjectTest.dll.config, nunit will automatically load this configuration file the first time you open the test dll in nunit gui or console. I have this process as part of my nant build scripts.

You can also just create a MyProjectTest.dll.config file instead of copying the web.config but I found that a pain to keep the two in sync.

Friday, May 02, 2008 11:17 AM by Andrew

# re: NUnit Issues...

For those who were looking for the same thing as me, here's how I added a specific domain account to nUnitASP.  This is using NUint 2.4.7 and nUnit 2.0:

       protected override void SetUp()

       {

           base.SetUp();

           NetworkCredential nc = new NetworkCredential("USERNAME", "PASSWORD", "DOMAIN");

           Browser.Credentials = nc;

       }

Leave a Comment

(required) 
(required) 
(optional)
(required)