Go Assertion Frameworks

agolangf · · 152 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>What options are out there? I have played around with testify but it seems like the PRs on the GitHub repo are all quite stale? also I really want to be able to name my tests in snake case and not camel case.</p> <hr/>**评论:**<br/><br/>nemith: <pre><p>My suggestion is to not use an assertion library. Doing standard <code>if</code> statements and calling t.Errorf or t.Fatalf goes a long way and I think it makes for more meaningful tests.</p> <p>The assertion libraries are really light wrappers around this and there is no reason why they would need &#34;updating&#34;.</p> <p>As for test names, if you want to use the Go testing framework the function must start with Test. The rest is up to you, however i would strongly suggest trying to stick with proper go style. It may feel weird at first, but it&#39;s better to get used to it, as all the libraries and stdlib are going to follow that styling anyway.</p></pre>mcandre: <pre><p>Seconded. I personally hate having to relearn yet another stupid framework when adding tests to a suite. Less is more.</p></pre>jerf: <pre><p>One thing that I suspect is an apparently blocker for some people is when they don&#39;t know about <a href="https://golang.org/pkg/reflect/#DeepEqual" rel="nofollow">reflect.DeepEqual</a>. But it is in the standard library.</p> <p>There is a library or libraries on github that can stand in for reflect.DeepEqual and tell you what the differences are instead of just returning true or false. I don&#39;t leave them in my code in the long term but it&#39;s useful for debugging. (I&#39;d go find them but I&#39;m getting rushed out the door.)</p> <p>Edit: Now that I&#39;m not under time pressure, <a href="https://godoc.org/github.com/go-test/deep" rel="nofollow">https://godoc.org/github.com/go-test/deep</a> .</p></pre>nemith: <pre><p>I have found Google&#39;s own <code>cmp</code> <a href="https://github.com/google/go-cmp" rel="nofollow">https://github.com/google/go-cmp</a> to be very friendly for these needs.</p></pre>GodsBoss: <pre><p>I used to use <a href="https://godoc.org/github.com/stretchr/testify/assert" rel="nofollow">testify/assert</a> (you seem to refer to it, too), but have switched to vanilla <a href="https://golang.org/pkg/testing" rel="nofollow">testing</a> for several reasons.</p> <p>First, you have to manage one more dependency. This may be fine for bigger projects where you need dependency management anyways, but for small, self-contained libraries this could mean introducing it, which is a big step, relatively.</p> <p>Second, it is something every developer has to learn. It is fair game to assume that developers know the standard testing package, and if not, they would have to learn it either way. Learning an assertion API is an additional obstacle.</p> <p>There are also disadvantages, I must admit. You have to write a bit more code, especially for tests like the ones I wrote recently where items (with specific properties) should be contained in a slice, but at what index wasn&#39;t defined. This can mean writing a few lines, wheras with an assertion framework you would have written a one-liner.</p> <p>But I still think the cons (against an assertion framework) outweigh the pros.</p></pre>kostix: <pre><p>I very much advise you to take your time and watch <a href="https://www.youtube.com/watch?v=yi5A3cK1LNA" rel="nofollow">The Three Fallacies of Dependencies</a> by Blake Miserany — it&#39;s fun to watch and it deals precisely with your use case ;-)</p></pre>rz2yoj: <pre><p>I&#39;ve been using testify for a long time now, on both big and small projects, and I&#39;m really uncompelled by statements like &#34;vanilla testing makes for more meaning tests&#34; or &#34;it&#39;s one more thing every developer has to learn&#34;. I&#39;m not convinced of the former, and while the latter one is true, the learning curve for testify is very small. If you&#39;re just using testify&#39;s assert/require packages, every developer that looks at your tests is going to find it intuitively obvious how to use it.</p> <p>I&#39;ve found time and time again that when your project uses only the standard testing package, the output when tests fail tends to be completely useless. People don&#39;t like spending a lot of time writing tests and often just throw in <code>t.Fail()</code> statements rather than figuring out and printing the actual and expected outputs of their test subjects.</p> <p>Don&#39;t be put off by testify&#39;s lack of updates. Its assert/require packages are pretty much feature complete, and I would recommend them.</p></pre>adamcolton: <pre><p>I&#39;d second this. I&#39;ve had great results with testify/assert. I like it&#39;s functions for Equal, Len, NoError and NotNil. That&#39;s about 90% of what I use it for. And (this may be obvious, but still...) using it doesn&#39;t mean you have to only use it. Write vanilla tests where it makes sense, use the assert helpers where they make sense.</p> <p>And the point about formatting is not a trivial one. The assert.Equal has a nice feature that if you give it two byte slices and they are not equal it will show just the differences. That saved me a big headache once.</p> <p>Finally, slightly off topic but... write <a href="https://dave.cheney.net/2013/06/09/writing-table-driven-tests-in-go" rel="nofollow">table tests</a> where you can.</p></pre>ar1819: <pre><p>testify and goconvey are amongst most used. Also, you might want to look into github.com/kindrid/gotest.</p> <p>But I agree with others - try built-in test package and see if it works for you.</p></pre>mmatczuk: <pre><p>Try <a href="https://github.com/google/go-cmp" rel="nofollow">https://github.com/google/go-cmp</a> it usually solves all the problems with assertions. You do a normal if and get a diff like in python or groovy.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:701969077

152 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传