Table driven tests and error/output testing

151 views
Skip to first unread message

Amit Saha

unread,
Oct 20, 2020, 12:03:31 AM10/20/20
to golang-nuts
Hi all, Consider the following test configuration:


func TestHandleCommand(t *testing.T) {

type expectedResult struct {
output string
err error
}
type testConfig struct {
args []string
result expectedResult
}

testConfigs := []testConfig{
testConfig{
args: []string{"-h"},
result: expectedResult{
err: nil,
output: `Expected output`,
},
},
}

Then, I do this:


for _, tc := range testConfigs {
byteBuf := new(bytes.Buffer)
w := bufio.NewWriter(byteBuf)

err := handleCommand(w, tc.args)
if tc.result.err == nil && err != nil {
t.Errorf("Expected nil error, got %v", err)
}

if tc.result.err != nil && err.Error() != tc.result.err.Error() {
t.Errorf("Expected error %v, got %v", tc.result.err, err)
}

if len(tc.result.output) != 0 {
w.Flush()
gotOutput := byteBuf.String()
if tc.result.output != gotOutput {
t.Errorf("Expected output to be: %v, Got: %v",
tc.result.output, gotOutput)
}
}
}
}

The above pattern works for me since the function may return an error
and/or it may have something it writes to the provided writer, w.

Is there a more concise way to write this?



Thanks,
Amit.

Tamás Gulácsi

unread,
Oct 20, 2020, 1:13:13 AM10/20/20
to golang-nuts
Use bytes.Buffer or strings.Builder directly - no need for the bufio.Writer.
bufio.Writer only speeds up writing by calling the underlying Writer less, with bigger slices.
Here you Write into a memory, just as bufio.Writer.

Also, you can save some allocations by creating the buffer once outside of the loop,
and Reset() it in each cycle before calling handleCommand.

Amit Saha

unread,
Oct 20, 2020, 2:51:56 AM10/20/20
to Tamás Gulácsi, golang-nuts

On 20 Oct 2020, at 4:13 pm, Tamás Gulácsi <tgula...@gmail.com> wrote:

Use bytes.Buffer or strings.Builder directly - no need for the bufio.Writer.
bufio.Writer only speeds up writing by calling the underlying Writer less, with bigger slices.
Here you Write into a memory, just as bufio.Writer.

Also, you can save some allocations by creating the buffer once outside of the loop,
and Reset() it in each cycle before calling handleCommand.

Thank you, appreciate the suggestions.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/2759fea4-a3d6-42ee-b3e4-2c0ac208f2b9n%40googlegroups.com.

seank...@gmail.com

unread,
Oct 20, 2020, 3:25:45 PM10/20/20
to golang-nuts
you should be able to unconditionally compare the output without doing the length check

roger peppe

unread,
Oct 20, 2020, 5:33:18 PM10/20/20
to Amit Saha, golang-nuts
It looks like you're testing a top level command. You might want to consider using the testscript package, which provides a very concise way of end-to-end testing this kind of thing.

e.g.

    mycommand -h
    cmp stdout expect-stdout

    -- expect-stdout --
    Expected output


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Amit Saha

unread,
Oct 22, 2020, 8:34:01 PM10/22/20
to roger peppe, golang-nuts

On 21 Oct 2020, at 8:31 am, roger peppe <rogp...@gmail.com> wrote:

It looks like you're testing a top level command. You might want to consider using the testscript package, which provides a very concise way of end-to-end testing this kind of thing.

e.g.

    mycommand -h
    cmp stdout expect-stdout

    -- expect-stdout --
    Expected output


This looks interesting, thank you. I will have a look.
Reply all
Reply to author
Forward
0 new messages