Run code once before and after ALL tests in xUnit.net

后端 未结 8 858
野趣味
野趣味 2020-12-02 15:15

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

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-02 15:54

    I use AssemblyFixture (NuGet).

    What it does is it provides an IAssemblyFixture interface that is replacing any IClassFixture where you want the object's lifetime to be as the testing assembly.

    Example:

    public class Singleton { }
    
    public class TestClass1 : IAssemblyFixture
    {
      readonly Singletone _Singletone;
      public TestClass1(Singleton singleton)
      {
        _Singleton = singleton;
      }
    
      [Fact]
      public void Test1()
      {
         //use singleton  
      }
    }
    
    public class TestClass2 : IAssemblyFixture
    {
      readonly Singletone _Singletone;
      public TestClass2(Singleton singleton)
      {
        //same singleton instance of TestClass1
        _Singleton = singleton;
      }
    
      [Fact]
      public void Test2()
      {
         //use singleton  
      }
    }
    

提交回复
热议问题