NUnit单元测试整理高级篇之针对输入数据比较多的TestCase的解决办法
针对输入数据比较多的TestCase,通常将测试用例放在XML或文本文件中,这根据个人的使用习惯来决定,由此带来的好处是在不改变TestCase的情况下动态的增加测试用例的数量,以求测试尽量达到面面俱到。
要测试的类
using System;
using System.Collections.Generic;
using System.Text;
namespace NUnitTest
{
public class MathCompute
{
public int Largest(int[] array)
{
if (null == array || 0 == array.Length)
{
throw new Exception("参数传递错误!");
}
int largest = Int32.MinValue;
foreach (int element in array)
{
if (element > largest)
{
largest = element;
}
}
return largest;
}
}
}
测试用例:放在一个Txt文本文件中
# Input Test Data
5 1 2 3 4 5
-2 -2 -3 -5 -9
90 -2 90 -100 4 -20
10 10 10 2 -10
1 1
测试类
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using System.IO;
namespace NUnitTest
{
[TestFixture]
public class MathComputeTest
{
private MathCompute mc;
[TestFixtureSetUp]
public void Init()
{
mc = new MathCompute();
}
[Test]
public void TestLargest()
{
StreamReader sr = new StreamReader("http://www.cnblogs.com/DataSet.txt");
string line = null;
while (null != (line = sr.ReadLine()))
{
if (line.StartsWith("#"))
{
continue;
}
string[] tokens = line.Split(null);
int expected = Int32.Parse(tokens[0]);
List<int> list = new List<int>();
for (int i = 1; i < tokens.Length; ++i)
{
list.Add(Int32.Parse(tokens[i]));
}
int[] input = list.ToArray();
Assert.AreEqual(expected, mc.Largest(input));
}
}
}
}
测试结果
转载于:https://www.cnblogs.com/jewleo/archive/2009/05/25/05252252_1.html