原来做项目,大抵是用TD平台测试,因为有专业的测试人员,所以也没有做单元测试,所以对此一无所知,今天才下载了NUnit 2.4.1,感觉还是非常不错的,毕竟自己写的代码自己先做一下测试,是比较好的做法,能最大限度地发现逻辑上的纰漏
之前做一个模仿Amg的音乐网站,因为先期对业务的复杂性考虑不够周全,没有编写接口,结果维护变得很狼狈,所以在这里做了一个mini 泛型接口:
using System;
using System.Collections.Generic;
using System.Text;

namespace TestUnit


{

/**//// <summary>
/// 管理员类接口
/// 泛型T代指专辑类
/// </summary>
interface IAdmin<T> where T:IComparable

{

/**//// <summary>
/// 返回当前月所有的天数
/// </summary>
IList<T> getAllAlbumList();
}
}
然后实现它:
using System;
using System.Collections.Generic;
using System.Text;

namespace TestUnit


{

/**//// <summary>
/// 实现IADMIN接口
/// </summary>
public class Admin:IAdmin<string>

{
public IList<string> getAllAlbumList()

{
IList<string> list = new List<string>();

int days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);

list.Add(string.Format(
"{0,-4}年{1,-2}月有{2,-2}天", DateTime.Now.Year, DateTime.Now.Month,days
));

for (int i = 1; i <= days; i++)

{
list.Add("day "+i.ToString());
}

return list;
}
}
}
引入NUnit,进行测试
using System;
using System.Collections.Generic;
using System.Text;

using NUnit.Framework;

namespace TestUnit


{

/**//// <summary>
/// 单元测试类
/// </summary>
[TestFixture]
public class TestUnits

{
[Test]
public void printAdminInfo()

{
Admin ad = new Admin();

IList<string> list = ad.getAllAlbumList();

foreach (string ss in list)

{
Console.WriteLine("{0,-20},bingo!", ss);
}
}
}
}
在NUnit里测试,查看“控制台”输出结果: