Skip to content

Instantly share code, notes, and snippets.

@draptik
Created July 31, 2017 21:15
Show Gist options
  • Save draptik/e5619e3a77de6b31839876ee3882bad1 to your computer and use it in GitHub Desktop.
Save draptik/e5619e3a77de6b31839876ee3882bad1 to your computer and use it in GitHub Desktop.
xunit-net-collection-fixtures
using FluentAssertions;
using Xunit;
namespace FooTests
{
/// <summary>
/// https://xunit.github.io/docs/shared-context.html#collection-fixture
///
/// Important note: Fixtures can be shared across assemblies, but collection definitions must
/// be in the same assembly as the test that uses them.
/// </summary>
// Important: Remove call to IClassFixture when using Collection Fixtures!
[Collection("mycollection")]
public class Class1 // : IClassFixture<SomeFixture>
{
public Class1(SomeFixture fixture)
{
var foo = "called before every test in Class1"; // <- Breakpoint here
}
[Fact]
public void Test1()
{
1.Should().Be(1); // <- Breakpoint here
}
[Fact]
public void Test2()
{
1.Should().Be(1); // <- Breakpoint here
}
}
// Important: Remove call to IClassFixture when using Collection Fixtures!
[Collection("mycollection")]
public class Class2 //: IClassFixture<SomeFixture>
{
public Class2(SomeFixture fixture)
{
var foo = "called before every test in Class2"; // <- Breakpoint here
}
[Fact]
public void Test1()
{
1.Should().Be(1); // <- Breakpoint here
}
[Fact]
public void Test2()
{
1.Should().Be(1); // <- Breakpoint here
}
}
public class SomeFixture
{
public SomeFixture()
{
// The following break point should be hit only once!
var x = "Should only be called once!"; // <- Breakpoint here
}
}
[CollectionDefinition("mycollection")]
public class SomeCollection : ICollectionFixture<SomeFixture>
{
// marker class
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment