doulan7166 2018-05-28 08:45
浏览 34

Golang-涵盖所有用例的文本/模板单元测试

I've the following example (similar of what we have in prod) "text/template" code which works OK, now I want to create a unit test for it that checks the function and also the text/template to see that I cover 100% of the code...

The problem here is how to do the text/template unit test which covers all the cases. I'm currently new to text/template and I want to make sure it's working as expected.

Please visit: https://play.golang.org/p/203Al36Zigk

This is the template:

const tmpl = `#!/bin/bash
{{- range .File.Dependency}}
echo {{.EchoText}}
{{- range .Install}}
submitting {{.name}}
{{- end}}
{{.TypeCommand}}
{{end}}

{{- range $k, $v := .API}}
echo {{$k}}
submitting {{$v}}
{{end}}
`
  • 写回答

2条回答 默认 最新

  • douzhao2047 2018-06-03 16:13
    关注

    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])
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?