17

Recently I hope to write a unit test for golang. The function is as below.

func (s *containerStats) Display(w io.Writer) error {
    fmt.Fprintf(w, "%s %s\n", "hello", "world")
    return nil
}

So how can I test the result of "func Display" is "hello world"?

wonderflow
  • 665
  • 2
  • 7
  • 18

1 Answers1

33

You can simply pass in your own io.Writer and test what gets written into it matches what you expect. bytes.Buffer is a good choice for such an io.Writer since it simply stores the output in its buffer.

func TestDisplay(t *testing.T) {
    s := newContainerStats() // Replace this the appropriate constructor
    var b bytes.Buffer
    if err := s.Display(&b); err != nil {
        t.Fatalf("s.Display() gave error: %s", err)
    }
    got := b.String()
    want := "hello world\n"
    if got != want {
        t.Errorf("s.Display() = %q, want %q", got, want)
    }
}
Paul Hankin
  • 54,811
  • 11
  • 92
  • 118
  • Thank you very much for your help! It's just what I want! – wonderflow Apr 08 '15 at 04:26
  • 4
    Typo in this: you need either `&b` in the Display call *or* `b := new(bytes.Buffer)` since the `Write` method has a pointer receiver. – Dave C Apr 08 '15 at 17:53
  • Thanks Dave! I don't know it can be fixed so easy. :) – wonderflow Apr 09 '15 at 00:49
  • When I do this, String() returns "". I can tell that a write happened because Len() changed. As I pass the object as a Writer, I don't know what is reading it. Is there any way to still read the contents of the buffer? – Philippe Aug 13 '21 at 17:33