草庐IT

Viewport-units

全部标签

unit-testing - go 模板的示例测试因导入和未使用 : "testing" 而失败

据我所知,我正在完美地遵循“进行测试”所需的结构。我没有发现与我可以在其他包中运行的测试有差异。“去build”工作正常。我得到了./HelloTemplate_test.go:3:importedandnotused:"testing"./HelloTemplate_test.go:5:undefined:TestinginTesting.T我错过了什么?HelloTemplate.gopackagetemplateprintimport"testing"funcTestRunTempl(t*Testing.T){sweaters:=Inventory{"wool",17}tmpl:

image-processing - 如何让 golang 读取 jpeg 并获得与 Python/C 相同的 unit8 值?

我有一个用于某些图像处理代码的测试jpeg&我正在尝试获取与python中的opencv和Python中的C和Pillow(通过scipy)相同的uint8值数组,它们都匹配这些值当我使用gimp打开jpeg时,我得到了。我尝试将颜色值移动8位,但这并不能准确地转换值-看起来有一些我不太明白的舍入。我知道gimp和opencv以及Pillow/scipy都使用libjpeg,所以我尝试使用https://github.com/pixiv/go-libjpeg并使用将图像转换为8位funcConvert(imgimage.Image)*image.RGBA{b:=img.Bounds()

unit-testing - golang 中的单元测试

我目前正在考虑在Go中为我的服务创建一些单元测试,以及在该功能之上构建的其他功能,我想知道在Go中进行单元测试的最佳方法是什么?我的代码如下所示:typeBBPeripheralstruct{client*http.Clientendpointstring}typeBBQuerystruct{Namestring`json:"name"`}typeBBResponsestruct{Brandstring`json:"brand"`Modelstring`json:"model"`...}typePeripheralstruct{BrandstringModelstring...}typ

unit-testing - assert_called_once() 或 assert_called_xyz().... 等效?

能够断言在我的测试中调用了多少次伪造/模拟方法对我来说很重要,我想知道在不使用testify之类的情况下执行此操作的最佳方法是什么。在我的例子中,对模拟方法的调用是一些递归调用的结果。假设我对各种动物进行了表驱动测试,我想断言Hello实际上是为某些测试调用的,但不是为其他测试调用的。在某些情况下,对于给定的测试(遍历一个slice)应该多次调用它。在我的表驱动测试中只添加一个计数器并对其进行断言是否合适?在我看来,也许有更好的方法可以做到这一点。如果我确实在hello方法中添加了一个计数器...应该在哪里处理和检查它。在假方法本身还是在测试等中?typefakeFarmService

unit-testing - 让 'go test -run <case>' 成功为 'go test' 的一般规则是什么?

我发现'gotest'PASS,但是如果我指定subtest,它会失败,这里我给一个全局变量sample,'gotest'会PASS,'gotest-runf/sample2'会失败.我想知道我应该遵循什么一般规则来防止此类问题?走吧packagemainimport"fmt"vargstringfuncf(sstring)string{g=g+sreturns+g}funcmain(){fmt.Println(f("a"))}t_test.gopackagemainimport("testing")funcTest_f(t*testing.T){tests:=[]struct{nam

unit-testing - 如何使用结构/接口(interface)来模拟依赖项以进行测试

我是新手...我的目标是单元测试我的ready()中的状态是否正在更新。我一直在看https://engineering.aircto.com/writing-testable-code-in-golang/并尝试找出如何使他们正在做的事情适应我的用例,尽可能填补golang知识的空白。我收到错误消息cannotusefakeSession(type*FakeSession)astype*discordgo.Sessioninargumenttoready但我不确定为什么我'我收到此错误。ma​​in.goimport("fmt""os""os/signal""syscall""git

unit-testing - Go - 表驱动测试辅助函数

我发现了很多关于表驱动测试的好例子,但似乎没有人写过关于创建辅助测试方法以传递要测试的函数的下一步。这样就不必为您要测试的每个函数重复这部分代码:funcTestFib(t*testing.T){for_,tt:=rangefibTests{actual:=Fib(tt.n)ifactual!=tt.expected{t.Errorf("Fib(%d):expected%d,actual%d",tt.n,tt.expected,actual)}}}//from:https://medium.com/@matryer/5-simple-tips-and-tricks-for-writin

unit-testing - 用单元测试覆盖主函数中的代码

Golang显示我只有50%的覆盖代码,而且我看到main中的代码没有被覆盖,我尝试搜索但没有找到任何解释如何覆盖main中的代码的内容。ma​​in.gopackagemainfuncSum(xint,yint)int{returnx+y}funcmain(){Sum(5,5)}ma​​in_test.gopackagemainimport("testing")funcTestSum(t*testing.T){total:=Sum(5,5)iftotal!=10{t.Fail()}} 最佳答案 测试文件通常紧挨着他们测试的代码。根

unit-testing - 如何为单元测试模拟标准包函数

我有三个功能:funcIsSymlinks(pathstring){......}func(c*MyClass)myFunc1(pathstring){...morecode...morecodeifIsSymlinks(path){realPath:=filepath.EvalSymlinks(path)}...morecode...morecode}funcmyFunc2(pathstring){...morecode...morecodeifIsSymlinks(path){realPath:=filepath.EvalSymlinks(path)}...morecode...m

unit-testing - 数据库单元测试

我希望就我正在编写的用于测试某些数据库条目的单元测试获得一些建议。如果没有找到记录,我正在测试的函数会为数据库播种。funcSeed(db*gorm.DB){vardata[]Datadb.Find(&data)iflen(data)==0{//doseeddefaultdata}}我似乎不太了解的是对iflen测试的测试。我正在使用一个测试数据库,所以我可以随时核对它,所以如果我只需要在函数上强制一个空数据库,这不是问题。该功能本身有效,我只是想确保我能做到这一点。任何建议都会很棒。谢谢! 最佳答案 这真的取决于,有很多方法可以根