FsCheck Documentation

repository·master·Indexed 22 days ago

https://github.com/fscheck/fscheck

An automated property-based testing tool for .NET that allows developers to define specifications as properties and automatically generates random inputs to find edge cases and counter-examples. It supports F#, C#, and VB, and integrates with testing frameworks including xUnit, NUnit, MSTest, and MbUnit.

Tokens
1.1K
Snippets
1
Records
8
Agent score
79%

What's inside FsCheck

  1. What is FsCheck?

    master

    FsCheck is an automated testing tool for .NET programs. Instead of writing individual test cases, you provide a specification of your program in the form of properties (rules that functions, methods, or objects must satisfy). FsCheck then attempts to falsify these properties by testing them against a large number of randomly generated cases.

    Key features include:

    • Property-based testing: Define testable specifications using combinators.
    • Automatic Counter-examples: When a property fails, FsCheck automatically finds and displays a minimal counter-example that triggers the failure.
    • Generators: Provides combinators to define custom test data generators and observe data distribution.
    • Multi-language support: Specifications can be written in F#, C#, or VB.
    • Framework Integration: Integrates with NUnit, xUnit, MSTest, and MbUnit. Its generator combinators can also be used independently in any testing framework.
  2. Support for Async and Task in Properties

    master

    FsCheck supports properties that return Task<T> or Async<'Testable>.

    Note for Task<'T> users: In version 3.1.0, if you are using Task<'T> where 'T is not itself a testable type, you may need to provide an explicit upcast to a non-generic Task (using :> Task) to ensure compatibility.

  3. Migrate to FsCheck 3.0.0+ API (Breaking Changes)

    master

    Version 3.0.0 introduced significant breaking changes to the API structure to better support both F# and C# consumers.

    API Splitting

    The Gen and Arbitrary APIs were split into two distinct namespaces:

    • F# Users: Use FsCheck.FSharp. You should add open FsCheck.FSharp in addition to open FsCheck.
    • C# / VB.NET Users: Use FsCheck.Fluent. You should add using FsCheck.Fluent.

    Arbitrary Instance Discovery

    The global, mutable map for Arbitrary instances has been replaced with immutable IArbMap instances. You now configure these via Config.WithArbitrary on your test.

    • To look up default types, use ArbMap.defaults |> ArbMap.arbitrary<type> (F#) or ArbMap.Default.ArbFor<type>() (C#).

    Removed Operators and Methods

    • The operators |@, @|, and %> have been removed. Use Prop.label instead.
    • StringNoNnulls has been renamed to StringNoNullChar.
  4. Learn about property-based testing with FsCheck

    master

    To deepen your understanding of FsCheck and property-based testing (PBT) in general, you can consult the following curated resources:

    Articles

    • Introduction to FsCheck 3: "FsCheck 3: Property-based testing in C#"
    • PBT Fundamentals: A six-part series on implementing a simplified PBT library covering essentials, shrinking, unifying, and random/all strategies.
    • Academic & Foundational:
      • "QuickCheck: A Lightweight Tool for Random Testing of Haskell Programs" by Koen Claessen and John Hughes.
      • "An introduction to property-based testing" by Scott Wlaschin.
      • "Choosing properties for property-based testing" by Scott Wlaschin.
    • Case Studies:
      • Using FsCheck for testing branch and bound algorithms (from the "Learning F#" series).
      • Chapter 11 of "Real world Haskell" regarding QuickCheck.

    Books

    • "Property-Based Testing with PropEr, Erlang, and Elixir" by Fred Hebert.

    Videos

    • "The lazy programmer's guide to writing 1000's of tests: An introduction to property based testing" by Scott Wlaschin.
    • "Race Conditions, Distribution, Interactions - Testing the Hard Stuff and Staying Sane" by John Hughes.
    • "Property-Based Testing for Better Code" by Jessica Kerr.
  5. Install FsCheck via NuGet or Paket

    master

    FsCheck and its framework-specific adapters are available on NuGet.

    NuGet Packages

    • FsCheck: The core library.
    • FsCheck.Xunit: Integration for xUnit.
    • FsCheck.Nunit: Integration for NUnit.

    Using Paket

    If you use Paket, add the AppVeyor NuGet feed to your paket.dependencies file to access builds:

    source https://www.nuget.org/api/v2
    source https://ci.appveyor.com/nuget/fscheck

    Example dependency specifications:

    nuget FsCheck
    nuget FsCheck prerelease
    nuget FsCheck 2.0.4
    nuget FsCheck 2.0.5-b247
    source https://www.nuget.org/api/v2
    source https://ci.appveyor.com/nuget/fscheck
    
    nuget FsCheck