Golang单元测试用户输入

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

Golang unit testing user input

问题

我正在尝试以测试驱动开发(TDD)的思维方式学习Go语言。我在理解测试方面遇到了困难。

在下面的示例中,我提示用户输入,进行一些验证并打印结果。我为此编写了一个测试(测试通过),但我觉得它没有触发验证部分,所以我做错了什么。希望能得到一些建议。

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "os"
  7. "regexp"
  8. "strings"
  9. )
  10. func main() {
  11. response := askQuestion("What is your name?")
  12. fmt.Printf("Hello %s\n", response)
  13. }
  14. func askQuestion(question string) string {
  15. reader := bufio.NewReader(os.Stdin)
  16. answer := ""
  17. for {
  18. fmt.Printf("%s\n", question)
  19. input, err := reader.ReadString('\n')
  20. if err != nil {
  21. if err != io.EOF {
  22. panic(err)
  23. }
  24. break
  25. }
  26. if regexp.MustCompile(`[A-Z]{5}`).MatchString(strings.TrimSpace(input)) == true {
  27. answer = strings.TrimSpace(input)
  28. fmt.Printf("You entered %s\n", answer)
  29. break
  30. } else {
  31. fmt.Printf("3[31mYou must enter only 5 upper case letters.\n3[0m")
  32. continue
  33. }
  34. }
  35. return answer
  36. }
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "reflect"
  9. "strings"
  10. "testing"
  11. )
  12. func TestAskQuestion(t *testing.T) {
  13. expected := "foo"
  14. entered := "foo"
  15. askQuestion("What is your last name?")
  16. oldStdout := os.Stdout
  17. r, w, _ := os.Pipe()
  18. os.Stdout = w
  19. fmt.Println(entered)
  20. outC := make(chan string)
  21. go func() {
  22. var buf bytes.Buffer
  23. io.Copy(&buf, r)
  24. outC <- buf.String()
  25. }()
  26. w.Close()
  27. os.Stdout = oldStdout
  28. out := strings.TrimSpace(<-outC)
  29. b, _ := ioutil.ReadAll(os.Stdin)
  30. t.Log(string(b))
  31. if !reflect.DeepEqual(expected, out) {
  32. t.Fatalf("Test Status Failure Issue. Got: '%v' expected %s", out, expected)
  33. }
  34. }

以上是你提供的代码。

英文:

I am trying to learn go with a TDD mindset. I am stuck getting my head wrapped around testing.

In the example below, I am prompting a user for input, doing a little validation and printing the results. I wrote a test for it (which is passing) however I don't feel like it is hitting the validation portion, so I am doing something wrong. Any advice would be appreciated.

https://play.golang.org/p/FDpbof9Y20

  1. package main
  2. import (
  3. &quot;bufio&quot;
  4. &quot;fmt&quot;
  5. &quot;io&quot;
  6. &quot;os&quot;
  7. &quot;regexp&quot;
  8. &quot;strings&quot;
  9. )
  10. func main() {
  11. response := askQuestion(&quot;What is your name?&quot;)
  12. fmt.Printf(&quot;Hello %s\n&quot;,response)
  13. }
  14. func askQuestion(question string) string {
  15. reader := bufio.NewReader(os.Stdin)
  16. answer := &quot;&quot;
  17. for {
  18. fmt.Printf(&quot;%s\n&quot;, question)
  19. input, err := reader.ReadString(&#39;\n&#39;)
  20. if err != nil {
  21. if err != io.EOF {
  22. panic(err)
  23. }
  24. break
  25. }
  26. if regexp.MustCompile(`[A-Z]{5}`).MatchString(strings.TrimSpace(input)) == true {
  27. answer = strings.TrimSpace(input)
  28. fmt.Printf(&quot;You entered %s\n&quot;, answer)
  29. break
  30. } else {
  31. fmt.Printf(&quot;3[31mYou must enter only 5 upper case letters.\n3[0m&quot;)
  32. continue
  33. }
  34. }
  35. return answer
  36. }

https://play.golang.org/p/WcI4CRfle5

  1. package main
  2. import (
  3. &quot;bytes&quot;
  4. &quot;fmt&quot;
  5. &quot;io&quot;
  6. &quot;io/ioutil&quot;
  7. &quot;os&quot;
  8. &quot;reflect&quot;
  9. &quot;strings&quot;
  10. &quot;testing&quot;
  11. )
  12. func TestAskQuestion(t *testing.T) {
  13. expected := &quot;foo&quot;
  14. entered := &quot;foo&quot;
  15. askQuestion(&quot;What is your last name?&quot;)
  16. oldStdout := os.Stdout
  17. r, w, _ := os.Pipe()
  18. os.Stdout = w
  19. fmt.Println(entered)
  20. outC := make(chan string)
  21. go func() {
  22. var buf bytes.Buffer
  23. io.Copy(&amp;buf, r)
  24. outC &lt;- buf.String()
  25. }()
  26. w.Close()
  27. os.Stdout = oldStdout
  28. out := strings.TrimSpace(&lt;-outC)
  29. b, _ := ioutil.ReadAll(os.Stdin)
  30. t.Log(string(b))
  31. if !reflect.DeepEqual(expected, out) {
  32. t.Fatalf(&quot;Test Status Failure Issue. Got: &#39;%v&#39; expected %s&quot;, out, expected)
  33. }
  34. }

答案1

得分: 2

Go的测试需要放在以xyz_test.go命名的文件中,所以Playground不是熟悉单元测试功能的正确地方。

如果你在本地安装了Go,请运行命令go help test,以获得一个非常简要的介绍。

英文:

Go's tests need to live in files which are named xyz_test.go, so the playground is not the right place to familiarize yourself with the unit testing feature.

If you have go installed locally, run the command go help test, to get a very brief introduction.

huangapple
  • 本文由 发表于 2015年12月18日 08:00:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/34346496.html
匿名

发表评论

匿名网友

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

确定