app.configs and MSTest Project - null reference for a connection string

后端 未结 3 1696
故里飘歌
故里飘歌 2021-01-12 03:11

When I try to run Unit Tests (mstest) I run into this issue.
The line of code:

_mainCnStr = System.Configuration.ConfigurationManager.
               C         


        
相关标签:
3条回答
  • 2021-01-12 03:26

    One thing to watch with MSTest (from the IDE at least); it doesn't run the tests in the regular output (bin) folder, and it doesn't respect the project's file inclusions ("Copy to Output Directory"). You often need to explicitly tell it (MSTest) which files to put into the test area. You will need to include the "app.config" in this list; either via the testrunconfig ("Deployment"), or by adding an attribute ([DeploymentItem]) to the affected test fixtures.

    0 讨论(0)
  • 2021-01-12 03:40

    You should add an app.config to the unit test project. It won't automatically use the settings in UI application's app.config.

    0 讨论(0)
  • 2021-01-12 03:51

    I'm assuming mstests are, like nunit tests, embedded in a seperate assembly which gets loaded by the testing application? In that case, you may need to create some test set-up code which loads in the configuration file.

    0 讨论(0)
提交回复
热议问题