如何在for循环中生成Fuchu Unit testCases

时间:2017-09-17 03:30:30

标签: f#

我正在尝试使用Fuchu在F#中生成随机单元测试。

我正在使用普通的控制台应用程序在Vs 2017中执行我的代码。

问题是GeneraterandomTests无法编译。

open Fuchu

let OpenOrSenior xs = 
    [for (x:List<int>) in xs do
        if x.[0] >= 55 && x.[1] > 7 then yield "Senior" else yield "Open"]

let rand = new Random()

type ClubMember = {Age:int;Handi:int}

let values =  [
        {Age = 10;Handi=2}
        {Age=25;Handi=9}
        {Age=75;Handi=2}
        {Age=20;Handi=25}
        {Age=1;Handi=1}
        {Age=90;Handi=8}
        {Age=79;Handi=12}
        {Age=99;Handi=18}
        {Age=75;Handi=8}
        {Age=76;Handi=9}
    ]


let SelectMember (mem:ClubMember) = 
    match mem with 
    | x when (mem.Age >= 55) && (mem.Handi > 7) -> "Senior"
    | _ -> "Open"


let PutAttributesInList (mem:ClubMember) = 
    match mem with 
    | _ -> [mem.Age; mem.Handi]

let Randomtests expected actual = testCase "Randomness"  <|
                                        fun _ ->  Assert.Equal("expected == actual?", OpenOrSenior expected, actual)

[<Tests>]
let GeneraterandomTests = 

    for i in 1..10 do 
        let mutable expected:int list list = []
        let mutable actual: string list = []
        for _i in 1..5 do 
            let mutable rndNumb = rand.Next(0,9)
            actual <- SelectMember values.[rndNumb] :: actual
            expected <- PutAttributesInList values.[rndNumb] :: expected
        Randomtests expected actual

1 个答案:

答案 0 :(得分:0)

这适用于Visual Studio 2017并编译和测试,它与Fuchu

// random test cases...
open Fuchu
open System
let rand = new Random()

type ClubMember = {Age:int;Handi:int}

let values =  [
        {Age = 10;Handi=2}
        {Age=25;Handi=9}
        {Age=75;Handi=2}
        {Age=20;Handi=25}
        {Age=1;Handi=1}
        {Age=90;Handi=8}
        {Age=79;Handi=12}
        {Age=99;Handi=18}
        {Age=75;Handi=8}
        {Age=76;Handi=9}
    ]


let SelectMember (mem:ClubMember) = 
    match mem with 
    | x when (mem.Age >= 55) && (mem.Handi > 7) -> "Senior"
    | _ -> "Open"


let PutAttributesInList (mem:ClubMember) = 
    match mem with 
    | _ -> [mem.Age; mem.Handi]

let Randomtests expected actual =  Assert.Equal("expected == actual?", OpenOrSenior expected, actual)
type _testcases = {_expected : int list list ; _actual : string list}
let mutable testCasesList = []

let GeneraterandomTests = 
    for i in 1..10 do 
        let mutable expected:int list list = []
        let mutable actual: string list = []
        for _i in 1..5 do 
            let mutable rndNumb = rand.Next(0,9)
            actual <- SelectMember values.[rndNumb] :: actual
            expected <- PutAttributesInList values.[rndNumb] :: expected
        testCasesList <- {_expected = expected; _actual = actual} :: testCasesList

[<Tests>]
let __suite = 
    testList "Random Tests!" [testCase "Should return test results.." <| (fun _ -> 
            for i in testCasesList do 
                Randomtests i._expected i._actual)
                ]