Questions tagged [fscheck]

FsCheck is a framework for random testing of .NET programs. FsCheck is a port of Haskell's QuickCheck written in F#.

FsCheck is a framework for random testing of .NET programs. FsCheck is a port of Haskell's QuickCheck written in F#.

To test a program with FsCheck, you provide a specification of the program, in the form of properties which functions, methods or objects should satisfy, and FsCheck then tests that the properties hold in a large number of randomly generated cases. While writing the properties, you are actually writing a testable specification of your program. Specifications are expressed in F#, C# or VB, using a flexible and clean API. It sounds lofty, but it's more fun than unit testing!

You can write properties, observe the distribution of test data, and define test data generators. When a property fails, FsCheck automatically displays a minimal counter example - this process, called shrinking, is very useful to quickly find the bug. Instead of digging through a potentially large randomly generated test case to find the part that actually triggers the bug, FsCheck can do that work for you!

151 questions
13
votes
2 answers

FsCheck in C#: generate a list of two dimension arrays with the same shape

Let's say I'm writing some code for video analysis. Here is a simplified version of a Video class: public class Video { public readonly int Width; public readonly int Height; public readonly List Frames; public Video(int…
Pavel Murygin
  • 2,242
  • 2
  • 18
  • 26
13
votes
3 answers

Difficulty thinking of properties for FsCheck

I've managed to get xUnit working on my little sample assembly. Now I want to see if I can grok FsCheck too. My problem is that I'm stumped when it comes to defining test properties for my functions. Maybe I've just not got a good sample set of…
Benjol
  • 63,995
  • 54
  • 186
  • 268
12
votes
1 answer

Inconsistent IEnumerable ArgumentException while generating a complex object using FsCheck

The Problem In F#, I am using FsCheck to generate an object (which I'm then using in an Xunit test, but I can recreate entirely outside of Xunit, so I think we can forget about Xunit). Running the generation 20 times in FSI, 50% of the time, the…
chryosolo
  • 363
  • 2
  • 11
11
votes
2 answers

How does one generate a "complex" object in FsCheck?

I'd like to create an FsCheck Generator to generate instances of a "complex" object. By complex, I mean an existing class in C# that has a number of child properties and collections. These properties and collections in turn need to have data…
bentayloruk
  • 4,060
  • 29
  • 31
9
votes
1 answer

Force FsCheck to generate NonEmptyString for discriminating union fields of type string

I'm trying to achieve the following behaviour with FsCheck: I'd like to create a generator that will generate a instance of MyUnion type, with every string field being non-null/empty. type MyNestedUnion = | X of string | Y of int *…
9
votes
2 answers

How to easily filter out a discriminated union case in FsCheck?

Consider a Discriminated Union: type DU = | Foo of string | Bar of int | Baz of decimal * float | Qux of bool I'd like to create a list of DU values with FsCheck, but I want none of the values to be of the Qux case. This predicate already…
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
9
votes
2 answers

How to generate null strings for FsCheck tests

Using FsCheck, the F# version of the Haskell QuickCheck test library, to generate tests from C#, I found that the random string generator does not generate the null string. using FsCheck.Fluent; Spec.ForAny(s => s != null).QuickCheck(); //…
Alapago
  • 368
  • 2
  • 11
9
votes
2 answers

How do I register an Arbitrary instance in FsCheck and have xUnit use it?

I've got a type Average with a field count that's a positive int64 and a double field called sum. I made an arbitrary that generates valid instances with let AverageGen = Gen.map2 (fun s c -> Average(float(s),int64(int(c)))…
nimish
  • 4,755
  • 3
  • 24
  • 34
7
votes
1 answer

Generate random string with FsCheck using C#

I'd like to gradually integrate FsCheck in my C# test code (as first step). I'd like to randomly generate part of my input data. This is how I generate a random string: static string RandomString() { var kgen = Gen.Constant(Gen.Sized(g =>…
gsscoder
  • 3,088
  • 4
  • 33
  • 49
6
votes
3 answers

FsCheck integration with NUnit in C#

TL;DR: I'm not able to successfully use FsCheck with NUnit in C#: either: it tells me on stdout that the test failed but the test still appears green it tells me that it doesn't find any test to run or I don't understand how to apply in C# the doc…
gturri
  • 13,807
  • 9
  • 40
  • 57
6
votes
1 answer

Custom FsCheck Arbitrary type broken in Xunit but working in LINQPad and regular F# program

I'm trying to implement a custom Arbitrary that generates glob syntax patterns like a*c?. I think my implementation is correct, it's just that, when running the test with Xunit, FsCheck doesn't seem to be using the custom arbitrary Pattern to…
Good Night Nerd Pride
  • 8,245
  • 4
  • 49
  • 65
6
votes
1 answer

FsCheck: How to generate test data that depends on other test data?

FsCheck has some neat default Arbitrary types to generate test data. However what if one of my test dates depends on another? For instance, consider the property of string.Substring() that a resulting substring can never be longer than the input…
Good Night Nerd Pride
  • 8,245
  • 4
  • 49
  • 65
6
votes
2 answers

Expecto FsCheck getting stack overflow exception when generating string

I'm trying to learn how to use FsCheck properly, and integrating it with Expecto at the moment. I can get property tests to run if I'm using the default FsCheck config, but when I try to use my own Generator, it causes a stack overflow…
kaeedo
  • 458
  • 2
  • 13
6
votes
1 answer

How to use FsCheck to generate random numbers as input for property-based testing

I thought it's time to try out FsCheck but it proves tougher than I thought. There's a lot of documentation on Arb, generators and so on, but there doesn't seem to be any guidance in how to apply that knowledge. Or I'm just not getting it. What may…
Abel
  • 56,041
  • 24
  • 146
  • 247
6
votes
1 answer

fscheck doesn't generate random enough data

I'm playing with FsCheck so I have this implementation: let add a b = if a > 100 then failwith "nasty bug" else a + b ...and this FsCheck based test: fun (a:int) -> (add a 0) = a |> Check.QuickThrowOnFailure and the test never fails.…
vidi
  • 2,056
  • 16
  • 34
1
2 3
10 11