NUnit单元测试整理高级篇之针对输入数据比较多的TestCase的解决办法

    针对输入数据比较多的TestCase,通常将测试用例放在XML或文本文件中,这根据个人的使用习惯来决定,由此带来的好处是在不改变TestCase的情况下动态的增加测试用例的数量,以求测试尽量达到面面俱到。

    要测试的类

ContractedBlock.gif ExpandedBlockStart.gif Code
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

测试类

ContractedBlock.gif ExpandedBlockStart.gif Code
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));
            }
        }
    }
}

测试结果

2009052612211280.jpg

转载于:https://www.cnblogs.com/jewleo/archive/2009/05/25/05252252_1.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值