我们得到了一些使用SQL Server数据库的单元测试。为了使至少每个测试夹具独特并且独立于其他测试夹具,我尝试在每个测试夹具开始之前恢复数据库。每个测试都会打开并关闭其例程中的连接和数据库。在单元测试之间恢复数据库:数据库仍在使用中
要在第一个测试夹具工作得很好之前恢复数据库,但在第二个测试夹具之前(连接和数据库已打开并关闭后)恢复数据库则不会。
我把这个问题包括进去了。下面是这两个样品测试(NewUnitTest1将首先执行):
using NUnit.Framework;
using System.Data;
using System.Data.SqlClient;
[TestFixture]
class UnitTest1 : BaseTest
{
[Test]
public void NewUnitTest1()
{
string conString = ConnectionStringHelper.GetConnectionString(SCADADatabases.ConfigurationDatabase); // Helper method to optain connection string, is correct
using (SqlConnection dbConn = new SqlConnection(conString))
{
SqlCommand cmd = dbConn.CreateCommand();
cmd.CommandText = "SELECT * FROM TB_PV";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
} // Dispose should close connection and database ...
Assert.IsTrue(true);
}
}
[TestFixture]
class UnitTest2 : BaseTest
{
[Test]
public void NewUnitTest2()
{
Assert.IsTrue(true);
}
}
的基础测试类将在每次测试夹具前做恢复作业:
using My.Core.Helper;
using My.Core.UnitTest.Properties;
using My.DBRestore.Core;
using My.Domain;
using Microsoft.SqlServer.Management.Smo;
using NUnit.Framework;
using System;
using System.Data;
using System.IO;
/// <summary>
/// Base test class any test class can inherit from.
/// </summary>
public abstract partial class BaseTest
{
/// <summary>
/// Executes before all tests of this class start.
/// </summary>
[TestFixtureSetUp]
public virtual void FixtureSetUp()
{
Console.WriteLine("Recover database ... ");
restoreDatabase("MYDATABASE", @"D:\MYBACKUP.BAK", "");
Console.WriteLine("Run tests in " + this.GetType() + " ...");
}
private void restoreDatabase(string destinationDatabase, string backupFile, string dbPath)
{
Microsoft.SqlServer.Management.Smo.Server sqlServer = new Microsoft.SqlServer.Management.Smo.Server(Properties.Settings.Default.SQLInstance);
Microsoft.SqlServer.Management.Smo.Restore restore = new Microsoft.SqlServer.Management.Smo.Restore();
restore.Action = Microsoft.SqlServer.Management.Smo.RestoreActionType.Database;
restore.Devices.Add(new Microsoft.SqlServer.Management.Smo.BackupDeviceItem(backupFile, Microsoft.SqlServer.Management.Smo.DeviceType.File));
System.Data.DataTable dt = restore.ReadBackupHeader(sqlServer);
restore.FileNumber = Convert.ToInt32(dt.Rows[dt.Rows.Count - 1]["Position"]);
dt = restore.ReadFileList(sqlServer);
int indexMdf = dt.Rows.Count - 2;
int indexLdf = dt.Rows.Count - 1;
Microsoft.SqlServer.Management.Smo.RelocateFile dataFile = new Microsoft.SqlServer.Management.Smo.RelocateFile();
string mdf = dt.Rows[indexMdf][1].ToString();
dataFile.LogicalFileName = dt.Rows[indexMdf][0].ToString();
dataFile.PhysicalFileName = Path.Combine(dbPath, destinationDatabase + Path.GetExtension(mdf));
Microsoft.SqlServer.Management.Smo.RelocateFile logFile = new Microsoft.SqlServer.Management.Smo.RelocateFile();
string ldf = dt.Rows[indexLdf][1].ToString();
logFile.LogicalFileName = dt.Rows[indexLdf][0].ToString();
logFile.PhysicalFileName = Path.Combine(dbPath, destinationDatabase + Path.GetExtension(ldf));
restore.RelocateFiles.Add(dataFile);
restore.RelocateFiles.Add(logFile);
restore.Database = destinationDatabase;
restore.ReplaceDatabase = true;
restore.SqlRestore(sqlServer); // <- this is where the FailedOperationException is thrown on the second execution
}
}
如前所述,恢复工作得很好第一次。第二次FailedOperationException声明:由于数据库当前正在使用,因此无法独占访问数据库。 RESTORE DATABASE将会因错误而停止。 (由我手动翻译)
我们正在使用最新的NUnit 2版本(2.6.4)。为什么数据库仍在使用中,我怎样才能正确地关闭它?
其实你应该以这样的方式重构你的代码(抽象),你不需要数据库来测试你的代码。现在,您不仅仅在测试“被测单元”,而是在测试数据库内容。 –
这是相关的:[错误 - 由于数据库正在使用而无法获得独占访问](http://stackoverflow.com/questions/22209499/error-exclusive-access-could-not-be-obtained-because- the-database-is-in-use/22209822#22209822) – DMason
@MikkoViitala感谢您的建议。在我们的单元测试中,我们通过BLL和DAL明确测试插入,更新和删除操作。所以测试函数的关键功能是与数据库一起工作。正如你所建议的那样,我很困惑如何以抽象的重构方式测试那些以SQL语句结尾的逻辑(也需要进行测试)。 – modiX