Static Typing vs Unit Tests
The cluster debates whether static type systems replace or reduce the need for unit tests, with commenters arguing they complement each other by catching different classes of bugs in typed vs dynamic languages.
Activity Over Time
Top Contributors
Keywords
Sample Comments
What's the point of a static type system if I'm going to write unit tests anyways?
You need tests everywhere, statically typed doesn't mean you don't need to test.
Nobody made the argument that static typing removes the need for unit tests. It does check for many conditions that would have to be unit tested otherwise, such as calling functions with the wrong types.
Static types most definitely do not replace the need for tests!
This is wrong; tests and type systems have different purposes. One helps prevent logical bugs and regressions in your code, while the other enforces the correct use of code and data structures. They overlap a bit, but all the unit tests in the world can’t give you the guarantees that a type system does. And that’s without even mentioning the benefits to discoverability and documentation that a static type system gives you.
You're going to write the tests anyway. Static typing doesn't free you from that responsibility. The question is whether any type errors would make it past a unit test suite focused on other kinds of correctness. In practice they usually don't.
People write tests in statically typed languages too, it's just that there's a whole class of bugs that you don't have to test for.
Tests are not proofs, types provide proofs. Tests demonstrate that the exact conditions being tested actually work. That's not a bad thing but when working with static typing and robust type systems the need for unit testing goes down tremendously compared to dynamic languages.
You could argue static typing can replace testing for some subset of tests when compared to dynamic languages, but for the most part yeah, you can't use any type system I know of (not strongly conversant in Scala or Haskell's) to replace the vast majority of unit/etc tests.
As a general rule of thumb, once you have proper tests, static typing doesn't find very much, if anything.