Skip to content

jhenningerwebgility1/ByteStudio.AutoFixture

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ByteStudio.AutoFixture

Project Description

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.

NuGet Packages

ByteStudio.AutoFixture

Common interfaces, attributes, and AutoFixture customizations.

This package contains the core classes, attributes, and logic used for generating test data via AutoFixture.

ByteStudio.AutoFixture.MSTest1

Adds AutoFixture support for MSTest V1 unit tests.

ByteStudio.AutoFixture.MSTest2

Adds AutoFixture support for MSTest V2 unit tests.

ByteStudio.AutoFixture.Xunit2

Adds AutoFixture support for Xunit V2 unit tests.

ByteStudio.AutoFixture.AutoMoq

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.

Examples

Example 1 - Create test method with an AutoFixture-generated argument.

// 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);
}

Example 2 - Define parameters to run for test.

// 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);
}

Example 3 - Define different sets of parameters to run for the same test.

// 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);
}

Example 4 - AutoMoq interface test parameter.

// 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);
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%