对自己要求要更严格一些,对别人要再多宽容一些。
单元测试学习之旅(一)
上一篇 /
下一篇 2009-06-17 21:17:29
/ 个人分类:单元测试
最近在学习单元测试Nunit(当然啦,个人感觉Junit貌似发展得更加成熟了)
今天就介绍下Nunit框架。
1、环境:VS 20005,Nunit 2.5
2、先在VS 2005中新建一个类库项目(供测试使用),于是构造以下供测试使用的类Class1.cs:
namespace ClassLibrary1 {
public class Class1 { public static int Largest(int[] list) { int index, max = Int32.MinValue; for(index=0;index<list.Length;index++) { if(list[index]>max) { max=list[index]; } } return max; } } } |
3、然后就是新建一个测试用的类,Test.cs(这里应该注意的是引入NUnit.FrameWork引用):
using System; using System.Collections.Generic; using System.Text; using NUnit.Framework; namespace ClassLibrary1 { [TestFixture] class TicketTest { [Test] public void Largesttest() { //Assert.AreEqual(9, Class1.Largest(new int[] { 8,9,7})); Assert.AreEqual(9, Class1.Largest(new int[] { 7, 8, 9 })); Assert.AreEqual(9, Class1.Largest(new int[] { 8, 9, 7 })); Assert.AreEqual(9, Class1.Largest(new int[] { 9, 8, 7 })); } [Test] public void TestOne() { Assert.AreEqual(9, Class1.Largest(new int[] { 9,7,8,9 })); } [Test] public void TestTwo() { Assert.AreEqual(1, Class1.Largest(new int[] {1})); } [Test] public void TestThree() { Assert.AreEqual(-7, Class1.Largest(new int[] {-7, -8, -9 })); } [Test] public void TestFour() { int [] array = new int[] { -9,-8,-7}; Assert.AreEqual(-7, Class1.Largest(array)); } } }
|
4、编译该类,生成对应的dll,如图所示:
5、在NUnit中,运行该dll文件,选中某个断言,点击运行(其实一个断言就有点想一个用例那样),如图所示:
收藏
举报
TAG: