You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[AutoFixture.xUnit](https://github.com/AutoFixture/AutoFixture.xUnit) is a .NET library that integrates [AutoFixture](https://github.com/AutoFixture/AutoFixture) with xUnit 1.x, allowing you to effortlessly generate test data for your unit tests.
9
+
By automatically populating your test parameters, it helps you write cleaner, more maintainable tests without having to manually construct test objects.
13
10
14
-
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.
15
-
16
-
Check the [testimonials](https://github.com/AutoFixture/AutoFixture/wiki/Who-uses-AutoFixture) to see what other people have to say about AutoFixture.
11
+
> [!WARNING]
12
+
> While this package is still being developed, the xUnit 1 package is deprecated.<br/>
13
+
> This package is intended only for legacy projects that are still using xUnit 1.x.<br/>
17
14
18
15
## Table of Contents
19
16
20
-
-[Overview](#overview)
21
-
-[Downloads](#downloads)
22
-
-[Documentation](#documentation)
23
-
-[Feedback & Questions](#feedback--questions)
17
+
-[Installation](#installation)
18
+
-[Getting Started](#getting-started)
19
+
-[Integrations](#integrations)
20
+
-[Contributing](#contributing)
24
21
-[License](#license)
25
22
26
-
## Overview
23
+
## Installation
24
+
25
+
AutoFixture packages are distributed via NuGet.<br />
26
+
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.
(Jump straight to the [CheatSheet](https://github.com/AutoFixture/AutoFixture/wiki/Cheat-Sheet) if you just want to see some code samples right away.)
36
+
## Getting Started
29
37
30
-
AutoFixture is designed to make Test-Driven Development more productive and unit tests more refactoring-safe. It does so by removing the need for hand-coding anonymous variables as part of a test's Fixture Setup phase. Among other features, it offers a generic implementation of the [Test Data Builder](http://www.natpryce.com/articles/000714.html) pattern.
38
+
### Basic Usage
31
39
32
-
When writing unit tests, you typically need to create some objects that represent the initial state of the test. Often, an API will force you to specify much more data than you really care about, so you frequently end up creating objects that has no influence on the test, simply to make the code compile.
40
+
`AutoFixture.xUnit` provides an `[AutoData]` attribute that automatically populates test method parameters with generated data.
33
41
34
-
AutoFixture can help by creating such [Anonymous Variables](https://docs.microsoft.com/en-us/archive/blogs/ploeh/anonymous-variables) for you. Here's a simple example:
42
+
For example, imagine you have a simple calculator class:
35
43
36
44
```c#
37
-
[Fact]
38
-
publicvoidIntroductoryTest()
45
+
publicclassCalculator
39
46
{
40
-
// Arrange
41
-
Fixturefixture=newFixture();
42
-
43
-
intexpectedNumber=fixture.Create<int>();
44
-
MyClasssut=fixture.Create<MyClass>();
45
-
// Act
46
-
intresult=sut.Echo(expectedNumber);
47
-
// Assert
48
-
Assert.Equal(expectedNumber, result);
47
+
publicintAdd(inta, intb) =>a+b;
49
48
}
50
49
```
51
50
52
-
This example illustrates the basic principle of AutoFixture: It can create values of virtually any type without the need for you to explicitly define which values should be used. The number *expectedNumber* is created by a call to `Create<T>` - this will create a 'nice', regular integer value, saving you the effort of explicitly coming up with one.
53
-
54
-
The example also illustrates how AutoFixture can be used as a [SUT Factory](http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx) that creates the actual System Under Test (the MyClass instance).
55
-
56
-
Given the right combination of unit testing framework and extensions for AutoFixture, we can further reduce the above test to be even more declarative:
Notice how we can reduce unit tests to state only the relevant parts of the test. The rest (variables, Fixture object) is relegated to attributes and parameter values that are supplied automatically by AutoFixture. The test is now only two lines of code.
97
+
### Freezing Dependencies
81
98
82
-
Using AutoFixture is as easy as referencing the library and creating a new instance of the Fixture class!
99
+
AutoFixture's `[Frozen]` attribute can be used to ensure that the same instance of a dependency is injected into multiple parameters.
83
100
84
-
## Downloads
101
+
For example, if you have a consumer class that depends on a shared dependency:
85
102
86
-
AutoFixture packages are distributed via NuGet.<br />
87
-
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.
You can freeze the Dependency so that all requests for it within the test will return the same instance:
118
+
119
+
```c#
120
+
usingXunit;
121
+
usingAutoFixture.xUnit;
122
+
usingAutoFixture;
123
+
124
+
publicclassConsumerTests
125
+
{
126
+
[Theory, AutoData]
127
+
publicvoidConsumer_UsesSameDependency(
128
+
[Frozen] Dependencydependency, Consumerconsumer)
129
+
{
130
+
// Assert
131
+
Assert.AreSame(dependency, consumer.Dependency);
132
+
}
133
+
}
95
134
```
96
135
136
+
## Integrations
137
+
97
138
AutoFixture offers a variety of utility packages and integrations with most of the major mocking libraries and testing frameworks.
98
139
140
+
> [!NOTE]
141
+
> Since AutoFixture tries maintain compatibility with a large number of package versions, the packages bundled with AutoFixture might not contain the latest features of your (e.g. mocking) library.<br />
142
+
> Make sure to install the latest version of the integrated library package, alongside the AutoFixture packages.
143
+
99
144
### Core packages
100
145
101
146
The core packages offer the full set of AutoFixture's features without requring any testing framework or third party integration.
@@ -118,10 +163,6 @@ These integrations enable such features as configuring mocks, auto-injecting moc
> Since AutoFixture tries maintain compatibility with a large number of package versions, the packages bundled with AutoFixture might not contain the latest features of your mocking library.<br />
123
-
> Make sure to install the latest version of the mocking library package, alongside the AutoFixture package.
124
-
125
166
### Testing frameworks
126
167
127
168
AutoFixture offers integrations with most major .NET testing frameworks.<br />
@@ -139,41 +180,14 @@ These integrations enable auto-generation of test cases, combining auto-generate
139
180
140
181
You can check the compatibility with your target framework version on the [wiki](https://github.com/AutoFixture/AutoFixture/wiki#net-platforms-compatibility-table) or on the [NuGet](https://www.nuget.org/profiles/AutoFixture) website.
141
182
142
-
### vNext feed
143
-
144
-
The artifacts of the next major version are published to [nuget.org](https://www.nuget.org), and are marked with the `preview` suffix (e.g. `5.0.0-preview00007`).</br>
145
-
You can use these packages to early access and test the next major version of the AutoFixture.</br>
146
-
Make sure to enable the preview packages in your IDE in order to see the latest version.
147
-
148
-
> __NOTE:__ This preview versions exists for the _preview purpose_ only, so use them with caution:
149
-
>
150
-
>* New versions of packages might contain breaking changes and API could change drastically from package to package. By other words, we don't follow the SemVer policy for the packages in this feed;
151
-
>* Preview packages might be unlisted over time, in order to not clutter the version suggestion dialog in IDEs, but will generally remain available
If you would like to contribute, please review our [contributing guidelines](https://github.com/AutoFixture/AutoFixture.xUnit/blob/maste/CONTRIBUTING.md) and open an issue or pull request.
173
187
174
188
## License
175
189
176
-
AutoFixture is Open Source software and is released under the [MIT license](https://raw.githubusercontent.com/AutoFixture/AutoFixture/master/LICENCE.txt).<br />
190
+
AutoFixture is Open Source software and is released under the [MIT license](https://raw.githubusercontent.com/AutoFixture/AutoFixture.xUnit/master/LICENCE.txt).<br />
177
191
The licenses allows the use of AutoFixture libraries in free and commercial applications and libraries without restrictions.
0 commit comments