`
love19820823
  • 浏览: 974284 次
文章分类
社区版块
存档分类
最新评论

NUnit简短使用说明

 
阅读更多
namespaceUnitTestingExamples
{

usingSystem;
usingNUnit.Framework;

///<summary>
///TestFixtureattribute主要是用在class上,其作用是标志该class含有需要执行的testmethods。
///当你在一个class的定义里加上这个attribute,TestRunner就会检查该class,看看这个class是否含有
///testmethods
///</summary>

[TestFixture]
publicclassSomeTests
{
///<summary>
///这两个主要用在TestFixture里面,其作用是提供一组函数执行任何测试运行之前(TestFixtureSetUP)和
///最后一个测试执行后(TestFixtureTearDown)。每一个TestFixture只能有一个TestFixtureSetUp方法和
///TestFixtureTearDown方法。如果一个以上的TestFixtureSetUp和TestFixtureTearDown方法,
///可以通过编译但是不会执行。注意一个TestFixture可以拥有一个TestFixtureSetUp和一个SetUp,
///也可以拥有一个TestFixtureTearDown和一个TearDown方法。
///TestFixtureSetUp和TestFixtureTearDown被用在不方便使用SetUp和TearDown方法。
///一般情况使用SetUp和TearDownattributes。
///</summary>

[TestFixtureSetUp]
publicvoidRunBeforeAllTests()

{

Console.WriteLine(
"TestFixtureSetUp");

}


[TestFixtureTearDown]

publicvoidRunAfterAllTests()

{

Console.WriteLine(
"TestFixtureTearDown");

}


[SetUp]

publicvoidRunBeforeEachTest()

{

Console.WriteLine(
"SetUp");

}


[TearDown]

publicvoidRunAfterEachTest()

{

Console.WriteLine(
"TearDown");

}



///<summary>
///Testattribute主要用来标示在textfixture中的method,表示这个method需要被TestRunnerapplication
///所执行。有Testattribute的method必须是public的,并且必须returnvoid,也没有任何传入的参数。
///如果没有符合这些规定,在TestRunnerGUI之中是不会列出这个method的,而且在执行UnitTest的时候也不
///会执行这个method。上面的程序代码示范了使用这个attribute的方法。
///</summary>

[Test]
publicvoidTest1()

{

Console.WriteLine(
"Test1");

}


///<summary>
///有的时候,你希望你的程序在某些特殊的条件下会产生一些特定的exception。要用UnitTest来测试程序是
///否如预期的产生exception,你可以用一个try..catch的程序区段来catch(捕捉)这个exception,然后再设一
///个boolean的值来证明exception的确发生了。这个方法固然可行,但是太花费功夫。事实上,你应该使用这
///个ExpectedExceptionattribute来标示某个method应该产生哪一个exception
///</summary>

[Test]

[ExpectedException(
typeof(InvalidOperationException))]
publicvoidTest2()
{
//DosomethingthatthrowsanInvalidOperationException
}


///<summary>
///这个attribute你大概不会经常用的,但是一旦需要的时候,这个attribute是很方便使用的。你可以使用这个
///attribute来标示某个testmethod,叫TestRunner在执行的时候,略过这个method不要执行。
///</summary>

[Test]
[Ignore(
"We'reskippingthisonefornow.")]
publicvoidTestOne()
{
//Dosomething
}




}


}


分享到:
评论

相关推荐

    Cssss.rar_cssss

    - 使用 MSTest, NUnit 或 xUnit 等测试框架。 8. **遵循设计模式**: - 学习并应用常见的设计模式,如工厂模式、单例模式、观察者模式等,以提高代码的可扩展性和复用性。 至于压缩包中的 "www.pudn.com.txt" ...

    PhluffyFotos,一个图片上传和展示的程序

    3. **description**:这可能是一个文本文件,提供了关于PhluffyFotos的简短说明或者额外信息,比如开发者信息、版本历史等。 4. **C#**:这个文件名可能是被压缩的源代码文件夹,里面包含了PhluffyFotos的C#源代码...

    Visual.Build.Professional.v7.5_0.l.l.y.D.b.g.rar

    压缩包内的`file_id.diz`通常是文件发布者提供的简短说明文件,`inv.nfo`可能包含软件的详细信息、发布者声明或者版权信息。而`i-vb5700.r00`看起来像是一个分卷文件,可能是RAR格式的分割文件,用于大文件的分块...

    软件测试面试题集

    - **自动化**:使用单元测试框架(如JUnit、NUnit等)自动执行测试用例。 - **覆盖率**:确保测试覆盖了所有重要的代码路径。 - **持续集成**:将单元测试集成到持续集成流程中,确保代码质量。 #### 9. LoadRunner...

    Actual-FMP

    6. **测试文件**:C#项目通常会包含单元测试,使用 MSTest、NUnit 或 xUnit 框架。 7. **文档**:可能有README文件,提供了项目的简短介绍、安装指南和使用方法。 8. **构建脚本**:如MSBuild或dotnet CLI命令,...

    SitioWebPlatzi:SitioWebPlatzi

    10. **读取我(Readme)**:提供项目介绍、安装指南和使用说明。 11. **许可证文件(License)**:定义项目使用权限和条款。 从这个项目中,我们可以学习到: 1. **C#编程**:包括面向对象的概念,类、方法、属性...

Global site tag (gtag.js) - Google Analytics