Go: mock return value of a function in unit test

huangapple go评论148阅读模式
英文:

Go: mock return value of a function in unit test

问题

我已经在golang中创建了一个小脚本(我的第一个golang项目)。

示例:

  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. )
  6. func main() {
  7. i := rand.Intn(10)
  8. foo := foo(i)
  9. if foo {
  10. fmt.Printf("%d是偶数!", i)
  11. // 更多代码...
  12. } else {
  13. fmt.Printf("%d是奇数!", i)
  14. // 更多代码...
  15. }
  16. }
  17. func foo(i int) bool {
  18. if i%2 == 0 {
  19. return true
  20. } else {
  21. return false
  22. }
  23. }

我想为每个函数创建一个小的单元测试。
对于"main()"函数,我想模拟函数"foo()"的返回值,因为我不想测试"foo()",而是测试main()函数的其余代码。

我正在寻找一种简单的方法来存根/模拟返回值。
我只找到了使用结构体或接口等进行模拟。但是我在代码中没有使用这些元素(这是一个简单的项目)。

英文:

I have created a small script in golang (my first golang project).

Example:

  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. )
  6. func main() {
  7. i := rand.Intn(10)
  8. foo := foo(i)
  9. if foo {
  10. fmt.Printf("%d is even!", i)
  11. // more code ...
  12. } else {
  13. fmt.Printf("%d is odd!", i)
  14. // more code ...
  15. }
  16. }
  17. func foo(i int) bool {
  18. if i%2 == 0 {
  19. return true
  20. } else {
  21. return false
  22. }
  23. }

I would like to create a small unit test for each function.
For "main()", I want to mock the return value of function "foo()", because I won't test "foo()", but the rest of main()-code.

I'm looking for an easy way to stub/mock the return value.
I just found mocks with struct or interaces etc. But I didn't use these elements in code (it's a simple project).

答案1

得分: 3

使用一个真实、简洁、可重现的示例:如何创建一个简洁、可重现的示例

例如,在Go语言中:

  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. )
  6. func isEven(i int) bool {
  7. return i%2 == 0
  8. }
  9. func side(n int) string {
  10. if isEven(n) {
  11. return "right"
  12. } else {
  13. return "left"
  14. }
  15. }
  16. func main() {
  17. n := 1 + rand.Intn(10)
  18. hand := side(n)
  19. fmt.Printf("Number %d is on the %s-hand side of the street.\n", n, hand)
  20. }

https://go.dev/play/p/a0SudgaRCnu

输出结果为:

  1. Number 9 is on the left-hand side of the street.

使用Go的testing包side函数进行单元测试。你也可以直接对isEven函数进行单元测试。main函数不应包含任何你想要进行单元测试的代码。

  1. package main
  2. import (
  3. "testing"
  4. )
  5. func TestSide(t *testing.T) {
  6. n := 7
  7. got := side(n)
  8. want := "left"
  9. if got != want {
  10. t.Errorf("side(%d) = %s; want %s", n, got, want)
  11. }
  12. }
英文:

Use a realistic, minimal, reproducible example: How to create a Minimal, Reproducible Example.

For example, in Go,

  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. )
  6. func isEven(i int) bool {
  7. return i%2 == 0
  8. }
  9. func side(n int) string {
  10. if isEven(n) {
  11. return "right"
  12. } else {
  13. return "left"
  14. }
  15. }
  16. func main() {
  17. n := 1 + rand.Intn(10)
  18. hand := side(n)
  19. fmt.Printf("Number %d is on the %s-hand side of the street.\n", n, hand)
  20. }

https://go.dev/play/p/a0SudgaRCnu

  1. Number 9 is on the left-hand side of the street.

Use the Go testing package to unit test the side function. You might also unit test the isEven function directly. The main function should not contain any code that you want to unit test.

  1. package main
  2. import (
  3. "testing"
  4. )
  5. func TestSide(t *testing.T) {
  6. n := 7
  7. got := side(n)
  8. want := "left"
  9. if got != want {
  10. t.Errorf("side(%d) = %s; want %s", n, got, want)
  11. }
  12. }

huangapple
  • 本文由 发表于 2023年4月28日 04:11:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76124276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定