title: XUnit CollectionFixture instantiating multiple times
body: <p>I'm just getting started with <a href="https://xunit.net/docs/shared-context#class-fixture" rel="nofollow noreferrer"><code>ClassFixtures</code></a> and collections in <a href="https://xunit.net/" rel="nofollow noreferrer">xUnit</a> so its possible I'm doing this wrong, but my understanding is that I can create a <code>ICollectionFixture&lt;MyFixture&gt;</code>, assign it to a collection via the <code>CollectionDefinitionAttribtue</code> and then all the test classes that have the <code>Collection</code> attribute with the same connection name will share the same instance of the fixture.</p>
  <p>First a simple case to prove that a <code>Fixture</code> is only instantiating once per test class</p>
  <pre class="lang-cs prettyprint-override"><code>public class SampleFixture:IDisposable
  {
      public static int ConstructorCount { get; set; }
      public static int DisposeCount { get; set; }
  
      public SampleFixture()
      {
          ConstructorCount++;
      }
  
  
      public void Dispose()
      {
          DisposeCount++;
      }
  }
  
  public class SampleTestClass1 : IClassFixture&lt;SampleFixture&gt;
  {
      private readonly ITestOutputHelper outputHelper;
      private readonly SampleFixture sampleFixture;
  
      public SampleTestClass1(ITestOutputHelper outputHelper, SampleFixture sampleFixture)
      {
          this.outputHelper = outputHelper;
          this.sampleFixture = sampleFixture;
      }
      [Fact]
      public void Test1()
      {
          this.outputHelper.WriteLine($&quot;Test1 - Constructor Count: {SampleFixture.ConstructorCount} DisposeCount: {SampleFixture.DisposeCount}&quot;);
          Assert.Equal(1,SampleFixture.ConstructorCount);
      }
  
      [Fact]
      public void Test2()
      {
          this.outputHelper.WriteLine($&quot;Test2 - Constructor Count: {SampleFixture.ConstructorCount} DisposeCount: {SampleFixture.DisposeCount}&quot;);
          Assert.Equal(1,SampleFixture.ConstructorCount);
          
      }
  }
  </code></pre>
  <p>Run the tests, and they both pass. 1 pass through the constructor means both tests in the test class are using the same instance of my fixture.  Cool beans.</p>
  <p>Now let's create a <code>CollectionFixture</code>, add another test class and decorate both with the same <code>Collection</code> attribute.</p>
  <pre class="lang-cs prettyprint-override"><code>[CollectionDefinition(&quot;SampleFixture&quot;)]
  public class SampleFixtureCollection : ICollectionFixture&lt;SampleFixture&gt;
  {
      
  }
  public class SampleFixture:IDisposable
  {
      public static int ConstructorCount { get; set; }
      public static int DisposeCount { get; set; }
  
      public SampleFixture()
      {
          ConstructorCount++;
      }
  
  
      public void Dispose()
      {
          DisposeCount++;
      }
  }
  
  [Collection(&quot;SampleFixture&quot;)]
  public class SampleTestClass1 : IClassFixture&lt;SampleFixture&gt;
  {
      private readonly ITestOutputHelper outputHelper;
      private readonly SampleFixture sampleFixture;
  
      public SampleTestClass1(ITestOutputHelper outputHelper, SampleFixture sampleFixture)
      {
          this.outputHelper = outputHelper;
          this.sampleFixture = sampleFixture;
      }
      [Fact]
      public void Test1()
      {
          this.outputHelper.WriteLine($&quot;Test1 - Constructor Count: {SampleFixture.ConstructorCount} DisposeCount: {SampleFixture.DisposeCount}&quot;);
          Assert.Equal(1,SampleFixture.ConstructorCount);
      }
  
      [Fact]
      public void Test2()
      {
          this.outputHelper.WriteLine($&quot;Test2 - Constructor Count: {SampleFixture.ConstructorCount} DisposeCount: {SampleFixture.DisposeCount}&quot;);
          Assert.Equal(1,SampleFixture.ConstructorCount);
          
      }
  }
  
  [Collection(&quot;SampleFixture&quot;)]
  public class SampleTestClass2 : IClassFixture&lt;SampleFixture&gt;
  {
      private readonly ITestOutputHelper outputHelper;
      private readonly SampleFixture sampleFixture;
  
      public SampleTestClass2(ITestOutputHelper outputHelper, SampleFixture sampleFixture)
      {
          this.outputHelper = outputHelper;
          this.sampleFixture = sampleFixture;
      }
      [Fact]
      public void Test1()
      {
          this.outputHelper.WriteLine($&quot;Test1 - Constructor Count: {SampleFixture.ConstructorCount} DisposeCount: {SampleFixture.DisposeCount}&quot;);
          Assert.Equal(1,SampleFixture.ConstructorCount);
      }
  
      [Fact]
      public void Test2()
      {
          this.outputHelper.WriteLine($&quot;Test2 - Constructor Count: {SampleFixture.ConstructorCount} DisposeCount: {SampleFixture.DisposeCount}&quot;);
          Assert.Equal(1,SampleFixture.ConstructorCount);
          
      }
  }   
  </code></pre>
  <p>Now all my tests fail.  Check out this message from the failed test in the 2nd test class:</p>
  <blockquote>
  <p>Test1 - Constructor Count: 3 DisposeCount: 1</p>
  </blockquote>
  <p>It appears the constructor is called <strong>3 times!</strong>  Once during the fixture collection startup, and then once more for each test class.</p>
  <p>What gives here?  Both my tests are tied to the same collection, but each test class is getting its own instance of the test fixture.</p>
  <p>Have I done something wrong here or is this a bug? (I found no mention of it in GitHub).</p>
  <p>Using xUnit v2.4.1  with .net core 2.1</p>