问题
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!
回答1:
If you have your unit-test assembly named Company.Component.Tests.dll, then just make sure that Company.Component.Tests.dll.config is there with the proper connection string.
Additionally, it might be a good idea to decouple your connection provider class from the configuration, so that you will have flexibility in persistence (i.e.: switching from *.config to something else) and easier testing.
Also check out "How NUnit Finds Config Files"
回答2:
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.
回答3:
You can use the app.config for libraries (where I assume your tests are) and put them in there.
来源:https://stackoverflow.com/questions/152586/how-to-initialize-connectionstrings-collection-in-nunit