NUnit的使用

 

代码
单元测试方法之间没有依赖关系
一个方法可能对应多个测试方法
[TestFixture]类前标注时NUnit会自动加载该类
[Test]标注测试方法。测试方法必须为public,
void ,没有参数
[Ignore]忽略
[ExpectedException]捕捉异常
[Explicit]当整个类测试时 该测试不执行,只有当单独执行该方法时才会执行
[SetUp] 初始化 每个Test都会执行一次
[TearDown]资源释放 每个Test都要执行一次
[TestFixtureSetUp]类初始化,类共享,只执行一次,最新执行
[TestFixtureTearDown]类资源释放,类共享,只执行一次,最后执行



using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  NUnit.Framework;
using  System.IO;
using  System.Xml;
using  System.Web;
namespace  MyStackTestProject
{
    [TestFixture]
    
public   class  LargestTest
    {
        
private  Largest large;
        [SetUp]
        
public   void  Init()
        {
            large 
=   new  Largest();
        }
        
///   <summary>
        
///  用txt存储测试数据
        
///   </summary>
        [Test]
        
public   void  GetLargestUseTxtTest()
        {
            
// 如果path="LargestTest.txt" 
            
// System.IO.FileNotFoundException : 未能找到文件“E:\实例验证\泛型\MyStackTestProject\bin\Debug\LargestTest.txt
            StreamReader s  =   new  StreamReader( " http://www.cnblogs.com/LargestTest.txt " );
            
string  line  =   null ;
            
while  ((line  =  s.ReadLine())  !=   null )
            {
                
if  (line.StartsWith( " # " ))
                    
continue ;
                
string [] array  =  line.Split( null );
                
int  expected  =   int .Parse(array[ 0 ]);
                List
< int >  list  =   new  List < int > ();
                
foreach  ( string  arr  in  array)
                {
                    
if ( ! arr.Equals ( "" ))
                       list.Add(
int .Parse(arr));
                }
                
int  result  =  large.GetLargest(list.ToArray());
                Assert.AreEqual(expected, result);
            }
            s.Close();
        }
        
///   <summary>
        
///  用xml文件存储测试数据
        
///   </summary>
        [Test]
        
public   void  GetLargestUseXmlTest()
        {
            XmlDocument doc 
=   new  XmlDocument();
            doc.Load(
" http://www.cnblogs.com/LargestTest.xml " );
            XmlNodeList list1 
=  doc.ChildNodes;
            
foreach  (XmlNode n1  in  list1)
            {
                
if  (n1.Name  ==   " TestData " )
                {
                    XmlNodeList list2 
=  n1.ChildNodes;
                    
foreach  (XmlNode n2  in  list2)
                    {
                        XmlNodeList n3 
=  n2.ChildNodes;
                        
string  expected  = n3[ 0 ].InnerText;
                        
string  strArray  = n3[ 1 ].InnerText ;
                        
string [] array  =  strArray.Split( null );
                        List
< int >  list  =   new  List < int > ();
                        
foreach  ( string  arr  in  array)
                        {
                            list.Add(
int .Parse(arr));
                        }
                        
int  result  =  large.GetLargest(list.ToArray());
                        Assert.AreEqual(
int .Parse (expected), result);
                    }
                }
            }
        }
       
    }
}

 

 

你可能感兴趣的:(it)