NHibernate配置,单元测试总结

1 配置文件读取

1
2
3
4
5
6
7
8
9
10
11
// 默认从 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单元测试例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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单元测试例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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 @   深潭  阅读(705)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示