有没有一种方法,以有一次,每类,而不是每一次运行空间一SetUpFixture? [英] Is there a way to have a SetUpFixture that runs once per class instead of once per namespace?

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

问题描述

场景

:首先,我是新来的测试 - 所以请多多包涵。我里面的测试项目的,有一个控制器文件夹。该控制器文件夹可能包含一个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的设置里面做的事情是实例化一个控制器。当然,我可以实例在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文件到自己的文件夹,所以他们有不同的命名空间。从技术上解决了这一问题,它只是似乎有点丑有一堆的子文件夹中,只有1他们每个人的项目。丑陋,不再符合我的父项目的结构我去了。但是 - 也许这是要走的路

  • 未实例化我的控制器在 SetUpFixture 的安装程序,而不是做它在的TestFixture 设置,使一个新的实例得到每次测试(例如: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 上的方法在你的控制器类:

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是的代名词。

Here, "TestFixture" is synonymous with class.

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

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