繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> Asp.Net开发 >> NUnit简短使用说明

NUnit简短使用说明

2007-09-15 08:00:00  作者:  来源:互联网  浏览次数:0  文字大小:【】【】【
简介:namespace UnitTestingExamples { using System; using NUnit.Framework; /**//// /// TestFixture attribute主要是用在class上,其作用是标志该class含有需要执行的test methods。 /// 当你在一个class的定义里...

namespace UnitTestingExamples

{

using System;

using NUnit.Framework;

/**////

/// TestFixture attribute主要是用在class上,其作用是标志该class含有需要执行的test methods。

/// 当你在一个class的定义里加上这个attribute,Test Runner就会检查该class,看看这个class是否含有

/// test methods

///

[TestFixture]

public class SomeTests

{

/**////

/// 这两个主要用在TestFixture里面,其作用是提供一组函数执行任何测试运行之前(TestFixtureSetUP)和

/// 最后一个测试执行后(TestFixtureTearDown)。每一个TestFixture只能有一个TestFixtureSetUp方法和

/// TestFixtureTearDown方法。如果一个以上的TestFixtureSetUp和TestFixtureTearDown方法,

/// 可以通过编译但是不会执行。注意一个TestFixture可以拥有一个TestFixtureSetUp和一个SetUp,

/// 也可以拥有一个TestFixtureTearDown和一个TearDown方法。

/// TestFixtureSetUp 和 TestFixtureTearDown 被用在不方便使用SetUp和TearDown方法。

/// 一般情况使用 SetUp 和TearDown attributes。

///

[TestFixtureSetUp]

public void RunBeforeAllTests()

{

Console.WriteLine( "TestFixtureSetUp" );

}

[TestFixtureTearDown]

public void RunAfterAllTests()

{

Console.WriteLine( "TestFixtureTearDown" );

}

[SetUp]

public void RunBeforeEachTest()

{

Console.WriteLine( "SetUp" );

}

[TearDown]

public void RunAfterEachTest()

{

Console.WriteLine( "TearDown" );

}

/**////

/// Test attribute主要用来标示在text fixture中的method,表示这个method需要被Test Runner application

/// 所执行。有Test attribute的method必须是public的,并且必须return void,也没有任何传入的参数。

/// 如果没有符合这些规定,在Test Runner GUI之中是不会列出这个method的,而且在执行Unit Test的时候也不

/// 会执行这个method。上面的程序代码示范了使用这个attribute的方法。

///

[Test]

public void Test1()

{

Console.WriteLine( "Test1" );

}

/**////

/// 有的时候,你希望你的程序在某些特殊的条件下会产生一些特定的exception。要用Unit Test来测试程序是

/// 否如预期的产生exception,你可以用一个try..catch的程序区段来catch(捕捉)这个exception,然后再设一

/// 个boolean的值来证明exception的确发生了。这个方法固然可行,但是太花费功夫。事实上,你应该使用这

/// 个ExpectedException attribute来标示某个method应该产生哪一个exception

///

[Test]

[ExpectedException(typeof(InvalidOperationException))]

public void Test2()

{

// Do something that throws an InvalidOperationException

}

/**////

/// 这个attribute你大概不会经常用的,但是一旦需要的时候,这个attribute是很方便使用的。你可以使用这个

/// attribute来标示某个test method,叫Test Runner在执行的时候,略过这个method不要执行。

///

[Test]

[Ignore("We’re skipping this one for now.")]

public void TestOne()

{

// Do something

}

}

}

责任编辑:admin
相关文章