xUnit 2.x replacement for IUseFixture


I need to initialize my database for a bunch of tests and want to do that once for ALL my tests. It looks like for version 1.x, I would accomplish this with IUseFixture. However, IUseFixture seems to be missing from 2.0. Would this now be accomplished with Collections? Is there any sample code for how to setup a Collection? For this I would setup a collection with all my tests in it?

There is no mention of this on the migration page: http://xunit.net/docs/test-migration.html


schallm wrote Sep 2, 2014 at 10:06 PM

I have tried to use the sample found here: https://xunit.codeplex.com/SourceControl/latest#samples/CollectionFixtureExample/DatabaseCollection.cs

However, my test fails with:

The following constructor parameters did not have matching fixture data: DatabaseFixture configure
Public Class DatabaseFixture
    Public Sub New()
        Configure() ' I left out the implementation
    End Sub
End Class

Public Class DatabaseCollection
    Implements ICollectionFixture(Of DatabaseFixture)
End Class

Public Class InmateMediaPurchaseServiceTests

    Public Sub New(configure As DatabaseFixture)
    End Sub

    Public Sub Save()
    End Sub

End Class

RobertBWilliams wrote Oct 4, 2014 at 7:16 PM

The following code tests fine when run using the command prompt. But fails when run from VS2013 Test Explorer using xunit.runner.visualstudio 0.99.8

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace XunitPlayGround
public class DatabaseFixture : IDisposable
    public int Value { get; set; }
    public DatabaseFixture()
        Value = 42;

    public void Dispose()
        Value = 0;

public class DatabaseCollection : ICollectionFixture<DatabaseFixture>

public class XunitTest
    public DatabaseFixture Fixture { get; set; }

    public XunitTest(DatabaseFixture fixture)
        Fixture = fixture;

    public void Test()
        Assert.Equal(42, Fixture.Value);

TonyWallAtos wrote Jan 21, 2015 at 2:06 PM

I can confirm this works with the current preview version 2.0 RC build 2826.

At first I was annoyed about the addition of collection fixtures for no apparent benefit but now it supports start/stop of WCF services so I am happy, at least with XUnit.NET Visual Studio Runners (one of the two fixed it, whatever).

I look forward to the full release and proper documentation so we can see what the point was. IUseFixture<T> was pretty self explanatory and compiler checked.

bartelink wrote Jan 27, 2015 at 6:06 AM

@TonyWallAtos http://xunit.github.io/docs/shared-context.html any use? (If not, ya know what to do!)

CodeChief wrote Jan 27, 2015 at 9:55 AM

Well thanks for the link but the design still seems strange to me. The only purpose I can see is to force people to have stateless unit test classes, as they will be constructed multiple times when multiple fixtures are used.

Specifically regarding object lifetime and constructor parameters I'd like to see a section added to the documentation explaining step-by-step how multiple shared fixture combinations (in larger solutions) successfully initialize ONCE each per entire test run. If it is as I assume to make it difficult for people who try to add state to their unit test classes then I think it could have been achieved in a much more friendly way, no extra maintenance for those who do it correctly. The obvious way of course is stateless = static test methods only. If that's correct then make it so, dump all this extra decorative work so we have less maintenance effort.

Anyway I'm still behind XUnit and like the new runners/runtime implementation fixes, so will continue to use it so long as the maintenance effort does not increase too much.

bartelink wrote Jan 27, 2015 at 8:47 PM

@TonyWallAtos I'm not presently tracking xunit.net so don't have the info front of mind to respond.

This site is dead - only very strange people with opml files happen to see these posts. I'd suggest asking this as a github issue (potentially it might also make a good SO question if you can get some sample code together).

I'd also have a look at the tests and/or scenario tests for the feature - in general they are very well written and have the answers (I havent personally looked but the approach has never failed me in the xunit codebase)

taherchhabra wrote Mar 2, 2015 at 4:39 PM

How to configure when the DatabaseCollection class and DatabaseFixture class is in a different assembly