4 ms·
> If you're working with something more static, like C#, Java, Go, Rust, etc, then it usually doesn't make sense to write tests like that. I wouldn't trust mys
by rectang 6y ago
> If you're working with something more static, like C#, Java, Go, Rust, etc, then it usually doesn't make sense to write tests like that.
I wouldn't trust myself to write code without unit testing it, nor would I trust you, nor would I trust anybody else. It is all too easy to make mistakes like off-by-one errors or other edge case misbehavior.
- TillE 6y agoI've seen the opinion "static typing obviates the need for unit tests" expressed pretty frequently, and I can merely gape utter bafflement. I like static typing, but it's not magic pixie dust. Start with the fact that unit testing was widely popularized with Java.
- closeparen 6y agoYou have literally no idea whether an untested line in a dynamic language could ever work. A line in Java that compiles but doesn't have test coverage still probably does what it says, and how badly you need that test coverage depends on the complexity of what it says.
- quicklime 6y agoI'm not the person you're replying to, but I don't think that's what they're saying. When I write Python, I'm paranoid that someone will pass a string instead of an int to my function. Or that when I for-loop over my tuple of strings, I'll accidentally iterate over the characters in the (single) string of my (1-)tuple, instead of the many strings in a n-tuple (with n>1). So I write unit tests to cover these specific cases. When I'm writing Java or Go, I write all the other tests, but not those ones. Because the type checker would catch those problems for me.
- ufmace 6y agoI'm not saying to not write any micro-tests at all, just that most of them are not needed - use your judgement of course. Don't bother writing tests for things that your type system and compiler already protect you from. I do find that off-by-one errors are much harder to make, in both static and dynamic languages, when you use the collection looping and manipulation constructs that almost every language has now, instead of C-style triple-statement for loops.