部署数据库测试方法 [英] Deployable database Test Methods

查看:177
本文介绍了部署数据库测试方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在创建使用SQL CE数据库应用程序的中间,我提出这个数据库部署,能够与应用程序,但我目前所遇到的问题是我需要运行TestMethods但这被示数出来的时候没有找到该数据库作为其看在testingProject文件夹下的调试或释放,因为这是它的数据目录

I am currently in the middle of creating an app that uses a sql CE database, I have made this database deploy-able with the application, however the problem I'm having at the moment is I need to run TestMethods but this is erroring out when it doesn't find the database as its looking in the "testingProject" folder under debug or release as that is it's Data Directory

using (SqlCeConnection sqlCon = new SqlCeConnection(@"Data Source=|DataDirectory|\database.sdf;Persist Security Info=False;"))

上面的代码是我的连接字符串,所以我猜这意味着,测试运行,并寻求在自己的数据目录的数据库

The code above is my connection string, so I'm guessing that means that the test is running and searching for a database in its own data directory

这是我可以在不改变数据库连接字符串,数据库的位置仍然留给我的应用程序部署做任何帮助吗?还是我问什么是不可能的?

Any help on what I could do without changing the database connection string, database location and still leaving my application deployable? or am I asking something impossible?

修改

[TestMethod]
        public void TestForReadingFromDB()
        {
            List<string> list = class.readDB();
            Assert.IsNotNull(list);
            Assert.AreNotEqual(0, list.Count);
        }



只是在测试方法,因此目前没有加入

just added in the test method that's currently failing

推荐答案

在测试项目中,你可以使用覆盖DataDirectory目录位置

In the test project you can override the DataDirectory location using

AppDomain.CurrentDomain.SetData("DataDirectory", <PATH_TO_DATA_DIRECTORY>);



例如在我的app.config文件中的测试项目,我有

For instance in my app.config file the testing projects I have

  <appSettings>
    <add key="DataDirectory" value="..\..\Database"/>
  </appSettings>

在我的测试夹具底座我有:

In my test fixture base I have:

var dataDirectory = ConfigurationManager.AppSettings["DataDirectory"];
var absoluteDataDirectory = Path.GetFullPath(dataDirectory);
AppDomain.CurrentDomain.SetData("DataDirectory", absoluteDataDirectory);

这台测试项目文件夹结构下DataDirectory目录到文件夹/数据库。

This sets the DataDirectory to the folder /Database under the test project folder structure.

在我滴或在那里创建数据库的副本,我可以轻松地运行集成测试。

Once I drop or create a copy of the database in there I can easily run Integration Tests.

这篇关于部署数据库测试方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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