NUnit使用基础

TDD开发变得越来越流行,这里写关于NUnit的一些基本使用。

1、下载NUnit地址:http://www.nunit.org/downloads/NUnit-2.2.0.msi
2、安装NUnit后,组件会自动安装到GAC中,所以可以在直接在组件引用中添加,当然也可以直接引用该文件C:\Program Files\NUnit 2.2\bin\nunit.framework.dll
3、为了在调试中直接启用NUnit,要在测试用例所在的项目进行设置。在项目属性中设置调试模式为“程序”,设置启动应用程序为“C:\Program Files\NUnit 2.2\bin\nunit-gui.exe”,这样每次调试测试用例时都会自动启动NUnit的图形界面了。
4、基本使用,这里我设计了一个简单的例子,是关于Flags的使用的,使用Flags定义的枚举可以方便实现一些简单的权限管理。

None.gif using  System;
None.gif
using  NUnit.Framework;
None.gif
None.gif
namespace  RuiYa.Test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [TestFixture]
InBlock.gif    [Category(
"Flags用法测试")]
InBlock.gif    
public class NUnitDemo
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private TestType test;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public NUnitDemo()dot.gif{}
InBlock.gif
InBlock.gif        [SetUp]
InBlock.gif        
public void RuiYaSetUp()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            test 
= Add(test, TestType.View);
InBlock.gif            Assert.AreEqual(
"View", test.ToString());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [TearDown]
InBlock.gif        
public void RuiYaTearDown()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            test 
= Remove(test, TestType.View, TestType.Edit, TestType.Delete, TestType.Create);
InBlock.gif            Assert.AreEqual(
"0", test.ToString());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Test]
InBlock.gif        
public void TestEnum()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Assert.AreEqual(
"View", test.ToString());
InBlock.gif
InBlock.gif            test 
= Add(test, TestType.Edit, TestType.Delete);
InBlock.gif            Assert.AreEqual(
"View, Edit, Delete", test.ToString());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private TestType Add(TestType source, params TestType[] testTypes)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if((testTypes != null&& testTypes.Length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
foreach(TestType tt in testTypes)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    source 
|= (TestType)tt;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return source;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private TestType Remove(TestType source, params TestType[] testTypes)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if((testTypes != null&& testTypes.Length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
foreach(TestType tt in testTypes)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    source 
= source & (~tt);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return source;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    [Flags]
InBlock.gif    
public enum TestType : int
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        View        
= 1,
InBlock.gif        Edit        
= 2,
InBlock.gif        Delete        
= 4,
InBlock.gif        Create        
= 8,
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

#  如果要对某个测试用例类进行测试,则必须为这个类添加[TestFixture]属性
#  使用[Category]属性,可以把这个测试加入某个测试种类中,凡是属于某个测试种类的测试均可以集中一起进行测试,这个[Category]属性不只是可以添加到类上,也可以添加到方法上,这样可以单独把某个方法加入到某个测试种类中。
#  使用[SetUp]属性标记的方法,是在测试前期对测试环境进行初始化,而[TearDown]则在测试结束后运行,提供机会还原到测试前的状态。
#  使用[Test]属性,则是真正需要进行测试的方法,具体的测试都是写在这一个个的方法体内。
#Assert是主要的断言类,其中提供很多方法帮助我们对代码进行测试。


转载于:https://www.cnblogs.com/BillChen/archive/2005/09/20/240193.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值