Skip to content

Instantly share code, notes, and snippets.

@robdmoore
Created January 27, 2014 08:36
Show Gist options
  • Save robdmoore/8644975 to your computer and use it in GitHub Desktop.
Save robdmoore/8644975 to your computer and use it in GitHub Desktop.
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
Copy link

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