6

我正在玩 FsCheck 所以我有这个实现:

let add a b = 
    if a > 100
    then failwith "nasty bug"
    else a + b

...以及这个基于 FsCheck 的测试:

fun (a:int) -> (add a 0) = a
|> Check.QuickThrowOnFailure

并且测试永远不会失败。我的猜测是随机生成器产生的 100 个值永远不会大于 100。

这些值不应该更“随机”吗?

4

1 回答 1

11

当您使用Check.QuickThrowOnFailure时,它使用配置Config.QuickThrowOnFailure,它具有以下值:

> Config.QuickThrowOnFailure;;
val it : Config =
  {MaxTest = 100;
   MaxFail = 1000;
   Replay = null;
   Name = "";
   StartSize = 1;
   EndSize = 100;
   QuietOnSuccess = false;
   Every = <fun:get_Quick@342>;
   EveryShrink = <fun:get_Quick@343-1>;
   Arbitrary = [];
   Runner = <StartupCode$FsCheck>.$Runner+get_throwingRunner@355;}

这里要考虑的重要值是StartSize,但特别是EndSize。FsCheck 中的一些生成器使用大小上下文来确定它生成的值的大小或范围。

如果您将其更改EndSize为例如1,000,您可以使您的测试失败:

> Check.One({Config.QuickThrowOnFailure with EndSize = 1000}, fun (a:int) -> (add a 0) = a);;
System.Exception: Falsifiable, after 15 tests (0 shrinks) (StdGen (1912816373,296229213)):
Original:
101
with exception:
> System.Exception: nasty bug
   at FSI_0040.add(Int32 a, Int32 b)
   at FSI_0055.it@69-6.Invoke(Int32 a)
   at FsCheck.Testable.evaluate[a,b](FSharpFunc`2 body, a a) in C:\Users\Kurt\Projects\FsCheck\FsCheck\src\FsCheck\Testable.fs:line 161

   at <StartupCode$FsCheck>.$Runner.get_throwingRunner@365-1.Invoke(String message) in C:\Users\Kurt\Projects\FsCheck\FsCheck\src\FsCheck\Runner.fs:line 365
   at <StartupCode$FsCheck>.$Runner.get_throwingRunner@355.FsCheck-IRunner-OnFinished(String , TestResult ) in C:\Users\Kurt\Projects\FsCheck\FsCheck\src\FsCheck\Runner.fs:line 365
   at FsCheck.Runner.check[a](Config config, a p) in C:\Users\Kurt\Projects\FsCheck\FsCheck\src\FsCheck\Runner.fs:line 275
   at <StartupCode$FSI_0055>.$FSI_0055.main@()
Stopped due to error
于 2016-11-14T15:09:56.890 回答