|
| 1 | +# AutoFixture.TUnit |
| 2 | + |
| 3 | +[](https://raw.githubusercontent.com/AutoFixture/AutoFixture.TUnit/master/LICENCE.txt) |
| 4 | +[](https://www.nuget.org/packages/AutoFixture.TUnit) |
| 5 | + |
| 6 | +[AutoFixture.TUnit](https://github.com/AutoFixture/AutoFixture.TUnit) is a .NET library that integrates [AutoFixture](https://github.com/AutoFixture/AutoFixture) with [TUnit](https://github.com/tunitframework/tunit), allowing you to effortlessly generate test data for your unit tests. |
| 7 | +By leveraging the data generators feature of TUnit, this extension turns AutoFixture into a declarative framework for writing unit tests. In many ways it becomes a unit testing DSL (Domain Specific Language). |
| 8 | + |
| 9 | +## Table of Contents |
| 10 | + |
| 11 | +- [Installation](#installation) |
| 12 | +- [Getting Started](#getting-started) |
| 13 | +- [Features](#features) |
| 14 | +- [Contributing](#contributing) |
| 15 | +- [License](#license) |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +AutoFixture packages are distributed via NuGet. |
| 20 | +To install the packages you can use the integrated package manager of your IDE, the .NET CLI, or reference the package directly in your project file. |
| 21 | + |
| 22 | +```cmd |
| 23 | +dotnet add package AutoFixture.TUnit |
| 24 | +``` |
| 25 | + |
| 26 | +## Getting Started |
| 27 | + |
| 28 | +### Basic Usage |
| 29 | + |
| 30 | +`AutoFixture.TUnit` provides an `[AutoDataSource]` attribute that automatically populates test method parameters with generated data. |
| 31 | + |
| 32 | +For example, imagine you have a simple calculator class: |
| 33 | + |
| 34 | +```csharp |
| 35 | +public class Calculator |
| 36 | +{ |
| 37 | + public int Add(int a, int b) => a + b; |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +You can write a test using AutoFixture to provide the input values: |
| 42 | + |
| 43 | +```csharp |
| 44 | +using TUnit.Assertions; |
| 45 | +using AutoFixture.TUnit; |
| 46 | + |
| 47 | +public class CalculatorTests |
| 48 | +{ |
| 49 | + [Test, AutoDataSource] |
| 50 | + public async Task Add_SimpleValues_ReturnsCorrectResult( |
| 51 | + Calculator calculator, int a, int b) |
| 52 | + { |
| 53 | + // Act |
| 54 | + int result = calculator.Add(a, b); |
| 55 | + |
| 56 | + // Assert |
| 57 | + await Assert.That(result).IsEqualTo(a + b); |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +### Inline Auto-Data |
| 63 | + |
| 64 | +You can also combine auto-generated data with inline arguments using the `[AutoArguments]` attribute. |
| 65 | +This allows you to specify some parameters while still letting AutoFixture generate the rest. |
| 66 | + |
| 67 | +```csharp |
| 68 | +using TUnit.Assertions; |
| 69 | +using AutoFixture.TUnit; |
| 70 | + |
| 71 | +public class CalculatorTests |
| 72 | +{ |
| 73 | + [Test] |
| 74 | + [AutoArguments(5, 8)] |
| 75 | + public async Task Add_SpecificValues_ReturnsCorrectResult( |
| 76 | + int a, int b, Calculator calculator) |
| 77 | + { |
| 78 | + // Act |
| 79 | + int result = calculator.Add(a, b); |
| 80 | + |
| 81 | + // Assert |
| 82 | + await Assert.That(result).IsEqualTo(13); |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +### Freezing Dependencies |
| 88 | + |
| 89 | +AutoFixture's `[Frozen]` attribute can be used to ensure that the same instance of a dependency is injected into multiple parameters. |
| 90 | + |
| 91 | +For example, if you have a consumer class that depends on a shared dependency: |
| 92 | + |
| 93 | +```csharp |
| 94 | +public class Dependency { } |
| 95 | + |
| 96 | +public class Consumer |
| 97 | +{ |
| 98 | + public Dependency Dependency { get; } |
| 99 | + |
| 100 | + public Consumer(Dependency dependency) |
| 101 | + { |
| 102 | + Dependency = dependency; |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +You can freeze the Dependency so that all requests for it within the test will return the same instance: |
| 108 | + |
| 109 | +```csharp |
| 110 | +using TUnit.Assertions; |
| 111 | +using AutoFixture.TUnit; |
| 112 | + |
| 113 | +public class ConsumerTests |
| 114 | +{ |
| 115 | + [Test, AutoDataSource] |
| 116 | + public async Task Consumer_UsesSameDependency( |
| 117 | + [Frozen] Dependency dependency, Consumer consumer) |
| 118 | + { |
| 119 | + // Assert |
| 120 | + await Assert.That(consumer.Dependency).IsSameReferenceAs(dependency); |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +## Features |
| 126 | + |
| 127 | +### Data Source Attributes |
| 128 | + |
| 129 | +- **`[AutoDataSource]`** - Automatically generates test data for all parameters |
| 130 | +- **`[AutoArguments]`** - Combines inline values with auto-generated data |
| 131 | +- **`[AutoMemberDataSource]`** - Uses static members (properties, fields, methods) as data sources |
| 132 | +- **`[AutoClassDataSource]`** - Uses classes implementing `IEnumerable<object[]>` as data sources |
| 133 | +- **`[CompositeDataSource]`** - Composes multiple data source attributes together |
| 134 | + |
| 135 | +### Parameter Customization Attributes |
| 136 | + |
| 137 | +- **`[Frozen]`** - Freezes a parameter value so the same instance is reused (supports various matching criteria) |
| 138 | +- **`[Greedy]`** - Uses the constructor with the most parameters |
| 139 | +- **`[Modest]`** - Uses the constructor with the fewest parameters |
| 140 | +- **`[NoAutoProperties]`** - Prevents auto-population of properties |
| 141 | +- **`[FavorArrays]`** - Prefers constructors that take array parameters |
| 142 | +- **`[FavorEnumerables]`** - Prefers constructors that take `IEnumerable<T>` parameters |
| 143 | +- **`[FavorLists]`** - Prefers constructors that take `IList<T>` parameters |
| 144 | + |
| 145 | +All customization attributes can be combined and support custom fixture factories for advanced scenarios. |
| 146 | + |
| 147 | +## Contributing |
| 148 | + |
| 149 | +Contributions are welcome! |
| 150 | +If you would like to contribute, please review our contributing guidelines and open an issue or pull request. |
| 151 | + |
| 152 | +## License |
| 153 | + |
| 154 | +AutoFixture is Open Source software and is released under the [MIT license](LICENCE.txt). |
| 155 | +The license allows the use of AutoFixture libraries in free and commercial applications and libraries without restrictions. |
0 commit comments