用VS2005进行单元测试

先建立一个类库,名称为Largest,用于寻找最大数

 

using System;
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[] list = null; // TODO: 初始化为适当的值

            
//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[] 897 }));
            Assert.AreEqual(
9, Largest.Cmp.Largest(new int[] 987 }));
            Assert.AreEqual(
9, Largest.Cmp.Largest(new int[] 789 }));
            Assert.AreEqual(
9, Largest.Cmp.Largest(new int[] 9798 }));
            Assert.AreEqual(
1, Largest.Cmp.Largest(new int[] {1}));
            Assert.AreEqual(
-7, Largest.Cmp.Largest(new int[] -8-9-7 }));

 

然后运行测试项目,可以查看测试结果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章