【问题标题】:What is the NUnit framework lifecycle execution order?NUnit 框架生命周期执行顺序是什么?
【发布时间】:2018-01-28 14:07:16
【问题描述】:

最近,我在我的 ASP.NET MVC 项目中添加了一个基于 NUnit 框架(版本 3.7.1.0)的测试项目。我知道 NUnit 以及其他框架中有一些属性,我们可以将它们添加到我们的类和方法中,以便编写更好的测试用例。

以下是 NUnit 文档中介绍的属性:

  • 设置
  • 拆解
  • TestFixtureSetUp
  • TestFixtureTearDown

我想知道实现它的执行顺序和幕后的性能考虑。

【问题讨论】:

    标签: asp.net-mvc unit-testing testing nunit


    【解决方案1】:

    我想知道执行顺序

    TestFixture 相关的Setup/TearDown 方法对于TestFixture 只会执行一次。 为每个测试执行与Test 相关的设置/拆卸方法。这是中间有一个循环的顺序:

    TestFixtureSetUp    // executed once before first test SetUp
       SetUp            // excuted before *each* test
       TearDown         // executed after *each* test
    TestFixtureTearDown // executed once after last test TearDown
    

    我使用SetUp 方法将在测试的arrange 部分中重复的代码移到那里。通常你在SetUp 方法中初始化 SUT,方法是创建它并提供所需的模拟依赖项。

    TestFixtureSetUp 更加全球化——它为夹具中的所有测试准备了战场。通常,我用它来初始化一些环境——例如使用一些初始数据创建数据库和/或设置表。 TestFixtureTearDown 用于删除该数据库。当然,环境初始化与单元测试无关——它是用于验收测试。

    实现该功能的幕后性能考虑

    与任何其他方法调用相同。这取决于您将什么放入设置或拆卸方法中。

    【讨论】:

    • 请注意,TestFixtureSetUpTestFixtureTearDown 在 NUnit 3 中已弃用,取而代之的是 OneTimeSetUpOneTimeTearDown
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多