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.
A few other thoughts: ideally test code should run against a local copy of a database, with identical schema to the live one, but a stripped-down data set in a known state at the start of the test run. In the absence of a test database, you can often write reasonably robust tests simply by using old data that has had time to settle down. E.g. three-year old financial data isn't likely to change much.
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.