小菜鸟一步步打造图书馆外挂之五:数据访问之单元测试(二)
使用NUnit对DAL工程中的类进行测试,并介绍一个NUnit的插件testdriven.net...
上前已经写好了数据访问的方法,现在用NUnit对它进行测试。一般我们用NUnit自带的图形界面来测试它和Visual Studio 结合得不是很好,每次运行测试时还要打开一个别一个窗口,如下图:
现在介绍一个NUnit的插件testdriven.net,他是一个把Visual Studio和NUnit结合得很好的工具,可以从http://www.testdriven.net/上下载。下载后安装后想要运行某一个测试代码只要把光标放到该方法里单击右键就可以运行了,而且运行结果也在Visual Studio中输出,它还能以断点的方式来调试我们写的测试代码,还能统计出我们测试代码的覆盖率,很方便,如下图:
运行以上Coverage就会出现统计测试代码覆盖率的界面,如下图运行整个工程的测试:
红色的是原代码,黑色的是我们写的测试代码,后面有代码的覆盖率,如下图,蓝色的为测试时已运行的代码,红色的为未运行的代码:
上面讲了一些工具之类的,现在来看看DAL中的测试代码,我们对每个类写了一个测试类,如BookXML写了BookXMLTest类,UserXML写了UserXMLTest类,ADSLXML有ADSLXMLTest类,SettingXML有SettingXMLTest类,在写测试方法时要注意每个测试方法是独立的,不能相互依赖,不能共享作为参数的对象,运行完测试后要还原数据的原始性,不能因为测试而影响原来的数据。
BookXMLTest类:
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5using NUnit.Framework;
6using LibraryHelper.Model;
7
8namespace LibraryHelper.DAL.Test
9{
10 [TestFixture]
11 public class BookXMLTest
12 {
13 BookXML bookXML;
14 //该类中的所有Case都引用同一个BookXML对象
15 //所以该对象放到TestFixtureSetUp属性方法中初始化
16
17 [TestFixtureSetUp]
18 public void InitBookXML()
19 {
20 bookXML = new BookXML();
21 }
22
23 [Test]
24 public void TestInsertBook()
25 {
26
27 Book insertBook = new Book();
28 insertBook.Title = "重构";
29 insertBook.BorrowDate = Convert.ToDateTime("2009-3-1");
30 insertBook.ReturnDate = Convert.ToDateTime("2009-2-1");
31 insertBook.BorrowNum = "2";
32 insertBook.ISBN = "45454545";
33
34 //该方法添加成功后返回bool值,Assert一下是不是正确
35 Boolean insertResult = bookXML.InsertBook(insertBook);
36
37 Assert.IsTrue(insertResult);
38
39 //XML文件中没有索引,所以用这种取得记录数量再减1的方法可以获得刚添加进入记录的行号
40 Int32 insertBookIndex = bookXML.GetBookNum() - 1;
41
42 //添加进去再取出来对比一下两个对象
43 Book getInsertBook = bookXML.GetSingleBookByRowIndex(insertBookIndex);
44
45 CompareBook(insertBook, getInsertBook);
46
47 //对象完之后删除该记录,恢复数据的原样
48 bookXML.DeleteBookByRowIndex(insertBookIndex);
49
50 }
51
52 [Test]
53 public void TestGetBookList1()
54 {
55 //不添加数据看能不能取出集合
56 List<Book> getBookList = bookXML.GetBookList();
57 Assert.IsNotNull(getBookList);
58 }
59
60 [Test]
61 public void TestGetBookList2()
62 {
63
64 //添加两个Book对象再把它们取出来看看是不是刚刚加进去的对象
65
66 Book insertBook1 = new Book();
67 insertBook1.Title = "重构1";
68 insertBook1.BorrowDate = Convert.ToDateTime("2009-1-1");
69 insertBook1.ReturnDate = Convert.ToDateTime("2009-1-2");
70 insertBook1.BorrowNum = "1";
71 insertBook1.ISBN = "111111";
72 bookXML.InsertBook(insertBook1);
73 Int32 insertBookIndex1 = bookXML.GetBookNum() - 1;
74
75 Book insertBook2 = new Book();
76 insertBook2.Title = "重构2";
77 insertBook2.BorrowDate = Convert.ToDateTime("2009-2-1");
78 insertBook2.ReturnDate = Convert.ToDateTime("2009-2-2");
79 insertBook2.BorrowNum = "2";
80 insertBook2.ISBN = "222222";
81 bookXML.InsertBook(insertBook2);
82
83 Int32 insertBookIndex2 = bookXML.GetBookNum() - 1;
84
85 List<Book> getBookList = bookXML.GetBookList();
86
87 CompareBook(getBookList[insertBookIndex1], insertBook1);
88 CompareBook(getBookList[insertBookIndex2], insertBook2);
89
90 //比较完后删除刚加进去的对象
91 bookXML.DeleteBookByRowIndex(insertBookIndex2);
92 bookXML.DeleteBookByRowIndex(insertBookIndex1);
93 }
94
95 [Test]
96 public void TestGetBookNum1()
97 {
98 //取出来的对象不应该是Null的
99 Assert.IsNotNull(bookXML.GetBookNum());
100 }
101
102 [Test]
103 public void TestGetBookNum2()
104 {
105 Int32 bookNum1 = bookXML.GetBookNum();
106
107 Book insertBook = new Book();
108 insertBook.Title = "重构";
109 insertBook.BorrowDate = Convert.ToDateTime("2009-3-1");
110 insertBook.ReturnDate = Convert.ToDateTime("2009-2-1");
111 insertBook.BorrowNum = "1";
112 insertBook.ISBN = "111111";
113 bookXML.InsertBook(insertBook);
114
115 Int32 bookNum2 = bookXML.GetBookNum();
116
117 bookXML.DeleteBookByRowIndex(bookNum2 - 1);
118
119 Int32 bookNum3 = bookXML.GetBookNum();
120
121 Assert.AreEqual(bookNum1,bookNum3);
122 Assert.AreEqual(bookNum1 + 1, bookNum2);
123 }
124
125 [Test]
126 public void TestGetSingleBookByRowIndex()
127 {
128 TestInsertBook();
129 }
130
131 [Test]
132 public void DeleteBookByRowIndex()
133 {
134 Book insertBook = new Book();
135 insertBook.Title = "重构";
136 insertBook.BorrowDate = Convert.ToDateTime("2009-3-1");
137 insertBook.ReturnDate = Convert.ToDateTime("2009-2-1");
138 insertBook.BorrowNum = "2";
139 insertBook.ISBN = "45454545";
140
141 bookXML.InsertBook(insertBook);
142 Int32 bookNum1 = bookXML.GetBookNum();
143 bookXML.DeleteBookByRowIndex(bookNum1 - 1);
144 Int32 bookNum2 = bookXML.GetBookNum();
145
146 Assert.AreEqual(bookNum1, bookNum2 + 1);
147
148 }
149
150 [Test]
151 public void TestDeleteAllBook()
152 {
153 for (Int32 i = 0; i < 10; i++)
154 {
155 Book insertBook = new Book();
156 insertBook.Title = "重构";
157 insertBook.BorrowDate = Convert.ToDateTime("2009-3-1");
158 insertBook.ReturnDate = Convert.ToDateTime("2009-2-1");
159 insertBook.BorrowNum = "2";
160 insertBook.ISBN = "45454545";
161
162 bookXML.InsertBook(insertBook);
163 }
164
165 bookXML.DeleteAllBook();
166
167 Int32 bookNum = bookXML.GetBookNum();
168
169 Assert.AreEqual(0,bookNum);
170 }
171
172 [Test]
173 public void TestUpdataBookByRowIndex()
174 {
175 Book insertBook = new Book();
176
177 insertBook.Title = "重构";
178 insertBook.BorrowDate = Convert.ToDateTime("2009-3-1");
179 insertBook.ReturnDate = Convert.ToDateTime("2009-4-1");
180 insertBook.BorrowNum = "2";
181 insertBook.ISBN = "45454545";
182
183 bookXML.InsertBook(insertBook);
184
185 Int32 insertBookIndex = bookXML.GetBookNum() - 1;
186
187 Book updateBook1 = bookXML.GetSingleBookByRowIndex(insertBookIndex);
188 updateBook1.Title = "update";
189 updateBook1.BorrowDate = Convert.ToDateTime("2009-3-1");
190 updateBook1.ReturnDate = Convert.ToDateTime("2009-4-1");
191 updateBook1.BorrowNum = "212";
192 updateBook1.ISBN = "222222";
193
194 bookXML.UpdataBookByRowIndex(insertBookIndex, updateBook1);
195
196 Book updateBook2 = bookXML.GetSingleBookByRowIndex(insertBookIndex);
197
198 CompareBook(updateBook1, updateBook2);
199
200 bookXML.DeleteBookByRowIndex(insertBookIndex);
201 }
202
203
204 //该类中很多方法都用到两个对象的比较,所以单独写成一个方法调用
205 private void CompareBook(Book book1, Book book2)
206 {
207 Assert.AreEqual(book1.Title, book2.Title);
208 Assert.AreEqual(book1.BorrowDate,book2.BorrowDate);
209 Assert.AreEqual(book1.ReturnDate, book2.ReturnDate);
210 Assert.AreEqual(book1.BorrowNum,book2.BorrowNum);
211 Assert.AreEqual(book1.ISBN,book2.ISBN);
212 }
213 }
214}
215
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5using NUnit.Framework;
6using LibraryHelper.Model;
7
8namespace LibraryHelper.DAL.Test
9{
10 [TestFixture]
11 public class BookXMLTest
12 {
13 BookXML bookXML;
14 //该类中的所有Case都引用同一个BookXML对象
15 //所以该对象放到TestFixtureSetUp属性方法中初始化
16
17 [TestFixtureSetUp]
18 public void InitBookXML()
19 {
20 bookXML = new BookXML();
21 }
22
23 [Test]
24 public void TestInsertBook()
25 {
26
27 Book insertBook = new Book();
28 insertBook.Title = "重构";
29 insertBook.BorrowDate = Convert.ToDateTime("2009-3-1");
30 insertBook.ReturnDate = Convert.ToDateTime("2009-2-1");
31 insertBook.BorrowNum = "2";
32 insertBook.ISBN = "45454545";
33
34 //该方法添加成功后返回bool值,Assert一下是不是正确
35 Boolean insertResult = bookXML.InsertBook(insertBook);
36
37 Assert.IsTrue(insertResult);
38
39 //XML文件中没有索引,所以用这种取得记录数量再减1的方法可以获得刚添加进入记录的行号
40 Int32 insertBookIndex = bookXML.GetBookNum() - 1;
41
42 //添加进去再取出来对比一下两个对象
43 Book getInsertBook = bookXML.GetSingleBookByRowIndex(insertBookIndex);
44
45 CompareBook(insertBook, getInsertBook);
46
47 //对象完之后删除该记录,恢复数据的原样
48 bookXML.DeleteBookByRowIndex(insertBookIndex);
49
50 }
51
52 [Test]
53 public void TestGetBookList1()
54 {
55 //不添加数据看能不能取出集合
56 List<Book> getBookList = bookXML.GetBookList();
57 Assert.IsNotNull(getBookList);
58 }
59
60 [Test]
61 public void TestGetBookList2()
62 {
63
64 //添加两个Book对象再把它们取出来看看是不是刚刚加进去的对象
65
66 Book insertBook1 = new Book();
67 insertBook1.Title = "重构1";
68 insertBook1.BorrowDate = Convert.ToDateTime("2009-1-1");
69 insertBook1.ReturnDate = Convert.ToDateTime("2009-1-2");
70 insertBook1.BorrowNum = "1";
71 insertBook1.ISBN = "111111";
72 bookXML.InsertBook(insertBook1);
73 Int32 insertBookIndex1 = bookXML.GetBookNum() - 1;
74
75 Book insertBook2 = new Book();
76 insertBook2.Title = "重构2";
77 insertBook2.BorrowDate = Convert.ToDateTime("2009-2-1");
78 insertBook2.ReturnDate = Convert.ToDateTime("2009-2-2");
79 insertBook2.BorrowNum = "2";
80 insertBook2.ISBN = "222222";
81 bookXML.InsertBook(insertBook2);
82
83 Int32 insertBookIndex2 = bookXML.GetBookNum() - 1;
84
85 List<Book> getBookList = bookXML.GetBookList();
86
87 CompareBook(getBookList[insertBookIndex1], insertBook1);
88 CompareBook(getBookList[insertBookIndex2], insertBook2);
89
90 //比较完后删除刚加进去的对象
91 bookXML.DeleteBookByRowIndex(insertBookIndex2);
92 bookXML.DeleteBookByRowIndex(insertBookIndex1);
93 }
94
95 [Test]
96 public void TestGetBookNum1()
97 {
98 //取出来的对象不应该是Null的
99 Assert.IsNotNull(bookXML.GetBookNum());
100 }
101
102 [Test]
103 public void TestGetBookNum2()
104 {
105 Int32 bookNum1 = bookXML.GetBookNum();
106
107 Book insertBook = new Book();
108 insertBook.Title = "重构";
109 insertBook.BorrowDate = Convert.ToDateTime("2009-3-1");
110 insertBook.ReturnDate = Convert.ToDateTime("2009-2-1");
111 insertBook.BorrowNum = "1";
112 insertBook.ISBN = "111111";
113 bookXML.InsertBook(insertBook);
114
115 Int32 bookNum2 = bookXML.GetBookNum();
116
117 bookXML.DeleteBookByRowIndex(bookNum2 - 1);
118
119 Int32 bookNum3 = bookXML.GetBookNum();
120
121 Assert.AreEqual(bookNum1,bookNum3);
122 Assert.AreEqual(bookNum1 + 1, bookNum2);
123 }
124
125 [Test]
126 public void TestGetSingleBookByRowIndex()
127 {
128 TestInsertBook();
129 }
130
131 [Test]
132 public void DeleteBookByRowIndex()
133 {
134 Book insertBook = new Book();
135 insertBook.Title = "重构";
136 insertBook.BorrowDate = Convert.ToDateTime("2009-3-1");
137 insertBook.ReturnDate = Convert.ToDateTime("2009-2-1");
138 insertBook.BorrowNum = "2";
139 insertBook.ISBN = "45454545";
140
141 bookXML.InsertBook(insertBook);
142 Int32 bookNum1 = bookXML.GetBookNum();
143 bookXML.DeleteBookByRowIndex(bookNum1 - 1);
144 Int32 bookNum2 = bookXML.GetBookNum();
145
146 Assert.AreEqual(bookNum1, bookNum2 + 1);
147
148 }
149
150 [Test]
151 public void TestDeleteAllBook()
152 {
153 for (Int32 i = 0; i < 10; i++)
154 {
155 Book insertBook = new Book();
156 insertBook.Title = "重构";
157 insertBook.BorrowDate = Convert.ToDateTime("2009-3-1");
158 insertBook.ReturnDate = Convert.ToDateTime("2009-2-1");
159 insertBook.BorrowNum = "2";
160 insertBook.ISBN = "45454545";
161
162 bookXML.InsertBook(insertBook);
163 }
164
165 bookXML.DeleteAllBook();
166
167 Int32 bookNum = bookXML.GetBookNum();
168
169 Assert.AreEqual(0,bookNum);
170 }
171
172 [Test]
173 public void TestUpdataBookByRowIndex()
174 {
175 Book insertBook = new Book();
176
177 insertBook.Title = "重构";
178 insertBook.BorrowDate = Convert.ToDateTime("2009-3-1");
179 insertBook.ReturnDate = Convert.ToDateTime("2009-4-1");
180 insertBook.BorrowNum = "2";
181 insertBook.ISBN = "45454545";
182
183 bookXML.InsertBook(insertBook);
184
185 Int32 insertBookIndex = bookXML.GetBookNum() - 1;
186
187 Book updateBook1 = bookXML.GetSingleBookByRowIndex(insertBookIndex);
188 updateBook1.Title = "update";
189 updateBook1.BorrowDate = Convert.ToDateTime("2009-3-1");
190 updateBook1.ReturnDate = Convert.ToDateTime("2009-4-1");
191 updateBook1.BorrowNum = "212";
192 updateBook1.ISBN = "222222";
193
194 bookXML.UpdataBookByRowIndex(insertBookIndex, updateBook1);
195
196 Book updateBook2 = bookXML.GetSingleBookByRowIndex(insertBookIndex);
197
198 CompareBook(updateBook1, updateBook2);
199
200 bookXML.DeleteBookByRowIndex(insertBookIndex);
201 }
202
203
204 //该类中很多方法都用到两个对象的比较,所以单独写成一个方法调用
205 private void CompareBook(Book book1, Book book2)
206 {
207 Assert.AreEqual(book1.Title, book2.Title);
208 Assert.AreEqual(book1.BorrowDate,book2.BorrowDate);
209 Assert.AreEqual(book1.ReturnDate, book2.ReturnDate);
210 Assert.AreEqual(book1.BorrowNum,book2.BorrowNum);
211 Assert.AreEqual(book1.ISBN,book2.ISBN);
212 }
213 }
214}
215
UserXMLTest类:
Code
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using LibraryHelper.Model;
namespace LibraryHelper.DAL.Test
{
[TestFixture]
public class UserXMLTest
{
UserXML userXML;
[TestFixtureSetUp]
public void InitUserXML()
{
userXML = new UserXML();
}
[Test]
public void TestGetUserAndSaveUser()
{
//先添加一个用户再取出来比较,所以这里可以一次测两个方法
User insertUser = new User();
insertUser.LoginID = "111111";
insertUser.Password = "000000";
//把insertUser保存进去
userXML.SaveUser(insertUser);
//把刚刚保存进去的user取出来
User getUser = userXML.GetUser();
Assert.AreEqual(insertUser.LoginID,getUser.LoginID);
Assert.AreEqual(insertUser.Password,insertUser.Password);
//比较完后清空数据
userXML.SaveUser(new User("", ""));
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using LibraryHelper.Model;
namespace LibraryHelper.DAL.Test
{
[TestFixture]
public class UserXMLTest
{
UserXML userXML;
[TestFixtureSetUp]
public void InitUserXML()
{
userXML = new UserXML();
}
[Test]
public void TestGetUserAndSaveUser()
{
//先添加一个用户再取出来比较,所以这里可以一次测两个方法
User insertUser = new User();
insertUser.LoginID = "111111";
insertUser.Password = "000000";
//把insertUser保存进去
userXML.SaveUser(insertUser);
//把刚刚保存进去的user取出来
User getUser = userXML.GetUser();
Assert.AreEqual(insertUser.LoginID,getUser.LoginID);
Assert.AreEqual(insertUser.Password,insertUser.Password);
//比较完后清空数据
userXML.SaveUser(new User("", ""));
}
}
}
ADSLXMLTest类:
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5using NUnit.Framework;
6using LibraryHelper.Model;
7
8namespace LibraryHelper.DAL.Test
9{
10 [TestFixture]
11 public class ADSLXMLText
12 {
13 ADSLXML adslXML;
14
15 [TestFixtureSetUp]
16 public void InitadslXML()
17 {
18 adslXML = new ADSLXML();
19 }
20
21 [Test]
22 public void TestAll()
23 {
24 //这里先保存进去一个再取出来比较
25 adslXML.SaveADSL("connectionName", "userName", "userPassword");
26
27 Assert.AreEqual("connectionName", adslXML.GetConnectionName());
28 Assert.AreEqual("userName", adslXML.GetUserName());
29 Assert.AreEqual("userPassword", adslXML.GetUserPassword());
30
31 //比较完后清空数据
32 adslXML.SaveADSL("","","");
33 }
34 }
35}
36
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5using NUnit.Framework;
6using LibraryHelper.Model;
7
8namespace LibraryHelper.DAL.Test
9{
10 [TestFixture]
11 public class ADSLXMLText
12 {
13 ADSLXML adslXML;
14
15 [TestFixtureSetUp]
16 public void InitadslXML()
17 {
18 adslXML = new ADSLXML();
19 }
20
21 [Test]
22 public void TestAll()
23 {
24 //这里先保存进去一个再取出来比较
25 adslXML.SaveADSL("connectionName", "userName", "userPassword");
26
27 Assert.AreEqual("connectionName", adslXML.GetConnectionName());
28 Assert.AreEqual("userName", adslXML.GetUserName());
29 Assert.AreEqual("userPassword", adslXML.GetUserPassword());
30
31 //比较完后清空数据
32 adslXML.SaveADSL("","","");
33 }
34 }
35}
36
SettingXMLTest类:
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5using NUnit.Framework;
6using LibraryHelper.Model;
7
8namespace LibraryHelper.DAL.Test
9{
10 [TestFixture]
11 public class SettingXMLTest
12 {
13 SettingXML setttingXML;
14
15 [TestFixtureSetUp]
16 public void InitsettingXML()
17 {
18 setttingXML = new SettingXML();
19 }
20
21 [Test]
22 public void TestSetWarningDayAndGetWarningDay()
23 {
24 //先把一个值加进去再取出来比较,所以这两个方法可以同时测试,以下测试方法一样
25 setttingXML.SetWarningDay("100");
26
27 Assert.AreEqual("100",setttingXML.GetWarningDay());
28
29 }
30
31 [Test]
32 public void TestSetStartTypeAndGetStartType()
33 {
34 setttingXML.SetStartType("0");
35
36 Assert.AreEqual("0",setttingXML.GetStartType());
37
38 }
39
40 [Test]
41 public void TestSetLastStartTimeAndGetLastStartTime()
42 {
43 setttingXML.SetLastStartTime("2009-3-1");
44
45 Assert.AreEqual("2009-3-1",setttingXML.GetLastStartTime());
46
47 setttingXML.SetLastStartTime("");
48 }
49
50 [Test]
51 public void TestSetDisconnectionADSLAndGetDisconnectionADSL()
52 {
53 setttingXML.SetDisconnectionADSL("0");
54
55 Assert.AreEqual("0",setttingXML.GetDisconnectionADSL());
56 }
57
58
59 }
60}
61
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5using NUnit.Framework;
6using LibraryHelper.Model;
7
8namespace LibraryHelper.DAL.Test
9{
10 [TestFixture]
11 public class SettingXMLTest
12 {
13 SettingXML setttingXML;
14
15 [TestFixtureSetUp]
16 public void InitsettingXML()
17 {
18 setttingXML = new SettingXML();
19 }
20
21 [Test]
22 public void TestSetWarningDayAndGetWarningDay()
23 {
24 //先把一个值加进去再取出来比较,所以这两个方法可以同时测试,以下测试方法一样
25 setttingXML.SetWarningDay("100");
26
27 Assert.AreEqual("100",setttingXML.GetWarningDay());
28
29 }
30
31 [Test]
32 public void TestSetStartTypeAndGetStartType()
33 {
34 setttingXML.SetStartType("0");
35
36 Assert.AreEqual("0",setttingXML.GetStartType());
37
38 }
39
40 [Test]
41 public void TestSetLastStartTimeAndGetLastStartTime()
42 {
43 setttingXML.SetLastStartTime("2009-3-1");
44
45 Assert.AreEqual("2009-3-1",setttingXML.GetLastStartTime());
46
47 setttingXML.SetLastStartTime("");
48 }
49
50 [Test]
51 public void TestSetDisconnectionADSLAndGetDisconnectionADSL()
52 {
53 setttingXML.SetDisconnectionADSL("0");
54
55 Assert.AreEqual("0",setttingXML.GetDisconnectionADSL());
56 }
57
58
59 }
60}
61