有没有办法让 SetUpFixture 每个类运行一次而不是每个命名空间运行一次? [英] Is there a way to have a SetUpFixture that runs once per class instead of once per namespace?

查看:26
本文介绍了有没有办法让 SetUpFixture 每个类运行一次而不是每个命名空间运行一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Scenario
First of all, I'm new to testing - so please bear with me. Inside of my test project, there is a Controllers folder. The Controllers folder may contain a ControllerATest.cs, ControllerBTest.cs, and ControllerCTest.cs. Because my namespace aligns with my folder structure, they all share the namespace MyProject.Tests.Controllers.

从我在 NUnit SetUpFixture 文档中读到的,[SetUpFixture] 在这个命名空间内将为整个命名空间运行一次.也就是说,如果我一次运行所有控制器测试 - SetUpFixture 将只执行一次.

From what I've read in the NUnit SetUpFixture Documentation, a [SetUpFixture] inside this namespace will run once for the entire namespace. That is, if I run all of my controller tests at once - the SetUpFixture will be executed only once.

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

Problem
As I said, each controller test shares a namespace. SetUpFixtures apply to the entire namespace. What if I want each controller to have their own SetUpFixture? This is a problem when SetUpFixtures apply to entire namespaces. What I want is something that executes once, and not per-test. One of the things I do inside my SetUpFixture's SetUp is instantiate a controller. Sure, I could instantiate all 3 controllers in the SetUpFixture, but this seems ugly when maybe I am only testing ControllerC. That really doesn't seem clean. Therefore, I would like a SetUpFixture that applies only to the class it appears in, such as ControllerCTests.

据我所知,NUnit 似乎无法实现此特定功能.如果 NUnit 无法实现,那让我认为这不是一个常见的场景.如果这不是一个常见的场景,我做错了什么.我的问题是,什么?也许我的测试结构已关闭,需要更改.或者也许 可以使用 NUnit?

From what I've read, this specific functionality seems to be impossible with NUnit. And if it's not possible with NUnit, that makes me think it's not a common scenario. And if it's not a common scenario, I am doing something wrong. My question is, what? Maybe my testing structure is off and it needs to change. Or maybe it is possible with NUnit?

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

Code
An example of my desired structure:

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 个实例就可以逃脱惩罚,但我应该这样做.
  • Moving each .cs file into its own folder, so they have different namespaces. Technically this solves the problem, it just seems a little ugly to have a bunch of subfolders with only 1 item in each of them. Ugly, and will no longer match my parent project's structure which I was going for. But - maybe this is the way to go.
  • Not instantiating my controller in the SetUpFixture SetUp, and instead doing it in the TestFixture SetUp so that a new one gets instantiated for every test (ex: 5 tests = 5 separate instances) instead of once per class or namespace. Though it seems like if I can get away with only creating 1 instance, I should.

建议?

推荐答案

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

Use a TestFixtureSetUpAttribute on a method in your Controller class:

[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 内使用,以提供一组函数,这些函数在执行夹具中的任何测试之前执行一次.

This attribute is used inside a TestFixture to provide a single set of functions that are performed once prior to executing any of the tests in the fixture.

此处,TestFixture"与 class 同义.

Here, "TestFixture" is synonymous with class.

这篇关于有没有办法让 SetUpFixture 每个类运行一次而不是每个命名空间运行一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆