xUnit.net - 运行code之前曾经和所有测试后 [英] xUnit.net - run code once before and after ALL tests

查看:586
本文介绍了xUnit.net - 运行code之前曾经和所有测试后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR - 我在寻找的xUnit中的等价物的MSTest的 AssemblyInitialize (又名一个特点它有我喜欢)

TL;DR - I'm looking for xUnit's equivalent of MSTest's AssemblyInitialize (aka the ONE feature it has that I like).

具体来说,我正在寻找它,因为我有一些硒烟雾测试,我希望能够与任何其他的依赖运行。我有一个夹具,将推出IisEx preSS,我把它杀了处置。但是,这样做的每一个测试非常腌运行之前。

Specifically I'm looking for it because I have some Selenium smoke tests which I would like to be able to run with no other dependencies. I have a Fixture that will launch IisExpress for me and kill it on disposal. But doing this before every test hugely bloats runtime.

我想在测试开始,一旦触发此code和处理它(关闭进程)结尾。我怎么能去这样做呢?

I would like to trigger this code once at the start of testing, and dispose of it (shutting down the process) at the end. How could I go about doing that?

即使我能得到的东西像我多少测试,目前正在运行可以计算出来的东西编程访问。

Even if I can get programmatic access to something like "how many tests are currently being run" I can figure something out.

推荐答案

随着2015年11月的xUnit 2已经出来了,所以要测试之间共享功能的正规途径。据记载 href=\"https://xunit.github.io/docs/shared-context.html#collection-fixture\">。

As of Nov 2015 xUnit 2 is out, so there is a canonical way to share features between tests. It is documented here.

基本上,你需要创建一个类做夹具:

Basically you'll need to create a class doing the fixture:

    public class DatabaseFixture : IDisposable
    {
        public DatabaseFixture()
        {
            Db = new SqlConnection("MyConnectionString");

            // ... initialize data in the test database ...
        }

        public void Dispose()
        {
            // ... clean up test data from the database ...
        }

        public SqlConnection Db { get; private set; }
    }

一个哑类轴承 Col​​lectionDefinition 属性。
此类允许的xUnit创建测试集合,并且将使用给定夹具集合中的所有测试类。

A dummy class bearing the CollectionDefinition attribute. This class allows Xunit to create a test collection, and will use the given fixture for all test classes of the collection.

    [CollectionDefinition("Database collection")]
    public class DatabaseCollection : ICollectionFixture<DatabaseFixture>
    {
        // This class has no code, and is never created. Its purpose is simply
        // to be the place to apply [CollectionDefinition] and all the
        // ICollectionFixture<> interfaces.
    }

然后,你需要在所有测试类添加集合名称。
测试类可以通过构造函数接收夹具。

Then you need to add the collection name over all your test classes. The test classes can receive the fixture through the constructor.

    [Collection("Database collection")]
    public class DatabaseTestClass1
    {
        DatabaseFixture fixture;

        public DatabaseTestClass1(DatabaseFixture fixture)
        {
            this.fixture = fixture;
        }
    }

这是一个有点比MsTests更详细的 AssemblyInitialize 因为你必须申报其所属的测试集合中的每个测试类,但它也更modulable(与MsTests你还需要把TestClass的上你的类​​)

It's a bit more verbose than MsTests AssemblyInitialize since you have to declare on each test class which test collection it belongs, but it's also more modulable (and with MsTests you still need to put a TestClass on your classes)

注:该样本已经从文档

这篇关于xUnit.net - 运行code之前曾经和所有测试后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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