本文主要是介绍Nunit 2.5.5的使用总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
private Calc calc;
[SetUp]
public void init()
{
calc = new Calc();
a = 10 ;
b = 2 ;
}
[TearDown]
public void Destroy()
{
}
10.关于SetUp与TearDown方法的执行顺序:
11.测试之前是什么状态,测试执行完毕后就应该是什么状态,而不应该由于测试执行的原因导致
13.测试类的私有方法时可以采取两种方式:
{
try
{
Calc2 calc2 = new Calc2();
Type type = calc2.GetType();
MethodInfo method = type.GetMethod( " Add " ,BindingFlags.NonPublic | BindingFlags.Instance);
object result = method.Invoke(calc2, new object [] { 2 , 3 });
Assert.AreEqual( 5 , result);
}
catch (Exception ex)
{
Assert.Fail();
}
}
14.Test Suite(测试套件):可以将多个测试组合到一起,同时执行多个测试。
{
using NUnit.Framework;
using NUnit.Core;
[Suite]
public static TestSuite Suite
{
get
{
TestSuite suite = new TestSuite( " All Tests " );
suite.Add( new Calc2Test());
suite.Add( new LargestTest());
suite.Add( new StackTest());
return suite;
}
}
}
{
using NUnit.Framework;
[TestFixture]
public class AllTests
{
[Suite]
public static IEnumerable Suite
{
get
{
ArrayList suite = new ArrayList();
suite.Add( new Calc2Test());
suite.Add( new LargestTest());
suite.Add( new StackTest());
return suite;
}
}
}
}
{
using NUnit.Framework;
[TestFixture]
public class AllTests
{
[Suite]
public static IEnumerable Suite
{
get
{
ArrayList suite = new ArrayList();
suite.Add( typeof (Calc2Test));
suite.Add( typeof (LargestTest));
suite.Add( typeof (StackTest));
return suite;
}
}
}
}
15.在对数据库进行单元测试的时候,为了避免连接的重复建立,可以调用TestFixtureSetUp和TestFixtureTearDown两个特性:
{
}
[TestFixtureTearDown]
public void ConnectionDestroy()
{
}
16.当在单元测试中,想不执行某个单元测试方法时,可以用Ignore特性,表示不永远不会被执行:
public void TestSubtract()
{
int actual = calc.Subtract(a, b);
int expect = 8 ;
Assert.AreEqual(expect, actual);
}
17.当在单元测试中,如果不想对某个方法进行单元测试,只是在它被选中时才进行测试的话,可以调用Explicit特性
public void TestMultiple()
{
int actual = calc.Multiple(a, b);
int expect = 20 ;
Assert.AreEqual(expect, actual);
}
18.如果要对测试方法进行分组时,可以用Category特性
public void TestAdd()
{
int actual = calc.Add(a, b);
int expect = 12 ;
Assert.AreEqual(expect,actual);
}
[Test]
[Category( " GroupA " )]
public void TestSubtract()
{
int actual = calc.Subtract(a, b);
int expect = 8 ;
Assert.AreEqual(expect, actual);
}
[Test]
[Category( " GroupB " )]
public void TestMultiple()
{
int actual = calc.Multiple(a, b);
int expect = 20 ;
Assert.AreEqual(expect, actual);
}
[Test]
[Category( " GroupB " )]
public void TestDevide()
{
int actual = 0 ;
try
{
actual = calc.Devide(a, b);
}
catch (Exception ex)
{
Assert.Fail();
}
int expect = 5 ;
Assert.AreEqual(expect, actual);
}
19.一个方法的测试可能要写很多个测试方法,这都是正常的:
{
public class BubbleSortClass
{
public int [] BubbleSort( int [] array)
{
if ( null == array)
{
Console.Error.WriteLine( " Prameters shouldn't be null. " );
return new int [] { };
}
for ( int i = 0 ; i < array.Length - 1 ; ++ i)
{
bool swap = false ;
for ( int j = 0 ; j < array.Length - i - 1 ; ++ j)
{
if (array[j] > array[j + 1 ])
{
int temp = array[j];
array[j] = array[j + 1 ];
array[j + 1 ] = temp;
swap = true ;
}
}
if ( ! swap)
{
return array;
}
}
return array;
}
}
}
{
using NUnit.Framework;
[TestFixture]
public class BubbleSortTest
{
BubbleSortClass bubble;
[SetUp]
public void Init()
{
bubble = new BubbleSortClass();
}
[Test]
public void TestBubbleSort()
{
int [] array = { 1 , 4 , 8 , 4 , 9 , 5 , 7 , 4 , - 10 };
int [] result = bubble.BubbleSort(array);
int [] expect = { - 10 , 1 , 4 , 4 , 4 , 5 , 7 , 8 , 9 };
Assert.AreEqual(expect, result);
}
[Test]
public void TestBubbleSort2()
{
int [] arr = null ;
int [] result = bubble.BubbleSort(arr);
int [] expect = { };
Assert.AreEqual(expect, result);
}
[Test]
public void TestBubbleSort3()
{
int [] arr = { };
int [] result = bubble.BubbleSort(arr);
int [] expect = { };
Assert.AreEqual(expect, result);
}
}
}
20.如果你期望你的测试方法能抛出异常,可以用ExpectedException特性,它通常用于异常的测试:
public void TestDevideByZero()
{
int actual = 0 ;
actual = calc.Devide( 2 , 0 );
}
21.当你而要对一个测试方法进行多组数据测试时,可以用一个文件将数据保存起来,然后编写程序去读取
{
public class Largest
{
public int GetLargest( int [] array)
{
if ( null == array || 0 == array.Length)
{
throw new Exception( " 数组不能为空! " );
}
int result = array[ 0 ];
for ( int i = 0 ; i < array.Length; i ++ )
{
if (result < array[i])
{
result = array[i];
}
}
return result;
}
}
}
{
using NUnit.Framework;
using System.IO;
[TestFixture]
public class LargestTest2
{
Largest largest ;
[SetUp]
public void init()
{
largest = new Largest();
}
[Test]
public void TestGetLargest()
{
var lines = File.ReadAllLines( " http://www.cnblogs.com/LargestTest.txt " );
foreach (var line in lines)
{
if (line.StartsWith( " # " ))
{
continue ;
}
string [] arr = line.Split( ' ' );
int expect = Convert.ToInt32(arr[ 0 ]);
var list = new List < int > ();
int temp = 0 ;
for (var i = 1 ; i < arr.Length; ++ i)
{
temp = Convert.ToInt32(arr[i]);
list.Add(temp);
}
int [] finalArr = list.ToArray();
int result = largest.GetLargest(finalArr);
Assert.AreEqual(expect, result);
}
}
}
}
22.如果想要让Nunit和Visaul Studio无缝地集成,用TestDriven,可以到TestDriven.net下载.
这篇关于Nunit 2.5.5的使用总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!