nunit的最精简教程

可能要用到。

 

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using NUnit.Framework;

namespace NUnit
ExpandedBlockStart.gifContractedBlock.gif
{
    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
        }

    }

    [TestFixture]
//声明类中包含测试
    public class TestCalculator
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
//公用的变量要写在外面避免重复
        private Calculator Cal;
        
private int a;
        
private int b;
        [SetUp]
        
protected void SetUp()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            a 
= 2;
            b 
= 4;
            Cal 
= new Calculator();
        }

        [Test]
        
public void SumTest()//测试方法推荐不带参数
ExpandedSubBlockStart.gifContractedSubBlock.gif
        {
            Assert.AreEqual(
6, Cal.Sum(a, b));
        }

        [Test]
        [ExpectedException(
typeof(DivideByZeroException))]
        [Ignore(
"Not yet !")]//完全忽略;[Explicit]是还可以手动执行的
        public void DivTest()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
int c = 0;
            Cal.Div(a, c);
            Assert.Fail(
"Got an expection !");
        }

        
//[TestFixtureSetUp][TestFixtureTearDown]共享和清除昂贵的资源(db)
        
//[Suite] case和其他suite的容器(支持级联的集合类)
        
//[Category]用于case分类,cmd中用include和exclude实现
        
//测试生命周期合约:
        
    }

    
//用反射得到类结构,然后用Attribute规划测试。
    
//目标类要public且有默认的构造函数(不要有副作用),否则nunit无法识别或构造。
    public class Calculator
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
public int Sum(int a, int b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return a + b;
        }

        
public int Div(int a, int b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return a / b;
        }

    }

}

 

 

转载于:https://www.cnblogs.com/hbreset/archive/2008/08/29/1279313.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值