【问题标题】:Is there a way to have a SetUpFixture that runs once per class instead of once per namespace?有没有办法让每个类运行一次而不是每个命名空间一次的 SetUpFixture?
【发布时间】:2014-06-03 05:19:18
【问题描述】:

场景
首先,我是测试新手——所以请多多包涵。在我的测试项目中,有一个 Controllers 文件夹。 Controllers 文件夹可能包含 ControllerATest.cs、ControllerBTest.cs 和 ControllerCTest.cs。因为我的命名空间与我的文件夹结构一致,所以它们都共享命名空间 MyProject.Tests.Controllers。

根据我在NUnit SetUpFixture Documentation 中阅读的内容,此命名空间内的 [SetUpFixture] 将运行一次整个命名空间。也就是说,如果我一次运行所有控制器测试 - SetUpFixture 将只执行一次。

问题
正如我所说,每个控制器测试共享一个命名空间。 SetUpFixtures 适用于整个命名空间。如果我希望每个控制器都有自己的自己的 SetUpFixture 怎么办?当 SetUpFixtures 应用于整个命名空间时,这是一个问题。我想要的是执行一次,而不是每次测试。我在 SetUpFixture 的 SetUp 中做的一件事是实例化一个控制器。当然,我可以在 SetUpFixture 中实例化所有 3 个控制器,但是当我可能只测试 ControllerC 时,这看起来很难看。这看起来真的不干净。因此,我想要一个 SetUpFixture,它仅应用于它出现的类,例如 ControllerCTests。

根据我的阅读,NUnit 似乎无法实现这一特定功能。如果 NUnit 不可能,那让我认为这不是一个常见的场景。如果这不是一个常见的情况,我做错了什么。我的问题是,什么?也许我的测试结构已经关闭,需要改变。或者也许它是 NUnit 可能的?

代码
我想要的结构的一个例子:

namespace MyProject.Tests.Controllers
{
public class ControllerATests
{
    private static IMyProjectRepository _fakeRepository;
    private static ControllerA _controllerA;

    [SetUpFixture]
    public class before_tests_run
    {
        [SetUp]
        public void ControllerASetup()
        {
            _fakeRepository = FakeRepository.Create();

            _controllerA = new ControllerA(_fakeRepository);
        }
    }

    [TestFixture]
    public class when_ControllerA_index_get_action_executes
    {
        [Test]
        public void it_does_something()
        {
            //
        }

        [Test]
        public void it_does_something_else()
        {
            //
        }
    }
}

public class ControllerBTests
{
    private static IMyProjectRepository _fakeRepository;
    private static ControllerB _controllerB;

    [SetUpFixture]
    public class before_tests_run
    {
        [SetUp]
        public void ControllerBSetup()
        {
            _fakeRepository = FakeRepository.Create();

            _controllerB = new ControllerB(_fakeRepository);
        }
    }

    [TestFixture]
    public class when_ControllerB_index_get_action_executes
    {
        [Test]
        public void it_does_something()
        {
            //
        }

        [Test]
        public void it_does_something_else()
        {
            //
        }
    }
}
}    

我尝试过的事情

  • 将每个 .cs 文件移动到其自己的文件夹中,因此它们具有不同的命名空间。从技术上讲,这解决了这个问题,只是有一堆子文件夹,每个子文件夹只有一个项目,这似乎有点难看。丑陋,将不再符合我想要的父项目结构。但是 - 也许这是要走的路。
  • 不在 SetUpFixture SetUp 中实例化我的控制器,而是在 TestFixture SetUp 中执行它,以便为每个测试实例化一个新控制器(例如:5 个测试 = 5 个单独的实例)而不是每个类或命名空间一次。虽然看起来我可以只创建 1 个实例,但我应该这样做。

建议?

【问题讨论】:

    标签: c# unit-testing nunit asp.net-mvc-5


    【解决方案1】:

    在 Controller 类中的方法上使用 TestFixtureSetUpAttribute:

    [TestFixture]
    public class when_ControllerA_index_get_action_executes
    {
        [TestFixtureSetUp]
        public void FixtureSetUp()
        {
            // this code runs once no matter how many tests are in this class
        }
    
        [Test]
        public void it_does_something()
        {
            // ...
        }
    }
    

    来自文档:

    此属性在 TestFixture 中使用,以提供一组功能,这些功能在执行夹具中的任何测试之前执行一次。

    这里,“TestFixture”是class的同义词。

    【讨论】:

    • 谢谢 - 我已经阅读了相关文档,但误解了它,认为它与 SetUpFixture 本质上是一样的。这是一个巨大的改进,因为每个 TestFixture 只运行一次,但它仍然与单个 TestFixture 相关联;理想情况下,它会绑定到班级级别 (public class ControllerATests)。例如,我为每个控制器拥有的每个类都包含 几个 TestFixture 类。因此,我希望我可以在整个班级而不是单个 TestFixture 上拥有它,但同样,仍然是一个改进。
    • 如果您使用的是 Nunit 3.0 或更高版本,请查看下面 Tyler 的回答。
    【解决方案2】:

    对于 NUnit 3.4.1 及更高版本,请使用 OneTimeSetUp:

    [TestFixture]
    public class MyTest
    {
        [OneTimeSetUp]
        public void Setup()
        {
            // one time setup code for this class
        }
    }
    

    顺便说一句,TestFixtureSetUp 已被弃用。

    【讨论】:

    • 谢谢,直观地说,等效的“per TestFixture”拆解属性是[OneTimeTearDown]。
    猜你喜欢
    • 2022-11-23
    • 2019-06-09
    • 1970-01-01
    • 2019-12-17
    • 2020-06-09
    • 1970-01-01
    • 2016-04-27
    • 2012-12-08
    • 2019-02-15
    相关资源
    最近更新 更多