On Tuesday, February 5, 2013 7:01:58 PM UTC-7, Andrew Gerrand wrote: On 6 February 2013 12:18, Ewan Chou <coo...@gmail.com <javascript:>>wrote:
- Assert library always logs expected value, actual value and line number.
I think Kyle was being sarcastic. That is not a helpful error message.
What message would be more helpful? I personally find that information to
be the most useful, but I'm always open to better and smarter things. When
a unit test fails, especially one that I didn't write, I want to know where
it happened, what the expected value was and what actual value was.That is
often enough information to look at my test cases and see if there is a
mistake there or if something is wrong with the tested function.
I can see some value in a library that handles certain common test patterns
as well. I use one I wrote myself. I've found that 95% of my test cases
look like this:
foo, err := doSomeInit()
if err != nil {
t.Fatalf("doSomeInit failed: %v", err)
}
for i, test := range tests {
bar := myCoolFunc(foo, test.input)
if bar.Code != test.Code {
t.Errorf("(Iteration %d) expected %d from bar.Code but got %d", i,
bar.Code, test.Code)
}
if bar.Body.String() != test.Body {
t.Errorf("(Iteration %d) expected %s from bar.Body but got %s", i,
bar.Body, test.Body)
}
}
I'm just simplifying those common cases like:
foo, err := doSomeInit()
h.FatalNotNil("doSomeInit", err)
for i, test := range tests {
bar := myCoolFunc(foo, test.input)
h.ErrorNotEqual("bar.Code", bar.Code, test.Code)
h.ErrorNotEqual("bar.Body", bar.Body.String(), test.Body)
}
That looks much more easy to read to me and I get the exact same
information out of it. If a test fails, both produce something like:
test.go:85 (Iteration 3) expected "foo" from bar.Body but got "bar"
The testing.T is still around if I need it to log something more
substantial, but based on my experience that's a minority of the time.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit
https://groups.google.com/groups/opt_out.