Author : MD TAREQ HASSAN
Points to Note
- Autofixure => provides anonymous test data (data that is required to execute test, but the value itself is Unimportant)
- Autofixure can also provide sut (System Under Test)
[AutoData]
&[InlineAutoData]
=> xunit extension to feed data from Autofixure to test method[InlineAutoData]
=> you provide 0 or few arguments for test method, Autofixure will fill the rest- Use
[InlineAutoData]
‘n’ times to run test method ‘n’ times - AutoData (
[AutoData]
&[InlineAutoData]
) are used for theories - See: Autofixure Cheat Sheet
Autofixure installation
Install-Package AutoFixture
Vanilla Autofixure
var fixture = new Fixture();
var autoGeneratedText = fixture.Create<string>(); // "30a35da1-d681-441b-9db3-77ff51728b58"
// seeded
var generatedTextWithPrefix = fixture.Create("Name"); // "Name30a35da1-d681-441b-9db3-77ff51728b58"
AutoData installation
Install-Package AutoFixture.Xunit2
Using AutoData
AutoData without sut
[Theory]
[AutoData]
public void TestOne(int foo, string bar)
{
}
// both annotation togather
[Theory, AutoData]
public void TestTwo(int foo, string bar)
{
}
AutoData with sut
[Theory]
[AutoData]
public void TestOne(int foo, string bar, MyClass sut)
{
}
Inline AutoData
[Theory]
[InlineAutoData] // all args will be provided by autofixture
[InlineAutoData("FOO")] // BAR & sut will be provided by autofixture
[InlineAutoData("FOO", "BAR")] // sut will be provided bt autofixture
public void Test(string foo, string bar, MyClass sut)
{
}