Property-Based Testing Implementation in C#
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Property-Based Testing runs in O(n) time and O(n) space, in C# as in every other language.
- ✓The BCL has nothing. `FsCheck` is the standard (it is written in F# and consumed from C#), with `FsCheck.Xunit` supplying the `[Property]` attribute -- `MaxTest`, `Replay`, `Arbitrary`, `QuietOnSuccess` -- and `FsCheck.NUnit` the NUnit equivalent.
- ✓A reviewer replaces a `[Fact]` that loops over `Random` with a `[Property]` method taking generated parameters, and prefers the built-in constrained wrappers (`NonNull<string>`, `PositiveInt`, `NonEmptyArray<T>`) over `.Where(...)` filtering, which discards draws and can exhaust the generator.
- ✓The C# trap: Nullable reference types are a compile-time fiction erased from the assembly, and FsCheck is an F# library that never sees your `#nullable enable`: its default `string` arbitrary deliberately mixes `null` into the distribution, so a `string` parameter the compiler has just promised is non-null arrives null and the property throws `NullReferenceException` on a value your warnings said could not exist.
- ✓FsCheck's `Gen<T>` is an F# function value, so each combinator in a generator chain allocates a delegate and each draw invokes through it; shrinking is a lazy F# `seq`, so walking candidates allocates enumerators rather than arrays.
What Property-Based Testing actually is
Property-Based Testing in C#: what you actually reach for
C# implementation
using System.Collections.Generic;
using System.Linq;
using FsCheck;
using FsCheck.Xunit;
public class CodecProperties
{
[Property(MaxTest = 1000)]
public bool SortingIsIdempotent(int[] xs)
{
var once = xs.OrderBy(x => x).ToArray();
return once.OrderBy(x => x).SequenceEqual(once);
}
// The compiler swears `string` is non-null under NRTs, but FsCheck's
// default reference-type generator still yields null. NonNull<T> opts out.
[Property]
public bool TrimIsIdempotent(NonNull<string> s)
{
var once = s.Get.Trim();
return once.Trim() == once;
}
[Property]
public bool DedupeNeverGrows(NonEmptyArray<int> xs)
{
var set = new HashSet<int>(xs.Get);
return set.Count <= xs.Get.Length;
}
}
Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe C#-specific trap
Performance in C#, beyond the O(n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which C# type or library should I use for Property-Based Testing?
The BCL has nothing. `FsCheck` is the standard (it is written in F# and consumed from C#), with `FsCheck.Xunit` supplying the `[Property]` attribute -- `MaxTest`, `Replay`, `Arbitrary`, `QuietOnSuccess` -- and `FsCheck.NUnit` the NUnit equivalent. The core types are `Gen<T>`, `Arbitrary<T>`, the `Arb` helpers and built-in wrappers such as `NonNull<T>`, `NonEmptyString`, `PositiveInt` and `NonEmptyArray<T>`. Registration of custom generators changed shape across FsCheck's major versions, so read the API on your pinned package rather than copying old posts. `CsCheck` and `Hedgehog` are the C#-first alternatives.
What is the most common C# mistake when implementing Property-Based Testing?
Nullable reference types are a compile-time fiction erased from the assembly, and FsCheck is an F# library that never sees your `#nullable enable`: its default `string` arbitrary deliberately mixes `null` into the distribution, so a `string` parameter the compiler has just promised is non-null arrives null and the property throws `NullReferenceException` on a value your warnings said could not exist. Arrays and collections are generated non-null, which is why the bare `int[]` property above is safe. Take `NonNull<string>` (then `.Get`) or register an Arbitrary that excludes null. Related C#-only surprise: a generated `decimal` is not a binary float, so a law that holds for `double` inputs can fail for `decimal` on scale, and `-0.0` round-trips differently between the two.
How does Property-Based Testing actually perform in C#?
FsCheck's `Gen<T>` is an F# function value, so each combinator in a generator chain allocates a delegate and each draw invokes through it; shrinking is a lazy F# `seq`, so walking candidates allocates enumerators rather than arrays. Tiered compilation means the first tries of a 100-test default run execute tier-0 code before promotion, and value types crossing the reflection boundary in the xUnit adapter get boxed once per example. For array-heavy properties, generation and GC usually dominate the system under test; cap the value size with `StartSize`/`EndSize` (and the run length with `MaxTest`) rather than filtering after the fact. Time complexity is O(n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialStudying for this? Devin free tiers and academic plans are laid out on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.