Create a gist now

Instantly share code, notes, and snippets.

Data-driven tests with NUnit and XUnit when you have a Specification base class
public abstract class Specification
{
[Test]
public virtual void Run()
{
this.BDDfy();
}
[Fact]
public void RunX()
{
this.BDDfy();
}
}
public class NormalSpec : Specification
{
public void GivenX()
{
}
public void WhenY()
{
}
public void ThenZ()
{
}
}
public class DataDrivenSpec : Specification
{
private int _x;
[Ignore]
public override void Run() {}
public new void RunX() {}
public void GivenX()
{
}
public void WhenY()
{
Console.WriteLine(_x);
}
public void ThenZ()
{
}
[Test]
[TestCase(1)]
[TestCase(2)]
public void RunSpec(int x)
{
_x = x;
base.Run();
}
[Xunit.Extensions.Theory]
[InlineData(1)]
[InlineData(2)]
public void RunSpecX(int x)
{
_x = x;
base.RunX();
}
}
@MehdiK
MehdiK commented Jun 12, 2014

This is very handy. Thanks for sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment