Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Common.Test.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

<GenerateDocumentationFile>false</GenerateDocumentationFile>

<!-- Configure static code analysis -->
<RunCodeAnalysis>false</RunCodeAnalysis>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
Expand Down
24 changes: 4 additions & 20 deletions Common.props
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@

<!-- NuGet options -->
<Authors>AutoFixture Contributors</Authors>
<PackageProjectUrl>https://github.com/AutoFixture/AutoFixture</PackageProjectUrl>
<PackageProjectUrl>https://github.com/AutoFixture/AutoFixture.TUnit</PackageProjectUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageIconUrl>https://raw.githubusercontent.com/AutoFixture/AutoFixture/79c882c3f4af3cf52ad43e5c95851f25d217ac17/AutoFixtureLogo200x200.png</PackageIconUrl>
<PackageIconUrl>https://raw.githubusercontent.com/AutoFixture/AutoFixture.TUnit/master/AutoFixtureLogo200x200.png</PackageIconUrl>
<PackageIcon>icon.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
</PropertyGroup>
Expand All @@ -42,6 +43,7 @@
</ItemGroup>

<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)\README.md" Pack="true" PackagePath="README.md"/>
<None Include="$(MSBuildThisFileDirectory)\AutoFixtureLogo200x200.png" Pack="true" PackagePath="icon.png"/>
</ItemGroup>

Expand All @@ -52,23 +54,6 @@
</AssemblyAttribute>
</ItemGroup>

<Choose>
<When Condition="$(TargetFramework.StartsWith('net4')) == 'false'">
<PropertyGroup>
<DefineConstants>$(DefineConstants);SYSTEM_GLOBALIZATION_CULTUREINFO_CULTURESETTERS</DefineConstants>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup>
<_IsFullFramework>true</_IsFullFramework>
<DefineConstants>$(DefineConstants);SYSTEM_NET_MAIL;SYSTEM_RUNTIME_SERIALIZATION;SERIALIZABLE_MEMBERINFO;SYSTEM_THREADING_THREAD_CULTURESETTERS;SYSTEM_TYPE_FULL</DefineConstants>
</PropertyGroup>
</Otherwise>
</Choose>
<PropertyGroup>
<DefineConstants Condition=" $(TargetFramework.StartsWith('net4')) == 'false' ">$(DefineConstants);SYSTEM_NET_MAIL</DefineConstants>
</PropertyGroup>

<Choose>
<When Condition=" '$(Configuration)'=='Release' ">
<PropertyGroup>
Expand All @@ -77,7 +62,6 @@
</When>
<When Condition=" '$(Configuration)'=='Verify' ">
<PropertyGroup>
<DefineConstants>$(DefineConstants);CODE_ANALYSIS</DefineConstants>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
</PropertyGroup>
Expand Down
155 changes: 155 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# AutoFixture.TUnit

[![License](https://img.shields.io/badge/license-MIT-green)](https://raw.githubusercontent.com/AutoFixture/AutoFixture.TUnit/master/LICENCE.txt)
[![NuGet version](https://img.shields.io/nuget/v/AutoFixture.TUnit?logo=nuget)](https://www.nuget.org/packages/AutoFixture.TUnit)

[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.
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).

## Table of Contents

- [Installation](#installation)
- [Getting Started](#getting-started)
- [Features](#features)
- [Contributing](#contributing)
- [License](#license)

## Installation

AutoFixture packages are distributed via NuGet.
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.

```cmd
dotnet add package AutoFixture.TUnit
```

## Getting Started

### Basic Usage

`AutoFixture.TUnit` provides an `[AutoDataSource]` attribute that automatically populates test method parameters with generated data.

For example, imagine you have a simple calculator class:

```csharp
public class Calculator
{
public int Add(int a, int b) => a + b;
}
```

You can write a test using AutoFixture to provide the input values:

```csharp
using TUnit.Assertions;
using AutoFixture.TUnit;

public class CalculatorTests
{
[Test, AutoDataSource]
public async Task Add_SimpleValues_ReturnsCorrectResult(
Calculator calculator, int a, int b)
{
// Act
int result = calculator.Add(a, b);

// Assert
await Assert.That(result).IsEqualTo(a + b);
}
}
```

### Inline Auto-Data

You can also combine auto-generated data with inline arguments using the `[AutoArguments]` attribute.
This allows you to specify some parameters while still letting AutoFixture generate the rest.

```csharp
using TUnit.Assertions;
using AutoFixture.TUnit;

public class CalculatorTests
{
[Test]
[AutoArguments(5, 8)]
public async Task Add_SpecificValues_ReturnsCorrectResult(
int a, int b, Calculator calculator)
{
// Act
int result = calculator.Add(a, b);

// Assert
await Assert.That(result).IsEqualTo(13);
}
}
```

### Freezing Dependencies

AutoFixture's `[Frozen]` attribute can be used to ensure that the same instance of a dependency is injected into multiple parameters.

For example, if you have a consumer class that depends on a shared dependency:

```csharp
public class Dependency { }

public class Consumer
{
public Dependency Dependency { get; }

public Consumer(Dependency dependency)
{
Dependency = dependency;
}
}
```

You can freeze the Dependency so that all requests for it within the test will return the same instance:

```csharp
using TUnit.Assertions;
using AutoFixture.TUnit;

public class ConsumerTests
{
[Test, AutoDataSource]
public async Task Consumer_UsesSameDependency(
[Frozen] Dependency dependency, Consumer consumer)
{
// Assert
await Assert.That(consumer.Dependency).IsSameReferenceAs(dependency);
}
}
```

## Features

### Data Source Attributes

- **`[AutoDataSource]`** - Automatically generates test data for all parameters
- **`[AutoArguments]`** - Combines inline values with auto-generated data
- **`[AutoMemberDataSource]`** - Uses static members (properties, fields, methods) as data sources
- **`[AutoClassDataSource]`** - Uses classes implementing `IEnumerable<object[]>` as data sources
- **`[CompositeDataSource]`** - Composes multiple data source attributes together

### Parameter Customization Attributes

- **`[Frozen]`** - Freezes a parameter value so the same instance is reused (supports various matching criteria)
- **`[Greedy]`** - Uses the constructor with the most parameters
- **`[Modest]`** - Uses the constructor with the fewest parameters
- **`[NoAutoProperties]`** - Prevents auto-population of properties
- **`[FavorArrays]`** - Prefers constructors that take array parameters
- **`[FavorEnumerables]`** - Prefers constructors that take `IEnumerable<T>` parameters
- **`[FavorLists]`** - Prefers constructors that take `IList<T>` parameters

All customization attributes can be combined and support custom fixture factories for advanced scenarios.

## Contributing

Contributions are welcome!
If you would like to contribute, please review our contributing guidelines and open an issue or pull request.

## License

AutoFixture is Open Source software and is released under the [MIT license](LICENCE.txt).
The license allows the use of AutoFixture libraries in free and commercial applications and libraries without restrictions.