C # Unit Test: Writing to settings in unit test doesn't save values ​​in user.config

I am running C # unit test (VS 2008). As part of the test, I write settings that should lead to saving data in user.config.

Settings.Default.X = "History"; // X is string Settings.Default.Save(); 

But it just does not create the file (I cross-checked under "C: \ Documents and Settings \ HW \ Local Settings \ Application Data").

If I create the same material as the console application, there is no problem saving the data (the same code).

Is there anything special I need to do to do this in UnitTest?

+4
source share
3 answers

I tried this with Visual Studio 2010 on Windows 7, and the Visual Studio Unit Test environment actually created a temporary folder for test applications in which I found the user.config file with the correct settings. I think that it can be the same on VS 2008. The path to this folder looks like this:

Path to Windows 10:

C: \ Users \ $ USER $ \ AppData \ Local \ Microsoft_Corporation \ UnitTestAdapter__Running__StrongName_ {GUID} \ {number}

Windows Vista / Seven Ways:

C: \ Users \ $ USER $ \ AppData \ Local \ Microsoft_Corporation \ TestAppDomain {Number}

Path to Windows XP:

C: \ Documents and Settings \ $ USER $ \ Local Settings \ Microsoft_Corporation \ TestAppDomain {Number}

Good luck.

+5
source

Unit Test projects are just class libraries. There is no application context, and therefore, you may have problems because the settings object does not know that the company / application for the settings file is below.

I'm not sure, but he can just create it in his memory.

+1
source

Thanks for your help, it helped a lot to find the problem. The clue using the path helped me “see what is happening” and find the troublemaker.

By the way, this fragment

  config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal) config.FilePath 

It’s useful to find a storage location.

My problem was that I called Reset () before rebooting (). In fact, my test case checks that the objects are stored correctly, so it saves and reloads the settings. I did not know that Reset () “resets and saves to disk” - I assumed that it was only reset in memory. I should only call Reload ().

Since all test cases have their own directory, settings must be created (saved) in the test case.

0
source

Source: https://habr.com/ru/post/1307741/


All Articles