5 ms·
Wow… this can’t possibly be how you write tests in Golang?! Code: https://hastebin.com/share/puciyelayi.go https://hastebin.com/share/puciyelayi.go This is ju
by DarkNova6 3y ago
Wow… this can’t possibly be how you write tests in Golang?!
Code: https://hastebin.com/share/puciyelayi.go https://hastebin.com/share/puciyelayi.go
This is just insane, the ceremony is twice the length of the actual testing code. I seriously hope this is not representative of the language as a whole.
In Java I get the outcome with the Annotation @ParameterizedTest.
- zb 3y ago> this can’t possibly be how you write tests in Golang?! Only for very simple functions like in the example. Most of the time it’s much worse.
- kitd 3y agoThe "ceremony" is just the for loop and the t.Run( ... ) call. Everything else you'd need to do in Java too.
- rswskg 3y agoI was getting the same feeling.
- mrkeen 3y agoCan you write up the Java version too? I don't think it would be significantly more terse. Maybe you'd lose the: for name, tc := range tests { t.Run(name, func(t *testing.T) { But gain: @ParameterizedTest
- nowimaman 3y agoSpock is the most readable framework for testing java IMO, their data driven testing is great, but it might not be a fair comparison because it's written with groovy: def 'split #input with sep #sep should return #expected'() { expect: Split(input, sep) == expected where: testCase | input | sep || expected "simple" | "a/b/c" | "/" || ['a', 'b', 'c'] "wrong sep" | "a/b/c" | "," || ['a/b/c'] "no sep" | "abc" | "/" || ['abc'] "trailing sep"| "a/b/c/"| "/" || ['a', 'b', 'c'] }
- DarkNova6 3y agoPlaceholder Will do once I’m back from work, ~ 3h
- DarkNova6 3y agoAlright, it's not quite as concise as I remember because you can't use arrays in a CSVSource. But I still argue that it is a vast improvement. You have your data + test condition and almost nothing on top. Code: https://pastebin.com/14hLe6Fz https://pastebin.com/14hLe6Fz And now all of the horribleness of requiring a struct definition + defining the data inside your method + for loop is gone. The intend is clear and based upon your liking you can move the test data into a separate class, at the end of the file or next to the test case. PS: I just realized you can't edit your post after 1+ hours have passed!