dousu1900 2016-07-31 07:20
浏览 144
已采纳

go模板的示例测试因导入而失败,并且未使用:“ testing”

As far as I can tell I'm following structure needed for 'go test' flawlessly. I don't see a discrepancy from tests I could run in other packages. 'go build' works fine. I'm getting

./HelloTemplate_test.go:3: imported and not used: "testing" ./HelloTemplate_test.go:5: undefined: Testing in Testing.T

What am I missing?

HelloTemplate.go

package templateprint

import "testing"

func TestRunTempl(t *Testing.T) {
    sweaters := Inventory{"wool", 17}
    tmpl := "{{.Count}} items are made of {{.Material}}"
    err := RunTempl(tmpl, sweaters)
    if err != nil {
        t.Error("Template failed ")
    }
}

HelloTemplate_test.go

package templateprint

import (
    "os"
    "text/template"
)

type Inventory struct {
    Material string
    Count    uint
}

func RunTempl(templ string, inv Inventory) error {
    tmpl, err := template.New("test").Parse(templ)
    if err != nil {
        return (err)
    }
    err = tmpl.Execute(os.Stdout, inv)
    if err != nil {
        return (err)
    }
    return nil
}
  • 写回答

1条回答 默认 最新

  • doushe8577 2016-07-31 08:17
    关注

    You are using an incorrect type in your test function:

    // tesintg.T, not Testing.T
    // T is a type defined in testing module
    func TestRunTempl(t *testing.T) { 
        sweaters := Inventory{"wool", 17}
        tmpl := "{{.Count}} items are made of {{.Material}}"
        err := RunTempl(tmpl, sweaters)
        if err != nil {
            t.Error("Template failed ")
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式