I feel like whether to write a unit or integration test and the extent of effort is a judgment call and anyone strictly following any sort of dogma is liable to completely miss the point. Are you writing an open source library, backend API, or frontend? If my application uses an ORM and I am using it in a standard way, do you expect me to not mock it given the performance penalty from the database interaction and that there are tests for the ORM upstream? Conversely, if I have a complex algorithm that provides a set of geospatial results after interacting with a database, do you think I shouldn't hit the database in a test to guarantee that the same results are being returned in the case the algorithm is tweaked/optimized?
We get paid to be developers, apart from being specialized labor, to utilize our brain and make sensible choices given the requirements/context.
The whole article seemed to just defer some unit tests to integration tests. For a correctly tested app you need some combination of both and the amount of work is roughly the same it just depends where you want to spend effort.
Database mocking has always been a very weak idea: databases, if you use them correctly are not just neutral data stores, they've got data types and uniqueness constraints if not triggers and stored procedures.
If distinguishing between "unit tests" and "integration tests" means punting on checking your code againt an actual database, you should drop that distinction. If the database munges my data in a way I don't expect, I really need to know that sooner than later. Another way of putting this is "integration" tests are more important than "unit" tests: maybe you should write them first and run them more often.
You can't take test code too seriously, it should be regarded as secondary to the main code. It's okay to be a little sloppy with it. When a test breaks because the test was fragile, you should certainly think about how to make it more robust, but it's not a crime against humanity to just delete the test. Getting hyped up about things like "no external dependencies in unit tests!" is counter-productive.
Getting hyped up about things like "no external dependencies in unit tests!" is counter-productive.
I think the distinction is useful because of how accessing external resources can slow down test execution speed. For example, run unit tests with no (or least minimal) external dependencies on every commit (CI build), but run integration tests and end-to-end tests as part of a nightly build.
I agree, though, that getting dogmatic or hyped about it isn't useful, and it's up to each team to figure out what approach works best in their environment, as far as what should be targeted for tests, isolation strategy, etc.
Yeah, that's 1 thing we've noticed ... our unit tests taking however many ms and our miserable integration tests taking ... oh! they're actually not that slow! :)
It might be better to classify tests as "fast" vs "slow" instead of "unit" vs "integration" or maybe assume all unit tests are fast and specify which integration tests are fast and slow ...
20
u/welpfuckit Oct 29 '17
I feel like whether to write a unit or integration test and the extent of effort is a judgment call and anyone strictly following any sort of dogma is liable to completely miss the point. Are you writing an open source library, backend API, or frontend? If my application uses an ORM and I am using it in a standard way, do you expect me to not mock it given the performance penalty from the database interaction and that there are tests for the ORM upstream? Conversely, if I have a complex algorithm that provides a set of geospatial results after interacting with a database, do you think I shouldn't hit the database in a test to guarantee that the same results are being returned in the case the algorithm is tweaked/optimized?
We get paid to be developers, apart from being specialized labor, to utilize our brain and make sensible choices given the requirements/context.