zoukankan      html  css  js  c++  java
  • NDbUnit学习总结

    【参考】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,1NOT NULL,
            
    [Firstname] [varchar](50NULL,
            
    [Lastname] [varchar](50NULL,
     
    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
    <?xml version="1.0" encoding="utf-8" ?>
    <MyDataset xmlns="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类库,代码如下:

    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/
    版权:本文版权归作者和博客园共有
    转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢
    要求:未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任
     
  • 相关阅读:
    java OA系统 自定义表单 流程审批 电子印章 手写文字识别 电子签名 即时通讯
    flowable 获取当前任务流程图片的输入流
    最新 接口api插件 Swagger3 更新配置详解
    springboot 集成 activiti 流程引擎
    java 在线考试系统源码 springboot 在线教育 视频直播功能 支持手机端
    阿里 Nacos 注册中心 配置启动说明
    springboot 集成外部tomcat war包部署方式
    java 监听 redis 过期事件
    springcloudalibaba 组件版本关系
    java WebSocket 即时通讯配置使用说明
  • 原文地址:https://www.cnblogs.com/icebutterfly/p/1454869.html
Copyright © 2011-2022 走看看