You should setup a template_test file dedicated to testing the output of your template file.
For that, looking at the sources of the golang text/template
package is always a good idea.
As an example (to be adapted to your case), you have src/text/template/example_test.go
which uses a classic table-driven test approach:
package template_test
import (
"log"
"os"
"strings"
"text/template"
)
func ExampleTemplate() {
// Define a template.
const letter = `
Dear {{.Name}},
{{if .Attended}}
It was a pleasure to see you at the wedding.
{{- else}}
It is a shame you couldn't make it to the wedding.
{{- end}}
{{with .Gift -}}
Thank you for the lovely {{.}}.
{{end}}
Best wishes,
Josie
`
// Prepare some data to insert into the template.
type Recipient struct {
Name, Gift string
Attended bool
}
var recipients = []Recipient{
{"Aunt Mildred", "bone china tea set", true},
{"Uncle John", "moleskin pants", false},
{"Cousin Rodney", "", false},
}
// Create a new template and parse the letter into it.
t := template.Must(template.New("letter").Parse(letter))
// Execute the template for each recipient.
for _, r := range recipients {
err := t.Execute(os.Stdout, r)
if err != nil {
log.Println("executing template:", err)
}
}
// Output:
// Dear Aunt Mildred,
//
// It was a pleasure to see you at the wedding.
// Thank you for the lovely bone china tea set.
//
// Best wishes,
// Josie
//
// Dear Uncle John,
//
// It is a shame you couldn't make it to the wedding.
// Thank you for the lovely moleskin pants.
//
// Best wishes,
// Josie
//
// Dear Cousin Rodney,
//
// It is a shame you couldn't make it to the wedding.
//
// Best wishes,
// Josie
}
For the assertion part, look in src/text/template/multi_test.go
which defines multiParseTest
as a structure with a template, *and an expected result, which allows to do assertions like:
result := tmpl.Root.String()
if result != test.results[i] {
t.Errorf("%s=(%q): got
\t%v
expected
\t%v", test.name, test.input, result, test.results[i])
}