https://github.com/josh-degraw/autofixture.community.fsharp
A small library that adds support for native F# types to AutoFixture
https://github.com/josh-degraw/autofixture.community.fsharp
autofixture fsharp random-data-generation xunit xunit-theory
Last synced: 4 months ago
JSON representation
A small library that adds support for native F# types to AutoFixture
- Host: GitHub
- URL: https://github.com/josh-degraw/autofixture.community.fsharp
- Owner: josh-degraw
- Created: 2020-06-29T17:06:59.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-07-25T20:33:20.000Z (about 2 years ago)
- Last Synced: 2025-06-02T03:19:46.034Z (4 months ago)
- Topics: autofixture, fsharp, random-data-generation, xunit, xunit-theory
- Language: F#
- Homepage:
- Size: 35.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# AutoFixture.Community.FSharp

| Package | Version | Downloads |
| ------- | ------- | --------- |
| AutoFixture.Community.FSharp | [](https://www.nuget.org/packages/AutoFixture.Community.FSharp) | [](https://www.nuget.org/packages/AutoFixture.Community.FSharp) |
| AutoFixture.Community.FSharp.Xunit | [](https://www.nuget.org/packages/AutoFixture.Community.FSharp.Xunit) | [](https://www.nuget.org/packages/AutoFixture.Community.FSharp.Xunit) |This is a small library that builds on top of [AutoFixture](https://github.com/AutoFixture/AutoFixture) to add support
for F#-specific types.
Specifically, the important additions that are't currently handled well with vanilla `AutoFixture` are:1. Discriminated Unions
2. F# listsEverything else seems to work pretty well out of the box.
## Useful bits:
### `FSharpSpecimenBuilder`:
Can be inserted onto any existing `IFixture` by doing the following
```fsharp
let fixture = Fixture()
//...fixture.Customizations.Add(FSharpSpecimenBuilder(fixture))
```### `fsFixture`
A pre-made fixture with only the F# features applied.
### `randVal` and friends
Helper functions for creating random data inline, e.g.:
```fsharp
type MyDto =
{ Foo : int
Bar : string
Baz : DateTime }[]
let ``My test method when Foo is 5`` () =
// Generate random values explicitly for specific properties
let myDto =
{ Foo = 5
Bar = randVal()
Baz = randVal() }// Or generate a full random value and only override specific properties
let otherDto =
{ randVal() with Foo = 5 }
~~// Do assertions~~
```Thanks to F#'s type system, _most_ of the time you can omi~~~~t the type arguments.
Similar helper functions include:- `randVal<'a>()` : create a single random value
- `randVals<'a>()` : create an `'a seq`
- `randValsN<'a> (n: int)` : create an `'a seq` that is `n` elements long
- `randValExceptWhere<'a> (fn: 'a -> bool)` : create a single random value for which `fn` returns false.
- `randValExcept<'a> (x: 'a)` : create a random value that is not `x`
- `randValsExceptWhere<'a> (fn: 'a -> bool)` : create an infinite sequence of random values where `fn` returns false
- `randValsExcept<'a> (x: 'a)` : create an infinite sequence of random values except `x`
- `randValsNExceptWhere<'a> (n: int) (fn: 'a -> bool)` : create a sequence of `n` random values where `fn` returns false
- `randValsNExcept<'a> (n: int) (x: 'a)` : create a sequence of `n` random values except for `x`## AutoFixture.Community.FSharp.Xunit
This package defines just two attributes:
1. `AutoDataFSharpAttribute`
2. `InlineAutoDataFSharpAttribute`Which respectively correspond to the `AutoDatAttribute` and `InlineAutoDataAttribute`
from [AutoFixture.Xunit2](https://blog.ploeh.dk/2010/10/08/AutoDataTheorieswithAutoFixture/)