xUnit.net:全球安装拆卸+? [英] xUnit.net: Global setup + teardown?

查看:371
本文介绍了xUnit.net:全球安装拆卸+?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这问题是关于单元测试框架 xUnit.net

This question is about the unit testing framework xUnit.net.

我需要在执行任何测试之前运行一些代码,以及一些代码,所有的测试完成后。我觉得应该有某种属性或标记接口指示全局初始化和终止代码,但无法找到他们。

I need to run some code before any test is executed, and also some code after all tests are done. I thought there should be some kind of attribute or marker interface to indicate the global initialization and termination code, but couldn't find them.

另外,如果我调用编程的xUnit ,我也可以做到什么,我想用下面的代码:

Alternatively, if I invoke xUnit programmatically, I can also achieve what I want with the following code:

static void Main()
{
    try
    {
        MyGlobalSetup();
        RunAllTests();  // What goes into this method?
    }
    finally
    {
        MyGlobalTeardown();
    }
}



谁能给我提供一个关于提示如何以声明或编程方式运行一些全局安装/拆卸代码?

Can anyone provide me a hint about how to declaratively or programmatically run some global setup/teardown code?

推荐答案

据我所知,的xUnit没有一个全局初始化/拆卸扩展点。然而,很容易创建一个。只要创建一个基础测试类实现的IDisposable ,做在构造函数初始化你在你的拆卸 IDisposable.Dispose 方法。这将是这样的:

As far as I know, xUnit does not have a global initialization/teardown extension point. However, it is easy to create one. Just create a base test class that implements IDisposable and do your initialization in the constructor and your teardown in the IDisposable.Dispose method. This would look like this:

public abstract class TestsBase : IDisposable
{
    protected TestsBase()
    {
        // Do "global" initialization here; Called before every test method.
    }

    public void Dispose()
    {
        // Do "global" teardown here; Called after every test method.
    }
}

public class DummyTests : TestsBase
{
    // Add test methods
}

然而,基类的建立和拆除代码将每个呼叫执行。这可能不是你想要的,因为它不是很有效。更优化的版本将使用 IClassFixture< T> 接口,以保证全局初始化/拆卸功能仅调用一次。对于这个版本,你不会从你的测试类扩展基类,但实施 IClassFixture< T> 接口,其中 T 指的是您的夹具类:

However, the base class setup and teardown code will be executed for each call. This might not be what you want, as it is not very efficient. A more optimized version would use the IClassFixture<T> interface to ensure that the global initialization/teardown functionality is only called once. For this version, you don't extends a base class from your test class but implement the IClassFixture<T> interface where T refers to your fixture class:

using Xunit;

public class TestsFixture : IDisposable
{
    public TestsFixture ()
    {
        // Do "global" initialization here; Only called once.
    }

    public void Dispose()
    {
        // Do "global" teardown here; Only called once.
    }
}

public class DummyTests : IClassFixture<TestsFixture>
{
    public void SetFixture(TestsFixture data)
    {
    }
}

这的将会的导致的构造 TestsFixture 只被运行一次
根据每类测试。因此,它取决于你想要究竟这两种方法之间做出选择。

This will result in the constructor of TestsFixture only being run once for every class under test. It thus depends on what you want exactly to choose between the two methods.

这篇关于xUnit.net:全球安装拆卸+?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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