CsCheck

CsCheck is a C# random testing library inspired by QuickCheck.
It differs in that generation and shrinking are both based on PCG, a fast random number generator.
This gives the following advantages over tree based shrinking libraries:
- Automatic shrinking. Gen classes are composable with no need for Arb classes. So less boilerplate.
- Random testing and shrinking are parallelized. This and PCG make it very fast.
- Shrunk cases have a seed value. Simpler examples can easily be reproduced.
- Shrinking can be continued later to give simpler cases for high-dimensional problems.
- Parallel concurrency testing and random shrinking work well together. Repeat is not needed.
New to random testing? Read the beginner's getting started guide.
See why you should use it, the comparison with other random testing libraries, or how CsCheck does in the shrinking challenge.
In one shrinking challenge test CsCheck managed to shrink to a new smaller example than was thought possible and is not reached by any other testing library.
CsCheck is the only random testing library that can always shrink to the simplest example (given enough time).
Using an AI coding agent? CsCheck ships agent skills for writing CsCheck tests and finding/closing test gaps, installable in Claude Code, Copilot CLI, VS Code, Cursor and Codex.
CsCheck also has functionality to make multiple types of testing simple and fast:
The following tests are in xUnit TUnit but could equally be used in any testing framework.
More to see in the Tests. There are also 1,000+ F# tests using CsCheck in MKL.NET.
No Reflection was used in the making of this product and CsCheck 5 is now AOT compatible!
Generator Creation Example
Use Gen and its Linq methods to compose generators for any type. Here we create a Gen for json documents.
More often it will simply be composing a few primitives and collections.
Don't worry about shrinking as it's automatic and the best in the business.
static readonly Gen<string> genString = Gen.String[Gen.Char.AlphaNumeric, 2, 5];
static readonly Gen<JsonNode> genJsonValue = Gen.OneOf<JsonNode>(
Gen.Bool.Select(x => JsonValue.Create(x)),
Gen.Byte.Select(x => JsonValue.Create(x)),
Gen.Char.AlphaNumeric.Select(x => JsonValue.Create(x)),
Gen.DateTime.Select(x => JsonValue.Create(x)),
Gen.DateTimeOffset.Select(x => JsonValue.Create(x)),
Gen.Decimal.Select(x => JsonValue.Create(x)),
Gen.Double.Select(x => JsonValue.Create(x)),
Gen.Float.Select(x => JsonValue.Create(x)),
Gen.Guid.Select(x => JsonValue.Create(x)),
Gen.Int.Select(x => JsonValue.Create(x)),
Gen.Long.Select(x => JsonValue.Create(x)),
Gen.SByte.Select(x => JsonValue.Create(x)),
Gen.Short.Select(x => JsonValue.Create(x)),
genString.Select(x => JsonValue.Create(x)),
Gen.UInt.Select(x => JsonValue.Create(x)),
Gen.ULong.Select(x => JsonValue.Create(x)),
Gen.UShort.Select(x => JsonValue.Create(x)));
static readonly Gen<JsonNode> genJsonNode = Gen.Recursive<JsonNode>((depth, genJsonNode) =>
{
if (depth == 5) return genJsonValue;
var genJsonObject = Gen.Dictionary(genString, genJsonNode.Null())[0, 5].Select(d => new JsonObject(d));
var genJsonArray = genJsonNode.Null().Array[0, 5].Select(i => new JsonArray(i));
return Gen.OneOf(genJsonObject, genJsonArray, genJsonValue);
});
Random testing