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 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.
8
+
[AutoFixture.xUnit3](https://github.com/AutoFixture/AutoFixture.xUnit3) is a .NET library that integrates [AutoFixture](https://github.com/AutoFixture/AutoFixture) with xUnit 3.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.
17
10
18
11
## Table of Contents
19
12
20
-
-[Overview](#overview)
21
-
-[Downloads](#downloads)
22
-
-[Documentation](#documentation)
23
-
-[Feedback & Questions](#feedback--questions)
13
+
-[Installation](#installation)
14
+
-[Getting Started](#getting-started)
15
+
-[Integrations](#integrations)
16
+
-[Contributing](#contributing)
24
17
-[License](#license)
25
18
26
-
## Overview
19
+
## Installation
20
+
21
+
AutoFixture packages are distributed via NuGet.<br />
22
+
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.)
32
+
## Getting Started
29
33
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.
34
+
### Basic Usage
31
35
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.
36
+
`AutoFixture.xUnit3` provides an `[AutoData]` attribute that automatically populates test method parameters with generated data.
33
37
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:
38
+
For example, imagine you have a simple calculator class:
35
39
36
40
```c#
37
-
[Fact]
38
-
publicvoidIntroductoryTest()
41
+
publicclassCalculator
39
42
{
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);
43
+
publicintAdd(inta, intb) =>a+b;
49
44
}
50
45
```
51
46
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.
93
+
### Freezing Dependencies
81
94
82
-
Using AutoFixture is as easy as referencing the library and creating a new instance of the Fixture class!
95
+
AutoFixture's `[Frozen]` attribute can be used to ensure that the same instance of a dependency is injected into multiple parameters.
83
96
84
-
## Downloads
97
+
For example, if you have a consumer class that depends on a shared dependency:
85
98
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:
114
+
115
+
```c#
116
+
usingXunit;
117
+
usingAutoFixture.Xunit3;
118
+
usingAutoFixture;
119
+
120
+
publicclassConsumerTests
121
+
{
122
+
[Theory, AutoData]
123
+
publicvoidConsumer_UsesSameDependency(
124
+
[Frozen] Dependencydependency, Consumerconsumer)
125
+
{
126
+
// Assert
127
+
Assert.AreSame(dependency, consumer.Dependency);
128
+
}
129
+
}
95
130
```
96
131
132
+
## Integrations
133
+
97
134
AutoFixture offers a variety of utility packages and integrations with most of the major mocking libraries and testing frameworks.
98
135
136
+
> [!NOTE]
137
+
> 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 />
138
+
> Make sure to install the latest version of the integrated library package, alongside the AutoFixture packages.
139
+
99
140
### Core packages
100
141
101
142
The core packages offer the full set of AutoFixture's features without requring any testing framework or third party integration.
@@ -118,10 +159,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
162
### Testing frameworks
126
163
127
164
AutoFixture offers integrations with most major .NET testing frameworks.<br />
@@ -139,41 +176,14 @@ These integrations enable auto-generation of test cases, combining auto-generate
139
176
140
177
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
178
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.xUnit3/blob/maste/CONTRIBUTING.md) and open an issue or pull request.
173
183
174
184
## License
175
185
176
-
AutoFixture is Open Source software and is released under the [MIT license](https://raw.githubusercontent.com/AutoFixture/AutoFixture/master/LICENCE.txt).<br />
186
+
AutoFixture is Open Source software and is released under the [MIT license](https://raw.githubusercontent.com/AutoFixture/AutoFixture.xUnit3/master/LICENCE.txt).<br />
177
187
The licenses allows the use of AutoFixture libraries in free and commercial applications and libraries without restrictions.
0 commit comments