Unit Tests
在程序设计过程中会有许多种测试,单元只是其中的一种,单元测试并不能保证程序是完美无缺的,但是在所有的测试中,单元测试是第一个环节,也是最重要的一个环节。单元测试是一种由程序员自行测试的工作。简单点说,单元测试就是测试代码撰写者依据其所设想的方式执行是否产生了预期的结果。关于单元测试的重要性已经有许多文章做了很多深入的分析,这里就不再赘述。NUnit是一个为Net准备的自动化单元测试框架,它的作用就是帮助你方便的完成单元测试工作,同鼎鼎有名的JUnit一样,都是xUnit家族的成员。它的下载地址是:http://www.nunit.org。
在程序设计过程中会有许多种测试,单元只是其中的一种,单元测试并不能保证程序是完美无缺的,但是在所有的测试中,单元测试是第一个环节,也是最重要的一个环节。单元测试是一种由程序员自行测试的工作。简单点说,单元测试就是测试代码撰写者依据其所设想的方式执行是否产生了预期的结果。关于单元测试的重要性已经有许多文章做了很多深入的分析,这里就不再赘述。NUnit是一个为Net准备的自动化单元测试框架,它的作用就是帮助你方便的完成单元测试工作,同鼎鼎有名的JUnit一样,都是xUnit家族的成员。它的下载地址是:http://www.nunit.org。
TestFixture Attribute
TestFixture attribute主要是用在class上,其作用是标志该class含有需要执行的test methods。当你在一个class的定义里加上这个attribute,Test Runner就会检查该class,看看这个class是否含有test methods。
使用TextFixture Attribute的class需要的限制,就是需要有一个public的default constructor或者是没有定义任何的constructor。
TestFixtureSetUp 和TestFixtureTearDown
这两个主要用在TestFixture里面,其作用是提供一组函数执行任何测试运行之前(TestFixtureSetUP)和最后一个测试执行后(TestFixtureTearDown)。每一个TestFixture只能有一个TestFixtureSetUp方法和TestFixtureTearDown方法。如果一个以上的TestFixtureSetUp和TestFixtureTearDown方法,可以通过编译但是不会执行。注意一个TestFixture可以拥有一个TestFixtureSetUp和一个SetUp,也可以拥有一个TestFixtureTearDown和一个TearDown方法。
TestFixtureSetUp 和 TestFixtureTearDown 被用在不方便使用SetUp和TearDown方法。
一般情况使用 SetUp 和 TearDown。
TestFixtureSetUp 只在测试过程中执行一次,而 SetUp 会在测试中的每个测试方法执行前执行一次。
TestFixtureTearDown 和 TearDown 同理。
ExpectedException Attributes
ExpectedException attribute来标示某个method应该产生哪一个exception,[ExpectedException(typeof(Exception))]表示我们希望能捕获到发生的异常,如果没有捕获到异常,则表示测试失败。
Ignore Attributes
这个attribute来标示某个test method在测试执行的时候,略过这个method不要执行。
NUnit Assert Class
Assert class提供了一系列的static methods,让你可以用来验证主要程序的结果与你所预期的是否一样。
TicketTest.cs
1 using System;
2 using NUnit.Framework;
3
4 namespace TestNUnit
5 {
6 [TestFixture]
7 public class TicketTest
8 {
9 public TicketTest()
10 {
11 }
12
13 [TestFixtureSetUp]
14 public void RunBeforeAllTests()
15 {
16 Console.WriteLine("TestFixtureSetUp");
17 }
18
19 [TestFixtureTearDown]
20 public void RunAfterAllTests()
21 {
22 Console.WriteLine("TestFixtureTearDown");
23 }
24
25 [SetUp]
26 public void RunBeforeEachTest()
27 {
28 Console.WriteLine("SetUp");
29 }
30
31 [TearDown]
32 public void RunAfterEachTest()
33 {
34 Console.WriteLine("TearDown");
35 }
36
37 [Test]
38 public void Add()
39 {
40 Ticket ticket = new Ticket();
41 ticket.Add(100);
42 Assert.AreEqual(100, ticket.Amount);
43 }
44
45 [Test]
46 public void Sell()
47 {
48 Ticket ticket = new Ticket();
49 ticket.Add(100);
50 ticket.Sell();
51 ticket.Sell();
52 ticket.Sell();
53 Assert.AreEqual(97,ticket.Amount);
54 }
55
56 [Test]
57 [ExpectedException(typeof(Exception))]
58 public void ExcpetionTesting()
59 {
60 Ticket ticket = new Ticket();
61 ticket.Add(3);
62 ticket.Sell();
63 ticket.Sell();
64 ticket.Sell();
65 ticket.Sell();
66 }
67 }
68 }
2 using NUnit.Framework;
3
4 namespace TestNUnit
5 {
6 [TestFixture]
7 public class TicketTest
8 {
9 public TicketTest()
10 {
11 }
12
13 [TestFixtureSetUp]
14 public void RunBeforeAllTests()
15 {
16 Console.WriteLine("TestFixtureSetUp");
17 }
18
19 [TestFixtureTearDown]
20 public void RunAfterAllTests()
21 {
22 Console.WriteLine("TestFixtureTearDown");
23 }
24
25 [SetUp]
26 public void RunBeforeEachTest()
27 {
28 Console.WriteLine("SetUp");
29 }
30
31 [TearDown]
32 public void RunAfterEachTest()
33 {
34 Console.WriteLine("TearDown");
35 }
36
37 [Test]
38 public void Add()
39 {
40 Ticket ticket = new Ticket();
41 ticket.Add(100);
42 Assert.AreEqual(100, ticket.Amount);
43 }
44
45 [Test]
46 public void Sell()
47 {
48 Ticket ticket = new Ticket();
49 ticket.Add(100);
50 ticket.Sell();
51 ticket.Sell();
52 ticket.Sell();
53 Assert.AreEqual(97,ticket.Amount);
54 }
55
56 [Test]
57 [ExpectedException(typeof(Exception))]
58 public void ExcpetionTesting()
59 {
60 Ticket ticket = new Ticket();
61 ticket.Add(3);
62 ticket.Sell();
63 ticket.Sell();
64 ticket.Sell();
65 ticket.Sell();
66 }
67 }
68 }
TestNUnit.cs
1 using System;
2
3 namespace TestNUnit
4 {
5 public class Ticket
6 {
7 private int amount;
8
9 public int Amount
10 {
11 get { return amount; }
12 }
13
14 public void Add(int num)
15 {
16 amount += num;
17 }
18
19 public void Sell()
20 {
21 if(amount - 1 < 0)
22 throw new Exception("Amount不能为0");
23 amount -= 1;
24 }
25 }
26 }
2
3 namespace TestNUnit
4 {
5 public class Ticket
6 {
7 private int amount;
8
9 public int Amount
10 {
11 get { return amount; }
12 }
13
14 public void Add(int num)
15 {
16 amount += num;
17 }
18
19 public void Sell()
20 {
21 if(amount - 1 < 0)
22 throw new Exception("Amount不能为0");
23 amount -= 1;
24 }
25 }
26 }