Chapter 14 - Automated Testing
Exercise 1: Testing a Package
Here is code for an ordinals
package, with an Ordinal
function that converts the integer 1
to the string 1st
, 2
to 2nd
, and so on. We have intentionally left out the code that converts 3
to 3rd
, so that we have a case where tests should fail.
You can either create this file manually, or install it automatically with:
$ go get github.com/jaymcgavren/ordinals
$GOPATH/src/github.com/jaymcgavren/ordinals/ordinals.go
Your goal is to create automated tests with the following expectations:
Ordinal Parameter | Expected Return Value |
---|---|
1 |
"1st" |
2 |
"2nd" |
3 |
"3rd" (this test should fail) |
4 |
"4th" |
11 |
"11th" |
21 |
"21st" |
For now, you can use a failure message of "didn't match expected value"
for every test.
Solution
$GOPATH/src/github.com/jaymcgavren/ordinals/ordinals_test.go
Output:
--- FAIL: TestThree (0.00s)
ordinals_test.go:19: didn't match expected value
FAIL
FAIL github.com/jaymcgavren/ordinals 0.006s