4

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! =)

Community
  • 1
  • 1
Will Marcouiller
  • 23,773
  • 22
  • 96
  • 162

2 Answers2

5

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

Doc Brown
  • 19,739
  • 7
  • 52
  • 88
  • +1 This is a brilliant idea. And I would like to do so if only I could plan forward the test results directory name that is auto-generated per test run, that is, `C:\MyProject.Tests\TestResults\[auto-generated-test-result-directory]\Out`. This is the `[auto-generated]` part that confuses me and prevent to be able to copy post-build. – Will Marcouiller Mar 13 '11 at 16:06
  • You have at least 2 options: either work out how this `[auto-generated]` thing works and rebuild the logic by yourself. Or you copy the config file in any subdirectory having a path like `C:\MyProject.Tests\TestResults\*\Out`. That means, you have to write a small program or script for the copying task (I would typically choose VBScript for those kind of tasks, but you can choose any tool you like for that). – Doc Brown Mar 14 '11 at 06:53
4

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

It can be used like this:

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

Or you can put it on the class, if you need it for all tests in that class.

Evgeniy Berezovsky
  • 18,571
  • 13
  • 82
  • 156
Sergey Barskiy
  • 1,761
  • 2
  • 15
  • 17
  • I shall try this option when I test again so that I can see the behaviour and its result. Thanks for your reply (+1)! =) – Will Marcouiller Jun 16 '12 at 11:46
  • This works great and as it's so easy to set up, the *postbuild event* cannot possibly be easier. I haven't tried postbuild event, but unless you are a build expert, you'll probably have to figure out a few things first like variables etc. – Evgeniy Berezovsky Jan 15 '15 at 04:24