I love xUnit's data driven unit tests, I also really enjoy working with RavenDB, and now I can use them together!
Data driven unit tests are very powerful tools that allow you to execute the same test code against multiple data sets. Testing frameworks such as xUnit makes this extremely easy to develop by offering an out of the box set attributes to quickly and easily annotate your test methods with dynamic data sources.
Below is some simple code that adds a RavenDataAttribute to xUnit. This attribute will pull arguments from a document database and pass them into your unit test, using the fully qualified method name as a key.
Example Unit Tests
public class RavenDataTests
{
[Theory]
[RavenData]
public void PrimitiveArgs(int number, bool isDivisibleBytwo)
{
var remainder = number % 2;
Assert.Equal(isDivisibleBytwo, remainder == 0);
}
[Theory]
[RavenData]
public void ComplexArgs(ComplexArgsModel model)
{
var remainder = model.Number % 2;
Assert.Equal(model.IsDivisibleByTwo, remainder == 0);
}
[Fact(Skip = "Only run once for setup")]
public void Setup()
{
var type = typeof(RavenDataTests);
var primitiveArgsMethod = type.GetMethod("PrimitiveArgs");
var primitiveArgs = new object[] { 3, false };
RavenDataAttribute.SaveData(primitiveArgsMethod, primitiveArgs);
var complexArgsMethod = type.GetMethod("ComplexArgs");
var complexArgsModel = new ComplexArgsModel
{
IsDivisibleByTwo = true,
Number = 4
};
RavenDataAttribute.SaveData(complexArgsMethod, complexArgsModel);
}
public class ComplexArgsModel
{
public int Number { get; set; }
public bool IsDivisibleByTwo { get; set; }
}
}