我是基于属性和单元测试的新手,在我的项目中我想要使用这种技术,但遗憾的是很容易说......我看了一下关于FsCheck.XUnit库的讨论,但那家伙正在测试数字函数(模数)...我想测试使用字符串,列表和数组的函数。也许你们可以提供一个提示或源代码我可以查看? 附:我所看到的每个地方都只有数字示例,看起来很容易测试。
我想测试一些功能:
let wordSplit (text:string) =
text.Split([|' ';'\n';'\r';'\t';'!';',';'.';'?';';';':'; '/'
;'\\';'-';'+'; '*'; '#';'(';')';'^';'"';'\'';'`'; '@';'~';'|'|]
,StringSplitOptions.RemoveEmptyEntries)
|> Array.toList
let rec matchTails (tail1 : string list) (tail2 : string list) =
match tail1, tail2 with
| h1::t1 , h2::t2 ->
if (h1=h2) then
matchTails t1 t2
else
false
| [], _ -> false
| _, [] -> true
let rec phraseProcessor (textH: string) (textT: string list) (phrases: string list list) (n:int) =
match phrases with
|[] -> n
| h :: t ->
match h with
|x when x.Head = textH && (matchTails (textT) (x.Tail)) ->
phraseProcessor (textH) (textT) (t) (n+1)
| _ ->
phraseProcessor (textH) (textT) (t) (n)
let rec wordChanger (phrases : string list list) (text:string list) (n:int)=
match text with
| [] -> n
| h :: t ->
wordChanger phrases t (phraseProcessor (h) (t) (phrases) (n))
答案 0 :(得分:1)
你对非整数有什么问题?
您可以查看https://fsharpforfunandprofit.com/posts/property-based-testing/他提供的字符串和自定义类型的示例...
当然,你也可以生成随机字符串!
let stringGenerator = Arb.generate<string>
// generate 3 strings with a maximum size of 1
Gen.sample 1 3 stringGenerator
// result: [""; "!"; "I"]
// generate 3 strings with a maximum size of 10
Gen.sample 10 3 stringGenerator
// result: [""; "eiX$a^"; "U%0Ika&r"]
最好的事情是发电机可以自己使用 用户定义的类型!
type Color = Red | Green of int | Blue of bool
let colorGenerator = Arb.generate<Color>
// generate 10 colors with a maximum size of 50
Gen.sample 50 10 colorGenerator
// result: [Green -47; Red; Red; Red; Blue true;
// Green 2; Blue false; Red; Blue true; Green -12]
https://fsharpforfunandprofit.com/posts/property-based-testing-2/
如果您想生成复杂类型:How does one generate a "complex" object in FsCheck?