【参考】http://code.google.com/p/ndbunit/wiki/QuickStartGuide
简介:NDbUnit用于.net的数据库unit-testing 框架。在测试前和运行测试间将你的数据库放进一个已知状态。
在进行单元测试中集成NDBUnit需要以下几个步骤:
1,下载NDbUnit.Core.dll并添加引用到你的项目中
2,创建一个.NET XSD文件(dataset schema definition),并将你数据库中的表添加进来
3,在创建一个XML文件,它包含了你要通过NDbUnit加载的数据
4,通过NDbUnit方法控制你在进行测试时的数据库状态
示例:
准备:在你的数据库中创建一个表Customer,脚本如下
CREATETABLE[dbo].[Customer](
[CustomerId][int]IDENTITY(1,1)NOTNULL,
[Firstname][varchar](50)NULL,
[Lastname][varchar](50)NULL,
CONSTRAINT[PK_Customer]PRIMARYKEYCLUSTERED
(
[CustomerId]ASC
)WITH(PAD_INDEX=OFF,
STATISTICS_NORECOMPUTE=OFF,
IGNORE_DUP_KEY=OFF,
ALLOW_ROW_LOCKS=ON,
ALLOW_PAGE_LOCKS=ON)
ON[PRIMARY]
)ON[PRIMARY]
GO
然后进行一下操作:
1,新建工程,并引用NDBUnit.Core.dll
2,新建一个DataSet,命名为MyDataset.xsd,并通过Server Explorer将表Customer拖到MyDataset中(TableAdapter可以删除,MyDataset相关的文件,除MyDataset.xsd以外都可以删除)。
3,新建一个XML文件,命名为Customer.xml。添加以下内容:
<?xmlversion="1.0"encoding="utf-8"?>
<MyDatasetxmlns="http://tempuri.org/MyDataset.xsd">
<Customer>
<CustomerId>1</CustomerId>
<Firstname>John</Firstname>
<Lastname>Doe</Lastname>
</Customer>
<Customer>
<CustomerId>2</CustomerId>
<Firstname>Sam</Firstname>
<Lastname>Smith</Lastname>
</Customer>
<Customer>
<CustomerId>3</CustomerId>
<Firstname>Liu</Firstname>
<Lastname>RanJun</Lastname>
</Customer>
</MyDataset>
这时,所有的准备条件都已经准备好了,接下来就可以编写NDbUnit方法了。
4,先要添加NUnit.Framework.dll类库,代码如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingNUnit.Framework;
usingNDbUnit.Core;
namespaceNDbUnitDemo
{
[TestFixture]
publicclassTests
{
privatestringconnectionString;
privateNDbUnit.Core.INDbUnitTestmySqlDatabase;
[TestFixtureSetUp]
publicvoidTestSetupFixture()
{
//初始化
connectionString=@"Server=LIURJ\MYDATABASE;user=sa;password=sa;initialcatalog=demo;";
mySqlDatabase=newNDbUnit.Core.SqlClient.SqlDbUnitTest(connectionString);
mySqlDatabase.ReadXmlSchema(@"..\..\MyDataset.xsd");
mySqlDatabase.ReadXml(@"..\..\bin\debug\testdata.xml");
}
[SetUp]
publicvoidSetup()
{
//清除后插入数据
mySqlDatabase.PerformDbOperation(NDbUnit.Core.DbOperationFlag.CleanInsertIdentity);
}
[Test]
publicvoidTest()
{
//单元测试,判断数据库中Customer的记录数
CustomerRespositoryrespository=newCustomerRespository();
Assert.AreEqual(2,respository.GetAllCustomers().Count);
}
[TestFixtureTearDown]
publicvoidTestFixtureTearDown()
{
//删除数据
mySqlDatabase.PerformDbOperation(NDbUnit.Core.DbOperationFlag.DeleteAll);
}
}
}
就是这样一个过程,我们在执行单元测试前就先将数据库恢复到我们预期的那个状态了。
这样很好,但是有人就想,数据存放在xml中,如果是手动去编写的话,对呀数据量很大就显得很不现实。
不要担心,NDbUnit也为我们提供了方法,这样就很方便的创建出测试数据了。
connectionString="server=localhost;user=dbuser;password=dbpassword;initialcatalog=MyDatabase;";
_mySqlDatabase=newNDbUnit.Core.SqlClient.SqlDbUnitTest(_connectionString);
_mySqlDatabase.ReadXmlSchema(@"..\..\MyDataset.xsd");
System.Data.DataSetds=_mySqlDatabase.GetDataSetFromDb();
ds.WriteXml("TestData.xml");
如果默认情况下,表Customer包含数据的话,通过执行完上面的代码后,xml文件TestData.xml就包含了数据库中表Customer的原有的数据了。
出处: http://www.cnblogs.com/icebutterfly/
版权:本文版权归作者和博客园共有
转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢
要求:未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任
本文介绍如何在.NET环境中使用NDbUnit进行数据库单元测试,包括准备工作、集成步骤以及利用XML文件加载测试数据的方法。此外,还提供了解决手动编写大量测试数据问题的解决方案,以及NDbUnit生成测试数据的功能。

被折叠的 条评论
为什么被折叠?



