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

Darian311:

据我所知,我正在完美地遵循“进行测试”所需的结构。我看不到可以在其他软件包中运行的测试的差异。“构建”效果很好。我越来越

./HelloTemplate_test.go:3:导入但未使用:“ testing” ./HelloTemplate_test.go:5:未定义:在Testing.T中进行测试

我想念什么?

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
}
abhink:

您在测试函数中使用了错误的类型:

// 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 ")
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用React,Jest,React-Testing-Library测试失败的案例

如何使用Go for Unit Testing指定App Engine模板的路径?

Testing Functions with the same name in GO

使用react-testing-library测试mxGraph

导入Node核心模块会破坏Karma测试(使用@ open-wc / testing-karma)

Junit Testing JAXBContext引发JAXBException-使用Mockito测试失败方案

使用存储测试方法失败。Laravel中的Illuminate \ Http \ Testing \ imagepng()

反应 Fromik。Material-ui表单提交事件测试使用react-testing-library失败

Django Testing视图模板上下文

为什么JUnit Testing异常总是失败?

在React Testing库中测试click事件

react-testing-library: 测试 onClick 事件

使用 maven-plugin-testing-harness 进行困难的 mojo 测试

使用@ testing-library / react测试Ag-grid

使用React-Testing-Library运行Jest测试时出错

如何使用react-testing-library测试react-select

使用React Testing库的ReactJS测试中的Typescript错误

使用react-testing-library进行测试卸载

使用react-testing-library测试react-hotkeys

使用testing.TB进行测试定制

react-testing-library为什么使用测试ID

如何使用 react-testing-library 测试 DOM 节点的外观

使用 jest 和 react-testing-library 测试连接的组件

使用 jest 和 testing-library 测试组件内的函数

流明8不使用.env.testing

Angular 5 Karma测试因导入而失败

React-Testing:调试,为什么使用Material UI的网格对组件进行的这种开玩笑的快照测试失败

如何将xcodebuild与-only-testing和-skip-testing标志一起使用?

Angular 2 Testing组件给出“错误:未捕获(承诺):错误:模板解析错误”