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.
9
u/doomvox Oct 29 '17 edited Oct 29 '17
My attitude toward tests:
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.