Nunit 学习

该内容主要参考官方帮助文档:

断言内容:

1、同等断言

主要涉及方法Assert.AreEqual、Assert.AreNotEqual

2、一致性断言

主要方法:Assert.AreSame、Assert.AreNotSame、Assert.Contains

3、比较断言

主要方法:Assert.Greater、Assert.GreaterOrEqual、Assert.Less、Assert.LessOrEqual

4、类型断言

主要方法:Assert.IsInstanceOf、Assert.IsNotInstanceOf、Assert.IsInstanceOfType、Assert.IsNotInstanceOfType、Assert.IsAssignableFrom、Assert.IsNotAssignableFrom

5、条件测试

主要方法:Assert.IsTrue、Assert.IsFalse、Assert.IsNull、Assert.IsNotNull、Assert.IsNaN、Assert.IsEmpty、Assert.IsNotEmpty、

6、字符串断言

常用方法:StringAssert.Contains、StringAssert.DoesNotContain、StringAssert.AreEqualIgnoringCase、StringAssert.AreNotEqualIgnoringCase、StringAssert.DoesNotEndWith、StringAssert.DoesNotMatch、StringAssert.DoesNotStartWith、StringAssert.EndsWith、StringAssert.IsMatch等

7、异常断言

常用方法:Assert.Throws、Assert.DoesNotThrow、Assert.Catch

8、文件断言

常用方法:

FileAssert.AreEqual、FileAssert.AreNotEqual

9、目录断言

常用方法:DirectoryAssert.AreEqual、DirectoryAssert.AreNotEqual、DirectoryAssert.IsEmpty、DirectoryAssert.IsNotEmpty、DirectoryAssert.IsWithin、DirectoryAssert.IsNotWithin

 

属性:

1、TestFixture

这个属性标记一个类包含了测试,而且可选的还有setup或teardown方法。

作为一个测试fixture的类有一些限制。

  • 必须是一个公共的导出类型,否则NUnit不会识别它。
  • 它必须有一个缺省的构造子,否则Nunit不能构建他。
  • 构造子不应该有任何方面的负面影响,因为在一个对话的过程中,NUnit可能构造类多次。

using System;
using NUnit.Framework;
namespace NUnit.Tests
{
  [TestFixture]
  public class Test
  {
    // ...
  }
}

2、Test

Test属性标记某个类的某个方法为一个测试方法,此类已经标记为一个TestFixture。为了与较早的NUnit版本向后兼容,可以发现测试方法的头4个字母“test”是不考虑大小的。

一个测试方法的签名定义如下:

public void MethodName();

注意这里必须没有参数。如果程序员将测试方法标记为不正确的签名,它不会运行,而且会出现在运行程序的UI的TestNotRun区域。

using System;
using NUnit.Framework;
namespace NUnit.Tests
{
  [TestFixture]
  public class Test
  {
[Test]

 public void Add()
 {
 
 }
  }
}

3、SetUp

本属性在一个TestFixture里使用,并提供一组常用的功能,这些功能是在每个测试方法被调用之前来完成的。一个TestFixture可以仅有一个SetUp方法。如果有多个定义,TestFixture也会编译成功,但是测试不会运行。

using System;
using NUnit.Framework;
namespace NUnit.Tests
{
  [TestFixture]
  public class Test
  {
 [SetUp]
 public void Init()
 {
 
 }
 [Test]
 public void Add()
 {
 
 }
  }
}

4、TearDown

NUnit 2.5之前:一个TestFixture可能只有一个TearDown方法,它需要一个实例方法。

NUnit 2.5之后:TearDown方法可以是静态或实例方法,你可以定义一个以上TearDown方法。通常情况下,多个TearDown方法是只定义在不同级别的一个继承层次结构,只要SetUp方法运行时不会出错,TearDown方法能保证运行。若SetUp失败,它不会运行或者抛出一个异常。。

using System;
using NUnit.Framework;
namespace NUnit.Tests
{
  [TestFixture]
  public class Test
  {
 [SetUp]
 public void Init()
 {
 
 }
 [TearDown]
 public void Cleanup()
    {
  
 }
 [Test]
 public void Add()
 {
 
 }
  }
}

5、Ignore

Ignore属性一段时间内不会运行一个测试或者测试fixture。人们可以将一个测试或测试Fixture标记为Ignore属性。正在运行的程序看见了这个属性,不会执行一个或多个测试。

这个特性用来暂时不运行一个测试或fixture。比起注释掉测试或重命名方法,这是一个比较好的机制,因为测试会和余下的代码一起编译,而且在运行时有一个不会运行测试的标记,这样保证不会忘记测试。

using System;
using NUnit.Framework;
namespace NUnit.Tests
{
  [TestFixture]
  public class Test
  {
  [Ignore("Ignore a fixture")]
   public class Test
   {
  // ...
   }
  }
}

6、Explicit

与Ignore有细微差别,如果不显示指定或选择,该方法不会运行,被标记为黄色,在显示指定是会照常运行

using System;
using NUnit.Framework;
namespace NUnit.Tests
{
  [TestFixture]
  public class Test
  {
  [Test,Explicit]
   public class Test
   {
  // ...
   }
  }
}

7、ExpectedException

期望抛出的异常。如果方法没有抛出异常或者跑出了其他异常,则测试不通过

using System;
using NUnit.Framework;
namespace NUnit.Tests
{
  [TestFixture]
  public class Test
  {
  [Test]
  [ExpectedException(typeof(InvalidOperationException))]
   public class Test
   {
  // ...
   }
  }
}

8、Platform

平台属性,可以使用Exclude与Include,也可以直接写字符串

using System;
using NUnit.Framework;
namespace NUnit.Tests
{
  [TestFixture]
  public class Test
  {
  [Test]
  [Platform(Exclude="Win98,WinME")]
   public class Test
   {
  // ...
   }
  }
}

9、TimeOut

Platform Specifiers

The following values are recognized as platform specifiers. They may be expressed in upper, lower or mixed case.

  • Win
  • Win32
  • Win32S
  • Win32Windows
  • Win32NT
  • WinCE
  • Win95
  • Win98
  • WinMe
  • NT3
  • NT4
  • NT5
  • NT6
  • Win2K
  • WinXP
  • Win2003Server
  • Vista
  • Win2008Server
  • Win2008ServerR2
  • Windows7
  • Win2012Server
  • Windows8
  • Unix
  • Linux
  • Net
  • Net-1.0
  • Net-1.1
  • Net-2.0
  • Net-3.0 (1)
  • Net-3.5 (2)
  • Net-4.0
  • NetCF
  • SSCLI
  • Rotor
  • Mono
  • Mono-1.0
  • Mono-2.0
  • Mono-3.0 (3)
  • Mono-3.5 (4)

 

 

 

 

 

 

Notes:

  1. Includes Net-2.0
  2. Includes Net-2.0 and Net-3.0
  3. Includes Mono-2.0
  4. Includes Mono-2.0 and Mono-3.0

设定超时时间,毫秒为单位,如果超过规定时间还未完成该方法,则会标记失败

using System;
using NUnit.Framework;
namespace NUnit.Tests
{
  [TestFixture]
  public class Test
  { 
  [Test, Timeout(2000)]
   public class Test
   {
  // ...
   }
  }
}

10、MaxTime

设置最大时间,毫秒为单位,如果超过最大时间还未完成该方法,则标记为失败

using System;
using NUnit.Framework;
namespace NUnit.Tests
{
  [TestFixture]
  public class Test
  { 
  [Test, MaxTime(2000)]
   public class Test
   {
  // ...
   }
  }
}

11、Description

貌似就是一个描述性的语言,文本出现在XML输出文件并显示在测试属性对话框。

using System;
using NUnit.Framework;
namespace NUnit.Tests
{
   [TestFixture, Description("Fixture description here")]
  public class Test
  { 
  [Test, MaxTime(2000),Description("Hello World")]
   public class Test
   {
  // ...
   }
  }
}

#####################################################################################################################################

以后再写了

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值