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.

5 Comments

  • 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

  • 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.

  • 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.

  • 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.



  • 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;
    }

Comments have been disabled for this content.