先建立一个类库,名称为Largest,用于寻找最大数
using System.Collections.Generic;
using System.Text;
namespace Largest
{
public class Cmp
{
public static int Largest(int[] list)
{
int index, max = Int32.MinValue;
if (list.Length == 0)
{
throw new ArgumentException("largest:Empty list");
}
for (index = 0; index <= list.Length - 1; index++)
{
if (list[index] > max)
{
max = list[index];
}
}
return max;
}
}
}
接着创建测试单元。方法:在上面的页面空白处右键选择创建单元测试,接着系统自动创建了一个测试项目。下面修改代码,以便更好的查看测试过程
//int expected = 0;
//int actual;
//actual = Largest.Cmp.Largest(list);
//Assert.AreEqual(expected, actual, "Largest.Cmp.Largest 未返回所需的值。");
//Assert.Inconclusive("验证此测试方法的正确性。");
Assert.AreEqual(9, Largest.Cmp.Largest(new int[] { 8, 9, 7 }));
Assert.AreEqual(9, Largest.Cmp.Largest(new int[] { 9, 8, 7 }));
Assert.AreEqual(9, Largest.Cmp.Largest(new int[] { 7, 8, 9 }));
Assert.AreEqual(9, Largest.Cmp.Largest(new int[] { 9, 7, 9, 8 }));
Assert.AreEqual(1, Largest.Cmp.Largest(new int[] {1}));
Assert.AreEqual(-7, Largest.Cmp.Largest(new int[] { -8, -9, -7 }));
然后运行测试项目,可以查看测试结果