在xUnit.net中的所有测试之前和之后运行一次代码 [英] Run code once before and after ALL tests in xUnit.net

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

问题描述

TL; DR-我正在寻找xUnit等同于MSTest的AssemblyInitialize(又是我喜欢的一个功能).

特别是我正在寻找它,因为我有一些硒烟雾测试,希望能够在没有其他依赖项的情况下运行.我有一个Fixture,它将为我启动IisExpress并销毁它.但是,在每次测试前都要这样做会极大地破坏运行时.

我想在测试开始时触发一次此代码,然后在结束时将其处理(关闭过程).我该怎么做呢?

即使我可以通过编程方式访问当前正在运行多少个测试"之类的东西,我也可以弄清楚.

解决方案

截至2015年11月,xUnit 2已发布,因此有一种规范的方式可以在测试之间共享功能.在此处记录.

基本上,您需要创建一个执行夹具的类:

     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; }
    }
 

带有CollectionDefinition属性的伪类. 该类允许Xunit创建一个测试集合,并将给定的夹具用于该集合的所有测试类.

     [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.
    }
 

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

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

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

它比MsTests AssemblyInitialize更为冗长,因为您必须在每个测试类上声明它属于哪个测试集合,但它也更具可调节性(对于MsTests,您仍然需要在类上放置一个TestClass)

注意:示例取自文档

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

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.

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.

解决方案

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; }
    }

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;
        }
    }

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)

Note: the samples have been taken from the documentation.

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

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