简体   繁体   中英

Unit-tests can't read from config files

We have a setup where we use Visual Studio 2012 to run our nunit-based unit tests from our local dev environments.

However, we have observed the following pattern:

  1. When running unit tests the first time after Visual Studio has been launched, the tests fail when reading from config-files.

  2. When rerunning the failing unit tests they can read the values from config files just fine.

This is truly a little bit annoying so I was hoping for a bit of help here :-).

I looked at Problem in reading connection string from App.Config when using NUnit 2.5.2 , but since we run our unit tests directly in Visual Studio, there is no way for us to configure NUnit directly.

NUnit looks for the configuration file in the wrong position, and does not ifnd the config file. Look at

Assembly.GetExecutingAssembly().CodeBase 

and

ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = configFile }, ConfigurationUserLevel.None); 

to get the correct location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM