NHibernate配置,单元测试总结

1 配置文件读取

            // 默认从 App.config,web.config或者hibernate.cfg.xml查找配置文件;
            var cfg = new NHibernate.Cfg.Configuration().Configure();
            
            // 如果配置文件不是以上“App.config,web.config或者hibernate.cfg.xml”,是自定义的配置文件名称;
            // 要注意使用MSTest进行单元测试时,请写配置文件的绝对路径,否则找不到;
            //var cfg = new NHibernate.Cfg.Configuration().Configure("D:/ProjectStudyTest/NHibernate/NHibernateStudy/Lesson3.Dao/Config/hibernate.cfg.xml");
            
            //如果用户Nunit进行单元测试,写相对路径即可。
            //var cfg = new NHibernate.Cfg.Configuration().Configure("Config/hibernate.cfg.xml");
            
            sessionFactory = cfg.BuildSessionFactory();

2 MSTest单元测试例子:

using Lesson3.Dao;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using Lesson3.Domain;
using System.Collections.Generic;

namespace Lesson3.MSTest
{
    /// <summary>
    ///This is a test class for ProductDaoTest and is intended
    ///to contain all ProductDaoTest Unit Tests
    ///</summary>
    [TestClass()]
    public class ProductDaoTest
    {

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes

        static IProductDao productDao;

        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            productDao = new ProductDao();
        }
        
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Save
        ///</summary>
        [TestMethod()]
        public void SaveTest()
        {
            var product = new Domain.Product
            {
                ID = Guid.NewGuid(),
                BuyPrice = 10M,
                Code = "ABC123",
                Name = "电脑-11111111",
                QuantityPerUnit = "20x1",
                SellPrice = 11M,
                Unit = "台",
                Remark = DateTime.Now.ToLocalTime().ToString()
            };

            var obj = productDao.Save(product);

            Assert.IsNotNull(obj);
        }

        /// <summary>
        ///A test for Update
        ///</summary>
        [TestMethod()]
        public void UpdateTest()
        {
            var product = productDao.LoadAll().FirstOrDefault();
            Assert.IsNotNull(product);

            product.SellPrice = 12M;

            Assert.AreEqual(12M, product.SellPrice);
        }

        /// <summary>
        ///A test for LoadAll
        ///</summary>
        [TestMethod()]
        public void LoadAllTest()
        {
            var count = productDao.LoadAll().Count;
            Assert.IsTrue(count > 0);
        }

        /// <summary>
        ///A test for Load
        ///</summary>
        [TestMethod()]
        public void LoadTest()
        {
            var product = productDao.LoadAll().FirstOrDefault();
            Assert.IsNotNull(product);

            var id = product.ID;
            Assert.IsNotNull(productDao.Get(id));
        }

        /// <summary>
        ///A test for Get
        ///</summary>
        [TestMethod()]
        public void GetTest()
        {
            var product = productDao.LoadAll().FirstOrDefault();
            Assert.IsNotNull(product);

            var id = product.ID;
            Assert.IsNotNull(productDao.Get(id));
        }

        /// <summary>
        ///A test for Delete
        ///</summary>
        [TestMethod()]
        public void DeleteTest()
        {
            var product = productDao.LoadAll().FirstOrDefault();
            Assert.IsNotNull(product);

            var id = product.ID;
            productDao.Delete(product);
            Assert.IsNull(productDao.Get(id));
        }
    }
}

 

3 Nunit单元测试例子

using System;
using System.Linq;
using Lesson3.Dao;
using NUnit.Framework;

namespace Lesson3.NunitTest
{
    [TestFixture]
    public class ProductDaoTest
    {
        private IProductDao productDao;

        [SetUp]
        public void Init()
        {
            productDao = new ProductDao();
        }

        [Test]
        public void SaveTest()
        {
            var product = new Domain.Product
            {
                ID = Guid.NewGuid(),
                BuyPrice = 10M,
                Code = "ABC123",
                Name = "电脑",
                QuantityPerUnit = "20x1",
                SellPrice = 11M,
                Unit = "台",
                Remark = DateTime.Now.ToLocalTime().ToString()
            };

            var obj = this.productDao.Save(product);

            Assert.NotNull(obj);
        }

        [Test]
        public void UpdateTest()
        {
            var product = this.productDao.LoadAll().FirstOrDefault();
            Assert.NotNull(product);

            product.SellPrice = 12M;

            Assert.AreEqual(12M, product.SellPrice);
        }

        [Test]
        public void DeleteTest()
        {
            var product = this.productDao.LoadAll().FirstOrDefault();
            Assert.NotNull(product);

            var id = product.ID;
            this.productDao.Delete(product);
            Assert.Null(this.productDao.Get(id));
        }

        [Test]
        public void GetTest()
        {
            var product = this.productDao.LoadAll().FirstOrDefault();
            Assert.NotNull(product);

            var id = product.ID;
            Assert.NotNull(this.productDao.Get(id));
        }

        [Test]
        public void LoadTest()
        {
            var product = this.productDao.LoadAll().FirstOrDefault();
            Assert.NotNull(product);

            var id = product.ID;
            Assert.NotNull(this.productDao.Get(id));
        }

        [Test]
        public void LoadAllTest()
        {
            var count = this.productDao.LoadAll().Count;
            Assert.True(count > 0);
        }
    }
}
posted @ 2011-08-04 14:20  深潭  阅读(705)  评论(0编辑  收藏  举报