Skip to content

Commit 2771801

Browse files
authored
Added readme (#4)
* Added readme file * Updated package metadata * Added readme to the NuGet package * Removed redundant props
1 parent e8746f4 commit 2771801

3 files changed

Lines changed: 159 additions & 22 deletions

File tree

Common.Test.props

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
77
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
88

9-
<GenerateDocumentationFile>false</GenerateDocumentationFile>
10-
119
<!-- Configure static code analysis -->
1210
<RunCodeAnalysis>false</RunCodeAnalysis>
1311
<EnableNETAnalyzers>true</EnableNETAnalyzers>

Common.props

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828

2929
<!-- NuGet options -->
3030
<Authors>AutoFixture Contributors</Authors>
31-
<PackageProjectUrl>https://github.com/AutoFixture/AutoFixture</PackageProjectUrl>
31+
<PackageProjectUrl>https://github.com/AutoFixture/AutoFixture.TUnit</PackageProjectUrl>
3232
<PublishRepositoryUrl>true</PublishRepositoryUrl>
3333
<PackageLicenseExpression>MIT</PackageLicenseExpression>
34-
<PackageIconUrl>https://raw.githubusercontent.com/AutoFixture/AutoFixture/79c882c3f4af3cf52ad43e5c95851f25d217ac17/AutoFixtureLogo200x200.png</PackageIconUrl>
34+
<PackageIconUrl>https://raw.githubusercontent.com/AutoFixture/AutoFixture.TUnit/master/AutoFixtureLogo200x200.png</PackageIconUrl>
3535
<PackageIcon>icon.png</PackageIcon>
36+
<PackageReadmeFile>README.md</PackageReadmeFile>
3637
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
3738
<EmbedUntrackedSources>true</EmbedUntrackedSources>
3839
</PropertyGroup>
@@ -42,6 +43,7 @@
4243
</ItemGroup>
4344

4445
<ItemGroup>
46+
<None Include="$(MSBuildThisFileDirectory)\README.md" Pack="true" PackagePath="README.md"/>
4547
<None Include="$(MSBuildThisFileDirectory)\AutoFixtureLogo200x200.png" Pack="true" PackagePath="icon.png"/>
4648
</ItemGroup>
4749

@@ -52,23 +54,6 @@
5254
</AssemblyAttribute>
5355
</ItemGroup>
5456

55-
<Choose>
56-
<When Condition="$(TargetFramework.StartsWith('net4')) == 'false'">
57-
<PropertyGroup>
58-
<DefineConstants>$(DefineConstants);SYSTEM_GLOBALIZATION_CULTUREINFO_CULTURESETTERS</DefineConstants>
59-
</PropertyGroup>
60-
</When>
61-
<Otherwise>
62-
<PropertyGroup>
63-
<_IsFullFramework>true</_IsFullFramework>
64-
<DefineConstants>$(DefineConstants);SYSTEM_NET_MAIL;SYSTEM_RUNTIME_SERIALIZATION;SERIALIZABLE_MEMBERINFO;SYSTEM_THREADING_THREAD_CULTURESETTERS;SYSTEM_TYPE_FULL</DefineConstants>
65-
</PropertyGroup>
66-
</Otherwise>
67-
</Choose>
68-
<PropertyGroup>
69-
<DefineConstants Condition=" $(TargetFramework.StartsWith('net4')) == 'false' ">$(DefineConstants);SYSTEM_NET_MAIL</DefineConstants>
70-
</PropertyGroup>
71-
7257
<Choose>
7358
<When Condition=" '$(Configuration)'=='Release' ">
7459
<PropertyGroup>
@@ -77,7 +62,6 @@
7762
</When>
7863
<When Condition=" '$(Configuration)'=='Verify' ">
7964
<PropertyGroup>
80-
<DefineConstants>$(DefineConstants);CODE_ANALYSIS</DefineConstants>
8165
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
8266
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
8367
</PropertyGroup>

README.md

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# AutoFixture.TUnit
2+
3+
[![License](https://img.shields.io/badge/license-MIT-green)](https://raw.githubusercontent.com/AutoFixture/AutoFixture.TUnit/master/LICENCE.txt)
4+
[![NuGet version](https://img.shields.io/nuget/v/AutoFixture.TUnit?logo=nuget)](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

Comments
 (0)