通过多次传递输入来测试函数
Testing a Function by Passing Input Multiple Times
我正在通过测试学习 Go。我从“Head First Go”的第 2 章末尾得到了以下程序。
它让用户尝试猜测 1 到 10 之间的数字:
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()
?
我想通过将 guess()
的 return 值与 generateRandomNumber()
进行比较来执行测试。
您可以更改 guess
函数以从输入中获取 reader,这样我们就可以传递给它任何我们想要的 reader:在 main 中我们传递 stdin
reader 并且在测试中我们通过模拟 reader:
guess.go
package main
import (
"bufio"
"fmt"
"log"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
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(reader *bufio.Reader) (int, error) {
target := generateRandomNumber()
fmt.Println("Guess a number between 1 and 10")
for attempts := 0; attempts < 3; attempts++ {
fmt.Println("You have", 3-attempts, "guesses left")
userVal, err := reader.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, nil
} else if answer > target {
fmt.Println("Your guess was HIGHER")
} else if answer < target {
fmt.Println("Your guess was LOWER")
}
}
fmt.Printf("Sorry, you've run out of attempts... The correct value is %d\n", target)
return target, fmt.Errorf("attempts is over")
}
func main() {
reader := bufio.NewReader(os.Stdin)
guess(reader)
}
用于测试:
guess_test.go
package main
import (
"bufio"
"fmt"
"strings"
"testing"
)
func TestRandomNumberOk(t *testing.T) {
want := generateRandomNumber()
msg := fmt.Sprintf("3\n4\n%d\n", want)
reader := strings.NewReader(msg)
r := bufio.NewReader(reader)
_, err := guess(r)
if err != nil {
t.Errorf("guess must successfull")
}
}
func TestRandomNumberFail(t *testing.T) {
want := generateRandomNumber()
msg := fmt.Sprintf("3\n4\n%d\n", want+1)
reader := strings.NewReader(msg)
r := bufio.NewReader(reader)
_, err := guess(r)
if err == nil {
t.Errorf("guess must unsuccessfull")
}
}
我不得不更改你的猜测 return 值,因为它何时成功与否是未知的
我正在通过测试学习 Go。我从“Head First Go”的第 2 章末尾得到了以下程序。
它让用户尝试猜测 1 到 10 之间的数字:
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()
?
我想通过将 guess()
的 return 值与 generateRandomNumber()
进行比较来执行测试。
您可以更改 guess
函数以从输入中获取 reader,这样我们就可以传递给它任何我们想要的 reader:在 main 中我们传递 stdin
reader 并且在测试中我们通过模拟 reader:
guess.go
package main
import (
"bufio"
"fmt"
"log"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
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(reader *bufio.Reader) (int, error) {
target := generateRandomNumber()
fmt.Println("Guess a number between 1 and 10")
for attempts := 0; attempts < 3; attempts++ {
fmt.Println("You have", 3-attempts, "guesses left")
userVal, err := reader.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, nil
} else if answer > target {
fmt.Println("Your guess was HIGHER")
} else if answer < target {
fmt.Println("Your guess was LOWER")
}
}
fmt.Printf("Sorry, you've run out of attempts... The correct value is %d\n", target)
return target, fmt.Errorf("attempts is over")
}
func main() {
reader := bufio.NewReader(os.Stdin)
guess(reader)
}
用于测试:
guess_test.go
package main
import (
"bufio"
"fmt"
"strings"
"testing"
)
func TestRandomNumberOk(t *testing.T) {
want := generateRandomNumber()
msg := fmt.Sprintf("3\n4\n%d\n", want)
reader := strings.NewReader(msg)
r := bufio.NewReader(reader)
_, err := guess(r)
if err != nil {
t.Errorf("guess must successfull")
}
}
func TestRandomNumberFail(t *testing.T) {
want := generateRandomNumber()
msg := fmt.Sprintf("3\n4\n%d\n", want+1)
reader := strings.NewReader(msg)
r := bufio.NewReader(reader)
_, err := guess(r)
if err == nil {
t.Errorf("guess must unsuccessfull")
}
}
我不得不更改你的猜测 return 值,因为它何时成功与否是未知的