Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize ConnectionStrings collection in NUnit

I want to test ASP.NET application using NUnit, but it seems WebConfigurationManager.ConnectionStrings collection is empty when running from NUnit GUI.

Could you tell me how to initialize this collection (probably in [SetUp] function of [TestFixture])? Should I copy Web.config somethere?

Thank you!

like image 275
Alexander Prokofyev Avatar asked Sep 30 '08 11:09

Alexander Prokofyev


1 Answers

NUnit .config file location depends on how you created the NUnit project file

Where .config files for NUnit tests are located is a little bit more complicated than other posts here suggest. There are settings for this in the NUnit GUI Project/Edit dialogue. The default values all depend upon how you created your NUnit project file.

When you open the NUnit GUI and select File/Open and then select a .dll file a new project gets set up with settings to look for a config file with the same name as the dll in the same directory. So if you loaded \bin\debug\MyTests.dll NUnit looks for \bin\Debug\MyTests.dll.config by default. The only trouble with this is that when you create a release build you need to create a separate NUnit project.

If you have created the NUnit project by selecting File/NewProject then the default setting is to look for a config file with the same name as the NUnit project. So if you created \MyNUnitProject.nunit NUnit looks for \MyNUnitProject.config by default.

The chances are you have used Visual Studio to create an \App.config file and stuck it in the source folder for your test dll. When you buld your test project this gets copied to \bin\Debug\MyTests.dll.config or \bin\Release\MyTests.dll.config depending upon the configuration you have selected. If you have opened the MyTest.dll directly in NUnit this will work fine, however if you have created a new NUnit project you’re in trouble as it will not look for these files by default.

To resolve the issue you need to open up the Project/Edit dialog in the NUnit GUI and check that you have two Configurations Debug & Release to match your .Net project. Once you have done this you can select the Debug Configuration and set the ApplicationBase to bin\Debug\ and set the Configuration File Name to MyTests.dll.config. Do the same for the Release configuration and away you go.

like image 97
Martin Brown Avatar answered Sep 23 '22 17:09

Martin Brown