通过多次传递输入来测试函数

我正在通过测试学习围棋。从“Head First Go”的第 2 章末尾,我得到了以下程序。


它让用户尝试从 1 到 10 猜测一个数字 3 次:


guess.go


package main


import (

    "fmt"

    "math/rand"

    "time"

    "bufio"

    "os"

    "log"

    "strconv"

    "strings"

)


func generateRandomNumber() int {

    rand.Seed(time.Now().Unix())       // seed value based on time to generate non-deterministic random values

    randomNum := rand.Intn(10) + 1     // range: [0, 10); produces the same value without Seed(), i.e., pseudo-random

    return randomNum

}


func guess() int {

    var success bool = false

    target := generateRandomNumber()


    fmt.Println("Guess a number between 1 and 10")

    guess  := bufio.NewReader(os.Stdin)


    for attempts := 0; attempts < 3; attempts++ {

        fmt.Println("You have", 3-attempts, "guesses left")

        userVal, err := guess.ReadString('\n')

        if err != nil {

            log.Fatal(err)

        }

    

        input := strings.TrimSpace(userVal)

        answer, err := strconv.Atoi(input)

        if err != nil {

            log.Fatal(err)

        }

    

        if answer == target {

            fmt.Println("Congratulations !!")

            return answer

        } else if answer > target {

            fmt.Println("Your guess was HIGHER")

        } else if answer < target {

            fmt.Println("Your guess was LOWER")

        }

    }


    if !success {

        fmt.Printf("Sorry, you've run out of attempts... The correct value is %d\n", target)

        return target   

    }

    return 0

}


func main() {

    guess()

}


guess_test.go


package main


import (

    "testing"

)


func TestRandomNumber(t *testing.T) {

    want := generateRandomNumber()

    

    if 7 != want {

        t.Fail()

        t.Logf("Incorrect guess; The random number was %d", want)

    }

}


如何guess()通过传入三个不同的输入进行测试?


我想通过比较 with 的返回值来进行guess()测试generateRandomNumber()。


HUH函数
浏览 76回答 1
1回答

繁花如伊

您可以更改guess函数以从输入中获取阅读器,这样我们就可以将它传递给我们想要的任何阅读器:在主要我们通过stdin阅读器,在测试中我们通过模拟阅读器:guess.gopackage mainimport (&nbsp; &nbsp; "bufio"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "log"&nbsp; &nbsp; "math/rand"&nbsp; &nbsp; "os"&nbsp; &nbsp; "strconv"&nbsp; &nbsp; "strings"&nbsp; &nbsp; "time")func generateRandomNumber() int {&nbsp; &nbsp; rand.Seed(time.Now().Unix())&nbsp; &nbsp;// seed value based on time to generate non-deterministic random values&nbsp; &nbsp; randomNum := rand.Intn(10) + 1 // range: [0, 10); produces the same value without Seed(), i.e., pseudo-random&nbsp; &nbsp; return randomNum}func guess(reader *bufio.Reader) (int, error) {&nbsp; &nbsp; target := generateRandomNumber()&nbsp; &nbsp; fmt.Println("Guess a number between 1 and 10")&nbsp; &nbsp; for attempts := 0; attempts < 3; attempts++ {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("You have", 3-attempts, "guesses left")&nbsp; &nbsp; &nbsp; &nbsp; userVal, err := reader.ReadString('\n')&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; input := strings.TrimSpace(userVal)&nbsp; &nbsp; &nbsp; &nbsp; answer, err := strconv.Atoi(input)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if answer == target {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Congratulations !!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return answer, nil&nbsp; &nbsp; &nbsp; &nbsp; } else if answer > target {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Your guess was HIGHER")&nbsp; &nbsp; &nbsp; &nbsp; } else if answer < target {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Your guess was LOWER")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("Sorry, you've run out of attempts... The correct value is %d\n", target)&nbsp; &nbsp; return target, fmt.Errorf("attempts is over")}func main() {&nbsp; &nbsp; reader := bufio.NewReader(os.Stdin)&nbsp; &nbsp; guess(reader)}用于检测:guess_test.gopackage mainimport (&nbsp; &nbsp; "bufio"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "strings"&nbsp; &nbsp; "testing")func TestRandomNumberOk(t *testing.T) {&nbsp; &nbsp; want := generateRandomNumber()&nbsp; &nbsp; msg := fmt.Sprintf("3\n4\n%d\n", want)&nbsp; &nbsp; reader := strings.NewReader(msg)&nbsp; &nbsp; r := bufio.NewReader(reader)&nbsp; &nbsp; _, err := guess(r)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; t.Errorf("guess must successfull")&nbsp; &nbsp; }}func TestRandomNumberFail(t *testing.T) {&nbsp; &nbsp; want := generateRandomNumber()&nbsp; &nbsp; msg := fmt.Sprintf("3\n4\n%d\n", want+1)&nbsp; &nbsp; reader := strings.NewReader(msg)&nbsp; &nbsp; r := bufio.NewReader(reader)&nbsp; &nbsp; _, err := guess(r)&nbsp; &nbsp; if err == nil {&nbsp; &nbsp; &nbsp; &nbsp; t.Errorf("guess must unsuccessfull")&nbsp; &nbsp; }}我不得不更改您的猜测返回值,因为它何时成功与否未知
打开App,查看更多内容
随时随地看视频慕课网APP