可部署的数据库测试方法

我目前正在创建一个使用sql CE数据库的应用程序,我已经使该数据库可以与应用程序一起部署,但是我现在遇到的问题是我需要运行TestMethods但这是错误的当它没有找到数据库,因为它在调试或发布下的“testingProject”文件夹中查找,因为它是数据目录

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

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

在不更改数据库连接字符串,数据库位置以及仍然可以部署应用程序的情况下,我可以做些什么的帮助? 还是我问一些不可能的事情?

编辑

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

刚刚添加到当前失败的测试方法中

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

 AppDomain.CurrentDomain.SetData("DataDirectory", ); 

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

     

在我的测试夹具基础中,我有:

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

这将DataDirectory设置为测试项目文件夹结构下的文件夹/ Database。

一旦我删除或创建数据库的副本,我就可以轻松运行集成测试。

这是我在初始化数据类中指定测试的数据目录路径的方法

 public class TestClasse { public TestClass() { GetAppDataDirectoryForTesting(); } private static string GetAppDataDirectoryForTesting() { //NOTE: must be using visual studio test tools for this to work string path = AppDomain.CurrentDomain.BaseDirectory; var dirs = path.Split(Path.DirectorySeparatorChar); var appDataPath = ""; for (int i = 0; i < dirs.Length - 3; i++) { appDataPath += dirs[i] + Path.DirectorySeparatorChar.ToString(); } appDataPath = appDataPath + "[foldername(ie in my case project name)]" + Path.DirectorySeparatorChar.ToString() + "App_Data"; return appDataPath; } [TestMethod] public void SomeTestMethod() { ....test code } }