TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

BDD testing for golang: Ginkgo and Gomega

62 点作者 tissarah超过 11 年前

11 条评论

buro9超过 11 年前
I wouldn&#x27;t mind a BDD tool written in Go, but not for this purpose.<p>What I would like is a tool that allowed testers and QA people to define tests using BDD for RESTful APIs.<p>Something along the lines of this syntax:<p><pre><code> Using https:&#x2F;&#x2F;example.com&#x2F;myapi With access_token [accessToken] With content_type application&#x2F;json When I create: { &quot;somejson&quot;: true &quot;foo&quot;: &quot;bar&quot; } Expect status 200 Expect returnData.somejson = true Expect returnData.foo = &quot;bar&quot; Store returnData.id as myid And Using https:&#x2F;&#x2F;example.com&#x2F;myapi&#x2F;{{myid}} When I get Expect status 200 Expect returnData.somejson = true Expect returnData.foo = &quot;bar&quot; And With access_token [accessToken] With content_type application&#x2F;json When I update: { &quot;somejson&quot;: true &quot;foo&quot;: &quot;foo&quot; } Expect status 200 Expect returnData.somejson = true Expect returnData.foo = &quot;foo&quot; Store returnData.id as myid And With access_token [accessToken] When I delete Expect status 200 And When I get Expect status 404 </code></pre> I&#x27;ve seen a few things like this written in NodeJS, such as apieasy and vows. But nothing with this level of succinctness.<p>And I&#x27;d like it to be in Go as I&#x27;d like to just give a single binary to the testers (rather than an &quot;install node, get project, fetch dependencies, resolve any issues, run the project&quot;).<p>When I read the headline, this is roughly what I hoped for. If I can find the time (unlikely), I&#x27;d build this. But then... maybe someone reading this knows something very close to this.
评论 #6526760 未加载
评论 #6526807 未加载
redbad超过 11 年前
I can&#x27;t help but feel like the authors of this tool (and ones like it) are fundamentally misunderstanding either the language, the purposes of testing, or maybe even static type systems in general.
评论 #6526525 未加载
Perceptes超过 11 年前
Looks great. I&#x27;m a huge fan of RSpec and really miss it in other languages. It didn&#x27;t occur to me before reading this post, but the ability to implement a testing framework like RSpec is a great way to gauge the expressiveness of the language.
karma_fountain超过 11 年前
Interesting choice of the Omega symbol for the actual symbol. Not sure if I like that or not. Vim users can use CTRL-K W * in insert mode to get a nice block character (at least on my build of vim) on their screen.<p>I prefer to use Gherkin syntax for by bdd testing, but the matchers are nice. The bootstrapper and helpers seem to fail hopelessly on windows, and the console writer outputs a lot of control characters which do not work in windows command prompt.
pjvds超过 11 年前
Here are 3 alternatives:<p>1. Mao: <a href="https://github.com/azer/mao" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;azer&#x2F;mao</a><p>2. Zen: <a href="https://github.com/pranavraja/zen" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pranavraja&#x2F;zen</a><p>3. GoConvey: <a href="https://github.com/smartystreets/goconvey" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;smartystreets&#x2F;goconvey</a>
SmileyKeith超过 11 年前
I love that Go is getting the kind of attention and use that leads to the creation of a framework like this from a relatively well known company. Although I can agree with some other comments here that it doesn&#x27;t particularly feel native to Go.
programminggeek超过 11 年前
This looks pretty cool, but for some reason I tend to like using the built in tools. Maybe I hate external dependencies or I have trust issues, but I&#x27;m way less likely to use this for that reason alone.
maurycy超过 11 年前
Please no with this Ruby madness in the Go world.
okpatil超过 11 年前
First of all, good job.<p>Are you planning to add selenium support?
mongrelion超过 11 年前
This is totally awesome.
onsi超过 11 年前
Hey all, this is the author of Ginkgo &amp; Gomega. I&#x27;m excited to see all this thoughtful conversation and figured I&#x27;d chime in with a response to keep the conversation going. (Apologies for the long post!)<p>- Godoc documentation is coming soon. Godoc is great for API-style documentation but not particularly appropriate for tutorial&#x2F;structured&#x2F;narrative-style documentation. Think golang.org&#x2F;pkg vs golang.org&#x2F;doc. I wanted to write the &#x2F;doc first as I believe it to be more valuable for beginners and is often overlooked by the go community. &#x2F;pkg is coming soon (a few days) and is much easier to write.<p>- @buro9: Golang is BDD-style in the same way that Cedar and Jasmine are BDD-style. This is not Cucumber (way too much DSL for me!) With that said, asynchronous testing support is baked right into Ginkgo and Gomega so the API-testing that you&#x27;d like would be easily expressed in Ginkgo and Gomega.<p>- Assertions that I have fundamentally misunderstood Golang or the distinction between dynamic and static typed languages are interesting and I&#x27;d like to address some of them.<p>BDD-style testing need not be limited to dynamically-typed language. Yes dynamically-typed languages need far more comprehensive test coverage to make up for the missing compiler. That&#x27;s why I tend to prefer statically-typed languages. To me BDD-style (vs the XUnit style) isn&#x27;t primarily about addressing these deficiencies in dynamic languages -- it&#x27;s about <i>expressiveness</i>.<p>To that end, I think that BDD and Golang go hand in hand. Let me explain. BDD is exceptionally good at describing the behavior of branching code. Golang is filled with branching code that needs to be described in test. Here&#x27;s a classic example in pseudo-go:<p><pre><code> func DoSomethingAwesome(dependencyA DepA, dependencyB DepB) awesome Awesome, err error { stuffA, err := dependencyA.Gimme() if err != nil { return Awesome{}, err } stuffB, err := dependencyB.Gimme() if err != nil { return Awesome{}, err } .... return awesome, nil } </code></pre> With Ginkgo you can cover these branches expressively:<p><pre><code> Describe(&quot;Doing something Awesome&quot;, func() { BeforeEach(...) &#x2F;&#x2F;common happy case setup Context(&quot;When all is well&quot;, func() { It(&quot;should be awesome&quot;, ...) }) Context(&quot;When dependencyA fails&quot;, func() { BeforeEach(...) &#x2F;&#x2F;set dependencA up for failure It(&quot;should return a zero Awesome&quot;, func() { ... Expect(awesome).To(BeZero()) }) It(&quot;should error&quot;, func() { ... Expect(err).To(HaveOccured()) }) }) Context(&quot;When dependencyB fails&quot;, func() { &#x2F;&#x2F;etc... }) }) </code></pre> Compare this to the XUnit style:<p><pre><code> func TestDoingSomethingAwesomeWhenAllIsWell() { &#x2F;&#x2F;setup &#x2F;&#x2F;some sort of assertion } func TestDoingSomethingAwesomeWhenDependencyAFails() { &#x2F;&#x2F;setup + tweak &#x2F;&#x2F;some sort of assertion that awesome is zero &#x2F;&#x2F;some sort of assertion about err } func TestDoingSomethingAwesomeWhenDependencyBFails() { &#x2F;&#x2F;setup + tweak &#x2F;&#x2F;some sort of assertion that awesome is zero &#x2F;&#x2F;some sort of assertion about err } </code></pre> I prefer the former (note: that&#x27;s a subjective statement). IMO there&#x27;s nothing fundamentally more Golangish about the latter (if anything the fact that func&#x27;s that begin with Test are special is kinda weird), and since Gomega&#x27;s matchers are fluent in Go they know about things like zero values and errors having occured. Moreover, both Ginkgo and Gomega have been built to make testing asynchronous code (read: concurrent goroutines) easy and expressive. This isn&#x27;t a carbon copy of RSpec&#x2F;Cedar&#x2F;Jasmine, it&#x27;s a synthesis of the best ideas from those testing frameworks expressed in ways that cater specifically to Golang.<p>- Concerns about using interface{} and reflection seem odd to me. First off, the Go authors provide both of these things to precisely solve the sort of problem that Gomega is trying to solve. Nobody wants to write and maintain matchers that look like:<p><pre><code> Expect(foo).To(EqualInt64(3)) Expect(bar).To(EqualString(&quot;blarp&quot;)) Expect(baz).To(EqualMyCustomAwesomeType(awesome)) </code></pre> besides the reflect package&#x27;s `DeepEqual` does a <i>great</i> job comparing `interface{}` to `interface{}` correctly.<p>Most importantly of all: we&#x27;re all using interface{} <i>all</i> the time: `fmt.Sprintf(&quot;%d %f %s %v&quot;, ...)`!<p>And thanks for seeing the point @karma_fountain: Gomega&#x27;s matchers have excellent reporting about what precisely went wrong when a matcher fails. Bare assertions lack this and a look through the Go tests shows a lot of reinventing-the-wheel to provide what probably amounts to inconsistent error output to the developer. Why not put that all in one place, call it a matcher library, and make it <i>dead easy</i> to write custom matchers?<p>- And @shoo: yes, thanks for pointing it out: the example is somewhat fabricated and isn&#x27;t a compelling argument for BDD vs XUnit. It&#x27;s hard to cook up a good BDD example in very short space!
评论 #6541926 未加载