How to make a configuration file copied to the test results directory during unit testing?

I wrote several unit tests that depend on the configuration file. This file.config deployed to the bin\Debug directory of my test project. However, it does not seem to have been copied to my catalog of test results, where the test really takes place.

I searched and found them:
TFS UnitTesting does not deploy a local copy of the assembly to test dir on the assembly server
Check project and configuration file

The first link let me know how to deploy my configuration file in my bin\Debug test project.

The second one is a working solution, although I find it a bit crowded for my needs, not to mention the fact that I am adding myself as a class for testing, etc. Therefore, I would prefer a simpler approach, which could just let me have this configuration file automatically copied to the test results directory.

EDIT NO. 1

I use:

  • Microsoft Enterprise Library 4.1 along with named connections with
  • Microsoft Visual Studio 2008; and
  • Microsoft UnitTest Framework.

My configuration file looks like this:

 <configuration> <configSections> <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </configSections> <dataConfiguration defaultDatabase="Tests" /> <connectionStrings> <add name="Tests" connectionString="Database=Tests;Server=(local)\SQLEXPRESS;Integrated Security=SSPI" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration> 

and called: Tests.config .

Now I have project parameters that contain the DefaultSource parameter, which contains the default source name, that is, to create connection objects and databases. This is the Tests setting value.

So, when I create a new connection, I just do it like this:

 public static IDbConnection CreateConnection(string source) { return new DatabaseProviderFactory(new FileConfigurationSource( string.Format("{0}\{1}.config", AppDomain.CurrentDomain.BaseDirectory, source) ).CreateDefault().CreateConnection(); } 

Now this does not work properly during unit testing due to the return value of AppDomain.CurrentDomain.Basedirectory . Since this property will not return the bin\Debug assembly assembly bin\Debug , but rather TestResults[auto-generated-test-results-directory] , where the tests are actually executed.

So, when in my test I do:

 [TestMethod()] public void Connection_InitializationWithSourceName() { using connection as IConnection = ConnectionProviderFactory.CreateConnection(DefaultSource) { // Asserts here... } } 

where the DefaultSource property will return my original source parameter whose value is Tests . Thus, the FileConfigurationSource object FileConfigurationSource will search for a file called Tests.config in the test results directory, where the tests are actually executed, as indicated earlier.

Any idea how to do this?

Thanks! =)

+4
source share
2 answers

Why don't you just add the postbuild event to a project that copies the file anywhere?

+5
source

You can add the [DeploymentItem] attribute to all the tests that need it, and this will allow you to deploy all the files you need into a folder. http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.deploymentitemattribute(v=vs.80).aspx

It can be used as follows:

 [DeploymentItem("resources/my-file.ini")] [TestMethod()] public void Connection_InitializationWithSourceName() { 

Or you can put it in a class if you need it for all the tests in this class.

+4
source

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


All Articles