NDbUnit学习总结 - 一叶知秋 一屑知城 - 博客园

来源:百度文库 编辑:神马文学网 时间:2024/05/23 20:24:25

NDbUnit学习总结

Posted on 2009-05-12 15:33 冰碟 阅读(448) 评论(4)  编辑 收藏 网摘 所属分类: 04 - 持续集成

【参考】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,脚本如下

Code
CREATE TABLE [dbo].[Customer](
        [CustomerId] [int] IDENTITY(1,1) NOT NULL,
        [Firstname] [varchar](50) NULL,
        [Lastname] [varchar](50) NULL,
 CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED 
(
        [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。添加以下内容:

Code


    
        1
        John
        Doe
    

    
        2
        Sam
        Smith
    

    
        3
        Liu
        RanJun
    

 

这时,所有的准备条件都已经准备好了,接下来就可以编写NDbUnit方法了。

4,先要添加NUnit.Framework.dll类库,代码如下:

Code
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using NDbUnit.Core;


namespace NDbUnitDemo
{
    [TestFixture]
    public class Tests
    {
        private string connectionString;

        private NDbUnit.Core.INDbUnitTest mySqlDatabase;
        
        [TestFixtureSetUp]
        public void TestSetupFixture()
        {
            //初始化
            connectionString = @"Server=LIURJ\MYDATABASE;user=sa;password=sa;initial catalog=demo;";
            mySqlDatabase = new NDbUnit.Core.SqlClient.SqlDbUnitTest(connectionString);

            mySqlDatabase.ReadXmlSchema(@"..\..\MyDataset.xsd");
            mySqlDatabase.ReadXml(@"..\..\bin\debug\testdata.xml");
        }

        [SetUp]
        public void Setup()
        {
            //清除后插入数据
            mySqlDatabase.PerformDbOperation(NDbUnit.Core.DbOperationFlag.CleanInsertIdentity);
        }       

        [Test]
        public void Test()
        {
            //单元测试,判断数据库中Customer的记录数
            CustomerRespository respository = new CustomerRespository();
            Assert.AreEqual(2, respository.GetAllCustomers().Count);             
        }

        [TestFixtureTearDown]
        public void TestFixtureTearDown()
        {
            //删除数据
            mySqlDatabase.PerformDbOperation(NDbUnit.Core.DbOperationFlag.DeleteAll);
        }
    }
}

就是这样一个过程,我们在执行单元测试前就先将数据库恢复到我们预期的那个状态了。

这样很好,但是有人就想,数据存放在xml中,如果是手动去编写的话,对呀数据量很大就显得很不现实。

不要担心,NDbUnit也为我们提供了方法,这样就很方便的创建出测试数据了。

Code
           connectionString = "server=localhost;user=dbuser;password=dbpassword;initial catalog=MyDatabase;";

            _mySqlDatabase = new NDbUnit.Core.SqlClient.SqlDbUnitTest(_connectionString);

            _mySqlDatabase.ReadXmlSchema(@"..\..\MyDataset.xsd");

            System.Data.DataSet ds = _mySqlDatabase.GetDataSetFromDb();
            ds.WriteXml("TestData.xml");

 

如果默认情况下,表Customer包含数据的话,通过执行完上面的代码后,xml文件TestData.xml就包含了数据库中表Customer的原有的数据了。

作者:冰碟
出处:http://www.cnblogs.com/icebutterfly/
版权:本文版权归作者和博客园共有
转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢
要求:未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任