This repository is a redesign of the original AutoFixture repository which provides functionality to:
Write maintainable unit tests, faster.
AutoFixture makes it easier for developers to do Test-Driven Development by automating non-relevant Test Fixture Setup, allowing the Test Developer to focus on the essentials of each test case.
The original concepts and designs have been rewritten here to reduce duplication as well as provide extensibility points for those who need to inject custom testing behavior easily and consistently across different testing frameworks.
Common interfaces, attributes, and AutoFixture customizations.
This package contains the core classes, attributes, and logic used for generating test data via AutoFixture.
Adds AutoFixture support for MSTest V1 unit tests.
Adds AutoFixture support for MSTest V2 unit tests.
Adds AutoFixture support for Xunit V2 unit tests.
Adds AutoMoq behavior to AutoFixture so interface dependencies are automatically mocked.
NOTE: If a parameter is an interface or has a dependency that is an interface, then [AutoMoq] must be added to the test method so AutoFixture knows how to generate a value.
// MSTest
[AutoData]
public void ShouldDoSomethingGivenSomeCondition(ClassUnderTest sut)
{
// Arrange
// sut is auto-generated by AutoFixture.
// Act
// Assert
Assert.IsNotNull(sut);
}
// Xunit
[Theory]
[AutoData]
public void ShouldDoSomethingGivenSomeCondition(ClassUnderTest sut)
{
// Arrange
// sut is auto-generated by AutoFixture.
// Act
// Assert
Assert.NotNull(sut);
}
// MSTest
[AutoData]
[InlineData(1, 2, "buckle my shoe")]
public void ShouldDoSomethingElseGivenSomeCondition(int number1, int number2, string message, ClassUnderTest sut)
{
// Arrange
/*
* number1, number2, and message are all assigned from the values defined in the [InlineData] attribute.
* Test method parameters that are not defined in [InlineData] are auto-generated by AutoFixture.
*/
// Act
// Assert
Assert.AreEqual(1, number1);
Assert.AreEqual(2, number2);
Assert.AreEqual("buckle my shoe", message);
Assert.IsNotNull(sut);
}
// Xunit
[Theory]
[InlineAutoData(1, 2, "buckle my shoe")]
public void ShouldDoSomethingElseGivenSomeCondition(int number1, int number2, string message, ClassUnderTest sut)
{
// Arrange
/*
* number1, number2, and message are all assigned from the values defined in the [InlineData] attribute.
* Test method parameters that are not defined in [InlineData] are auto-generated by AutoFixture.
*/
// Act
// Assert
Assert.Equal(1, number1);
Assert.Equal(2, number2);
Assert.Equal("buckle my shoe", message);
Assert.NotNull(sut);
}
// MSTest
[AutoData]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
public void ShouldDoSomethingElseGivenAnotherCondition(int number, ClassUnderTest sut)
{
// Arrange
/*
* number == 1 for first test run, 2 for the second test run, and 3 for the third test run.
* Test method parameters that are not defined in [InlineData] are auto-generated by AutoFixture.
*/
// Act
// Assert
Assert.IsNotEqualTo(0, number);
Assert.IsNotNull(sut);
}
// Xunit
[Theory]
[InlineAutoData(1)]
[InlineAutoData(2)]
[InlineAutoData(3)]
public void ShouldDoSomethingElseGivenAnotherCondition(int number, ClassUnderTest sut)
{
// Arrange
/*
* number == 1 for first test run, 2 for the second test run, and 3 for the third test run.
* Test method parameters that are not defined in [InlineData] are auto-generated by AutoFixture.
*/
// Act
// Assert
Assert.NotEqual(0, number);
Assert.NotNull(sut);
}
// MSTest
[AutoData, AutoMoq]
public void ShouldDoSomethingGivenSomeCondition(IAmAnInterface dependency, ClassUnderTest sut)
{
// Arrange
// `dependency` will be automatically generated as a Mock.Of<IAmAnInterface>() instance.
// Act
// Assert
Assert.IsNotNull(dependency);
}
// Xunit
[Theory]
[AutoData, AutoMoq]
public void ShouldDoSomethingGivenSomeCondition(IAmAnInterface dependency, ClassUnderTest sut)
{
// Arrange
// `dependency` will be automatically generated as a Mock.Of<IAmAnInterface>() instance.
// Act
// Assert
Assert.NotNull(dependency);
}