Nunit 2.5.5的使用总结

来源:互联网 发布:白金数据原著 编辑:程序博客网 时间:2024/05/01 10:22
引入必要的命名空间:
using NUnit.Framework;

 

引用组件:
nunit.framework.dll

 

1.使用Unit的最佳实践:
a.新建一个名为Test的souce folder, 用于存放测试类源代码。
b.目标类与测试类应该位于同一个包下面,这样测试类就不必导入源代码所在的包,因为他们位于
同一包下面。
c.测试类命名规则:假如目标类是Culculator,那么测试类应该命名为TestCalculator或者是CalculatorTest

 

2.Xunit的口号:Keep the bar green to keep the code clean.

 

3.单元测试不是为了证明你是对的,而是为了证明你没有错误。

 

4.测试用例:(Test Case) 是单元测试的一个很重要的方面。

 

5.单元测试主要是用来判断程序的执行结果与自己期望的结果是否一致。

 

6.测试方法需要满足如下原则:
a.  public的
b.  void的
c.  无方法参数
d.  方法名称必须以test开头
e.  方法前面要加上特性[Test]
f.   测试类的前面要加上[TestFixture]

 

7.TestCast之间一定要保持完全的独立性,不允许出现在任何的依赖关系。

 

8.我们不能依赖于测试方法的执行顺序。

 

9.DRY(Don't  Repeat Yourself),当你的测试中有重复代码出现时,可以用SeTup和TearDown两个特性,表示
在每个测试方法执行前,调用什么,每个测试方法执行结束之后调用什么.
private int a;
复制代码
        private int b;

        
private Calc calc;

        [SetUp]
        
public void init()
        {
            calc 
= new Calc();
            a 
= 10;
            b 
= 2;
        }

        [TearDown]
        
public void Destroy()
        {

        }
复制代码
 

10.关于SetUp与TearDown方法的执行顺序:

a.  setup
b.  testAdd
c.  teardown
 

11.测试之前是什么状态,测试执行完毕后就应该是什么状态,而不应该由于测试执行的原因导致

状态发生了变化。
 

13.测试类的私有方法时可以采取两种方式:

a.修改方法的访问修饰符,将private修改为default或public(但不推荐使用)
b.使用反射在测试类中调用目标类的私有方法(推荐)。
[Test]
复制代码
        public void TestAdd()
        {
            
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[] { 23 });
                Assert.AreEqual(
5, result);

            }
            
catch (Exception ex)
            {
               Assert.Fail();
            }
        }
复制代码
 

14.Test Suite(测试套件):可以将多个测试组合到一起,同时执行多个测试。

[TestFixture]
复制代码
    public class AllTests
    {
        
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;
            }
        }
    }
复制代码
还有两种更为简单的方法:
其一:
复制代码
namespace CalcTest
{
    
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;
            }
        }
    }
}
复制代码
其二:
复制代码
namespace CalcTest
{
    
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两个特性:

[TestFixtureSetUp]
复制代码
        public void ConnectionSetup()
        {

        }

        [TestFixtureTearDown]
        
public void ConnectionDestroy()
        {

        }
复制代码
 

16.当在单元测试中,想不执行某个单元测试方法时,可以用Ignore特性,表示不永远不会被执行:

[Test]
复制代码
        [Ignore("This case is ignored.")]
        
public void TestSubtract()
        {
            
int actual = calc.Subtract(a, b);
            
int expect = 8;
            Assert.AreEqual(expect, actual);
        }
复制代码
 

17.当在单元测试中,如果不想对某个方法进行单元测试,只是在它被选中时才进行测试的话,可以调用Explicit特性

[Test]
复制代码
        [Explicit]
        
public void TestMultiple()
        {
            
int actual = calc.Multiple(a, b);
            
int expect = 20;
            Assert.AreEqual(expect, actual);
        }
复制代码
 

18.如果要对测试方法进行分组时,可以用Category特性

[Test]
复制代码
        [Category("GroupA")]
        
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.一个方法的测试可能要写很多个测试方法,这都是正常的:

一个冒泡排序类:
复制代码
namespace Calculator
{
    
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;
        }
    }
}
复制代码
一个测试冒泡排序的类,尽可能的考虑到所有的情况:
复制代码
namespace CalcTest
{
    
using NUnit.Framework;

    [TestFixture]
    
public class BubbleSortTest
    {
        BubbleSortClass bubble;

        [SetUp]
        
public void Init()
        {
            bubble 
= new BubbleSortClass();
        }

        [Test]
        
public void TestBubbleSort()
        {
            
int[] array = { 14849574-10 };
            
int[] result = bubble.BubbleSort(array);
            
int[] expect = { -1014445789 };

            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特性,它通常用于异常的测试:

[Test]
复制代码
        [ExpectedException(typeof(DivideByZeroException))]
        
public void TestDevideByZero()
        {
            
int actual = 0;

            actual 
= calc.Devide(20);
        }
复制代码
 

21.当你而要对一个测试方法进行多组数据测试时,可以用一个文件将数据保存起来,然后编写程序去读取

,达到多组数据测试的目的,方式比较灵活,可以用xml,txt等格式.
首先写一个要测试的类:
复制代码
namespace Calculator
{
    
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;
        }
    }
}
复制代码
然后写单元测试:
复制代码
namespace CalcTest
{
    
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强大.有的公司要求测试的覆盖率,TestDriven就提供了这个功能.
除些之外它还支持测试调试,这个功能还是不错的,.当你写的测试方法有问题时,它就派上用场了.
还可以直接在net输出窗口输出测试结果等.
博文出自:http://www.cnblogs.com/anllin/articles/2006181.html
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 乐教乐学孩子登陆你那忘记了怎么办 脸擦破了痂掉了留斑怎么办 挤黑头后鼻子又红又疼怎么办 香奈儿邂逅清新淡香水不喷怎么办 脚面被压了肿起来了怎么办 每天加班很累反而失眠严重怎么办 减肥药吃了口臭嘴巴苦怎么办 上火引起的牙疼怎么办吃什么药 舌苔厚口气重怎么办应该吃什么药 宝宝老是额头热四肢不热怎么办 考老师考砸了心理崩溃了怎么办 苹果手机一会白屏一会黑屏怎么办 360云盘的东西删不了怎么办 手机邮箱打开的文件疑似病毒怎么办 电脑qq发送的文件失效了怎么办 小米4c温控文件打开是乱码怎么办 超星尔雅用学号登录密码忘了怎么办 全脸做激光去黄褐斑后脸发红怎么办 上传到微云中的视频下载不了怎么办 微云保存的小电影下载不了怎么办 苹果手机下载有云朵下载不了怎么办 手机下载登录忘了密码了怎么办 软软件被手机加密忘了密码怎么办 苹果手机想下载东西忘了密码怎么办 已经不念书几年了突然想上学怎么办 江湖风云录把王老爷子杀了怎么办 练扫踢胫骨旁边的肌肉受伤了怎么办 四个月宝宝没抱住摔了头部怎么办 老公老是跟年轻的小姑娘聊天怎么办 老婆出轨老公想离婚又舍不得怎么办 孕妇打完无痛分娩针就想睡觉怎么办 熟食店开空调菜品吹的很干怎么办 不锈钢锅在液化气烧了发黄怎么办 在小镇门面卤菜店不好卖怎么办? 被辣椒辣到嘴唇了该怎么办 沁园净水机不制水指示灯不亮怎么办 太辣了辣得胃疼怎么办 出现连接问题或mmi码无效怎么办 存折丢了怎么办卡号也不记得了 车内皮子被烂苹果腐蚀有印怎么办 锅被腐蚀后变黑色应该怎么办