3 ms·
It's because the Go philosophy is "tests are code", so you use the standard language features like != instead of a whole new domain-specific language that you h
by benhoyt 6y ago
It's because the Go philosophy is "tests are code", so you use the standard language features like != instead of a whole new domain-specific language that you have to learn. I've written more about that here: https://lwn.net/Articles/821358/ https://lwn.net/Articles/821358/
Personally I find the "cutesy" names like Expect(a).To.Equal(b) and It("should ...") patronizing and too English-like (code is not a human language). Similarly, I wouldn't like a language that made you write:
Please.Print(1.Plus.2)
Log.To(Console).Text("did something")
The vanilla Go approach can get a bit verbose when you're doing lots of asserts. In that case, you can use standard language features like refactoring things to variables or helper functions, or table-driven tests.
But if you really want assert-style code, I like the https://labix.org/gocheck https://labix.org/gocheck approach:
c.Assert(a, Equals, b)
Because there's only one function (Assert) and a bunch of comparator names to learn.
- jzoch 6y agoWhile I dont care much for the fluent cutesey langauge as you call it - the error messages and consistency are where it shines. Getting an entire team to write similar error messages in test cases is a fools game - just agree on an assert library. Added bonus is the error messages can be amazing (if the lib is good).
- ThePadawan 6y agoThis approach works well in more dynamic languages with some level of introspection that can derive meaningful assertion failures from that code. Coming from the C# world, the thoughtless use of Assert.* functions leads to the test failure of "Assertion failed: Expected true, got false." in 90% of cases.