简体   繁体   English

C#-nunit / mstest等效于junit的@RunWith(HierarchicalContextRunner.class)

[英]C# - nunit/mstest equivalent to junit's @RunWith(HierarchicalContextRunner.class)

I've been following a unit test tutorial and the code is written in java, the tests are hierachical and I can't translate them to c# without knowing whats the equivalent of @RunWith(HierarchicalContextRunner.class) in nunit and/or mstest. 我一直在遵循单元测试教程,并且代码是用Java编写的,测试是分层的,如果不知道nunit和/或mstest中的@RunWith(HierarchicalContextRunner.class)等效,我将无法将它们转换为c#。

I've been thinking about inheritance, but I'm not sure thats the right answer. 我一直在考虑继承,但是我不确定那是正确的答案。

I don't think there is a way to replace runners in either NUnit or MSTest, but here is the way I am doing it with NUnit. 我不认为可以用NUnit或MSTest替换流道,但是这是我用NUnit进行替换的方法。

First, you need NUnit >= 2.5 so that SetUp methods are run from the top of the inheritance hierarchy to the current test class. 首先,您需要NUnit> = 2.5,以便SetUp方法从继承层次结构的顶部运行到当前的测试类。 Then: 然后:

public abstract class Given_An_Engine {
    protected Engine _engine;

    [SetUp]
    public void GivenAnEngine() {
        _engine = new Engine();
    }

    public abstract class That_Is_Stopped : Given_An_Engine {
        [SetUp]
        public void ThatIsStopped() {
            _engine.State = STOPPED;
        }

        public class When_I_Open_The_Status_UI : That_Is_Stopped {
            public StatusUI _ui; // You'll learn better ways than making _ui public. This is just for shortness.

            [SetUp]
            public void WhenIOpenTheStatusUI() {
              _ui = _engine.OpenUI();
            }

            [TestFixture]
            public class Behaves_Like_Display_Of_Stopped_Engine
                : Behaves_Like_Display_Of_Stopped_Engine<When_I_Open_The_Status_UI> {}
        }
    }
}

public class Behaves_Like_Display_Of_Stopped_Engine<TContext> 
    where TContext: Given_An_Engine.That_Is_Stopped.When_I_Open_The_Status_UI, new() {

    TContext ctx = new TContext();

    [SetUp]
    public void SetUp() {
        ctx.RunSetUp();
    }

    [TearDown]
    public void TearDown() {
        ctx.RunTearDown();
    }

    [Test]
    public void Displays_Stopped() {
       Assert.AreEqual("Stopped", ctx._ui.lblStatus.Text);
    }

    [Test]
    public void Displays_A_Red_Lamp() {
       Assert.AreEqual(Color.Red, ctx._ui.LampColor);
    }
}

The RunSetUp and RunTearDown methods are extension methods on object that you should try to write yourself, they're only 30-ish lines long. RunSetUpRunTearDown方法是您应该尝试自己编写的object扩展方法,它们只有30余行。 RunSetUp should run all SetUp methods from the top of the hierarchy down to the objects's class, and RunTearDown should do just the reverse. RunSetUp应该运行从层次结构顶部一直到对象的类的所有SetUp方法,而RunTearDown应该执行相反的操作。 I leave it as an exercice to you. 我把它留给你练习。

RunSetUp and RunTearDown also allow you to compose your test classes. RunSetUpRunTearDown还允许您RunTearDown测试类。

public abstract Given_Two_Engines {
    protected Given_An_Engine engine1 = new Given_An_Engine.That_Is_Stopped();
    protected Given_An_Engine engine2 = new Given_An_Engine.That_Is_Stopped();

    [SetUp]
    public void GivenTwoEngines() {
        engine1.RunSetUp();
        engine2.RunSetUp();
    }

    [TearDown]
    public void UnGivenTwoEngines() {
        engine2.RunTearDown();
        engine1.RunTearDown();
    }

Try it, and let me know what you think of the resulting names of the fixtures in the NUnit GUI. 尝试一下,让我知道您对NUnit GUI中的灯具产生的名称有何看法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM