Thu 8 Dec 2005
NUnit and the app.config
Posted by david under Uncategorized , .NET(C#) , agile software development , test driven developmentI have begun a new winforms project where I’m using a three-tiered architecture including an Object Relational Mapper (NHibernate), an automated testing suite (NUnit) and a light, easy-to-distribute database (SQLite).
This has not been without problems, but the issue I’m going to post about here is rather small, although it took me a while to figure out.
The thing is that NHibernate uses the app.config file for the database connection settings. My solution is divided into 4 projects, one of them being the unit-testing project, and when I executed my solution normally and ran a small non-nunit test I had no problem connecting to the database through NHibernate, but when I ran the tests through the NUnit-gui program, the app.config was not loaded and therefore the database connection test failed.
Normally this should be solved by placing the config file in the same directory as the dll that is being tested and calling it the same, i.e. “Tester.dll.config”, but in my case this just didn’t do it. Until I found some advice telling me to name the config file the same as the nunit-project and placing it in that same directory. So I had a Tester.unit file and a Tester.config file. That did it, but I was wondering why so many people wrote about the other way of doing it. And finally I found out why:
The problem is that there are two ways of loading a dll for testing in the NUnit-gui program. One is to create a new project and then choose the dll’s that should be tested. That was what I did. But the other, and simpler, is just to load the dll directly without creating a project in NUnit-gui first. In that last case you have to name the config file the same as the dll as I wrote above and place it in the same directory.
Actually it was a pretty simple solution, but I find it quite non-intuitive to have two ways of doing the same thing, but with two different default sideeffects of working with the config file.
October 11th, 2006 at 8:02 pm
[…] My blog has moved. You can read the post here: NUnit and the app.config […]
January 25th, 2007 at 5:09 am
Thanks for the advice. I am using the second method so I had to use the tester.dll.config way. It worked.
August 24th, 2007 at 6:29 pm
Thank you. Spent a couple of hours trying to figure out why the app.config file wasn’t being read. Renamed it to the same as the .nunit file and good to go.
October 15th, 2007 at 6:57 am
Cheers! Was wondering what the hell was going on..
October 24th, 2007 at 2:34 pm
Good. I was getting stuck with using NHibernate and NUnit together. I’ll try the methods described and see how it goes.
Thx!
October 24th, 2007 at 3:08 pm
You’re welcome, I hope you get it to work.
October 30th, 2007 at 2:53 pm
I am only able to get it to work if the unit tests and NHibernate mapping files, etc. are in the same project. I put a config file with the name of my test project and the name of the project I am testing but neither way works. I’ve tried telling the nhibernate.config file to look at either the project to be tested or the test project itself for assebly info but neither way works.
Any advice?
October 5th, 2008 at 11:38 am
Just been stung by this one … 3 hour head scratch and finally realised that it was app.config problem.
D’oh!
A handy resource from the past, thanks.
February 10th, 2009 at 1:01 pm
You can set the App.config file in the nunit project. (Project/Edit)
Then you can save the config to a .nunit file like the following.
March 28th, 2009 at 12:19 am
If you run NUnit against a Visual Studio project or solution file, it still ignores the the config file even when properly named/placed. That is because NUnit defaults to using a single AppDomain. Running NUnit with /domain:Multiple should solve the problem.
May 19th, 2009 at 8:34 pm
Damn, tried to use my config file to test one specific assemby since yesterday and … I found your blog
Thank you ! really !