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

后端 未结 8 857
野趣味
野趣味 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:49

    To execute code on assembly initialize, then one can do this (Tested with xUnit 2.3.1)

    using Xunit.Abstractions;
    using Xunit.Sdk;
    
    [assembly: Xunit.TestFramework("MyNamespace.MyClassName", "MyAssemblyName")]
    
    namespace MyNamespace
    {   
       public class MyClassName : XunitTestFramework
       {
          public MyClassName(IMessageSink messageSink)
            :base(messageSink)
          {
            // Place initialization code here
          }
    
          public new void Dispose()
          {
            // Place tear down code here
            base.Dispose();
          }
       }
    }
    

    See also https://github.com/xunit/samples.xunit/tree/master/AssemblyFixtureExample

提交回复
热议问题