如何使配置文件被复制到测试结果目录单元测试? [英] How to make a config file being copied into the test results directory while unit testing?

查看:222
本文介绍了如何使配置文件被复制到测试结果目录单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些单元测试,这取决于配置文件。这个 file.config 被部署到我的测试项目的 bin \Debug 目录中。但是,它似乎没有被复制到我的输出测试结果目录,测试实际发生。



我已经搜索并找到了这些:

TFS UnitTesting未在构建服务器上部署本地副本组合件以测试dir

测试项目和配置文件



第一个链接允许我了解如何将我的配置文件部署到我的测试项目的 bin \Debug 目录。



第二个提供了一个可行的解决方案,虽然我觉得对我的需要有点过分,添加自己一个类来测试等等。所以,我更喜欢一个更简单的方法,只需允许我把这个配置文件自动复制到我的测试结果目录。


编辑#1


我使用的是:


  1. Microsoft Enterprise Library 4.1及其命名连接;

  2. Microsoft Visual Studio 2008;和

  3. Microsoft UnitTest框架。

我的配置文件如下所示:

 < configuration> 
< configSections>
< section name =dataConfigurationtype =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 =TestsconnectionString =Database = Tests; Server =(local)\SQLEXPRESS; Integrated Security = SSPI
providerName =System.Data.SqlClient/&
< / connectionStrings>
< / config>

并命名为: Tests.config



现在,我的项目设置包含一个包含默认源名称的 DefaultSource 参数,为其创建连接和数据库对象。此设置的值为 Tests



因此,当我创建一个新连接时,

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

现在,这是单元测试时不能正常工作,因为 AppDomain.CurrentDomain.Basedirectory因为此属性不会返回程序集构建目录 bin \Debug ,而是返回 TestResults



因此,在我的测试中,我会执行以下操作:

p>

  [TestMethod()] 
public void Connection_InitializationWithSourceName(){
using connection as IConnection = ConnectionProviderFactory.CreateConnection DefaultSource){
//在此处断言...
}
}

其中 DefaultSource 属性将返回我的默认源设置参数,其值为 Tests 。因此, FileConfigurationSource 对象类将在测试结果目录中搜索一个名为 Tests.config 的文件



谢谢!



=)

解决方案

为什么不只是添加一个postbuild事件到你的项目, p>

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

解决方案

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

这篇关于如何使配置文件被复制到测试结果目录单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆