Google Android Development Agency SASS

Wednesday 9 December 2009

NUnit and App.config

Here's the problem: You have a project which you want to unit test with NUnit but the project relies on some appSettings or other config data normally loaded from app.config or web.config. You know the sort of stuff, connection strings, usernames etc.

When you run your code in NUnit you get a NullReferenceException when you try to access your config, it's like it's being ignored.

I come across this problem quite often but I always have to refresh my memory of how I fix it. Well there's two ways, but only one I can get to work reliably:

1) When you create your nunit project, rather than clicking Project-> Add Assembly, just drag your DLL from Windows Explorer onto NUnit. After doing this it'll pick up the assembly.dll.config file for your assembly. I couldn't get to work reliably though, it would work once and then fail.

2) You need to have a config file named the same as your nunit project and located alongside. So in my case I have com.sass.ecommerce.tests.nunit and alongside it my assembly (com.sass.ecommerce.tests.dll) and my renamed config file, com.sass.ecommerce.tests.config.

Doing it the second way gave me more reliable results and I could access my config file no problem from my unit tests.

No comments:

Post a Comment