Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a config file being copied into the test results directory while unit testing?

I have written some unit tests which depends on a configuration file. This file.config gets deployed into the bin\Debug directory of my test project. However, it doesn't seem to get copied into my output test results directory where the test actually takes place.

I have searched and found these:
TFS UnitTesting not deploying local copy assembly to test dir when on build server
Test project and config file

The first link allowed me to find out how to deploy my configuration file into my test project's bin\Debug directory.

The second presents a working solution, though I find it a little overkill for my needs, not to mention that I add myself a class to test, etc. So, I would prefer a simpler approach which could simply allow me to have this config file copied automatically into my test results directory.

EDIT #1

I'm using:

  1. Microsoft Enterprise Library 4.1 along with its named connections; with
  2. Microsoft Visual Studio 2008; and the
  3. Microsoft UnitTest Framework.

My config 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 is named: Tests.config.

Now, I have my project settings that contain a DefaultSource parameter which contains the default source name, that is, the for which to create connections and databases objects for. This setting's value is Tests.

So, when I create a new connection, I simply do it like so:

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

Now, it is this which doesn't work properly while unit testing, because of the AppDomain.CurrentDomain.Basedirectory value that is returned. Since this property will not return the assembly build directory bin\Debug, but rather the TestResults[auto-generated-test-results-directory] where the tests actually run.

So, when in my test I do:

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

where DefaultSource property will return my default source setting parameter which value is Tests. So, the FileConfigurationSource object class will search for a file called Tests.config in the test results directory where the tests actually run, as stated earlier.

Any idea of how to do it?

Thanks! =)

like image 471
Will Marcouiller Avatar asked Jan 21 '23 06:01

Will Marcouiller


1 Answers

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

like image 112
Doc Brown Avatar answered Jan 31 '23 02:01

Doc Brown