Last active
July 8, 2019 20:18
-
-
Save mrcampbell/379d4afb36776d5dbac7a3afe3a13482 to your computer and use it in GitHub Desktop.
Medium Article: Unit Tests: A Form of Documentation. Exhibit "Express expectations of the current functionality"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Test_functionA(t *testing.T) { | |
type args struct { | |
a int | |
b int | |
} | |
tests := []struct { | |
name string | |
args args | |
want int | |
wantErr bool | |
}{ | |
{ | |
name: "successful case of adding 6 and 10", | |
args: args{ | |
a: 6, | |
b: 10, | |
}, | |
want: 16, | |
wantErr: false, | |
}, | |
{ | |
name: "if one of the numbers is odd, return an error", | |
args: args{ | |
a: 6, | |
b: 9, | |
}, | |
want: 0, | |
wantErr: true, | |
}, | |
} | |
for _, tt := range tests { | |
t.Run(tt.name, func(t *testing.T) { | |
got, err := addEvenNumbers(tt.args.a, tt.args.b) | |
if (err != nil) != tt.wantErr { | |
t.Errorf("addEvenNumbers() error = %v, wantErr %v", err, tt.wantErr) | |
return | |
} | |
if got != tt.want { | |
t.Errorf("addEvenNumbers() = %v, want %v", got, tt.want) | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment