3 ms·
I disagree - I always test a real database, never mock it. This is because in my embedded domain we often ship with an empty database, so it is no big deal to a
by bluGill 6y ago
I disagree - I always test a real database, never mock it. This is because in my embedded domain we often ship with an empty database, so it is no big deal to add just the tables I need with whatever data I want, and then test it. Sqlite makes this easy for us.
Likewise filesystems on modern operating systems are fast (there is a cache): I cannot measure the time difference I save mocking the filesystem calls out, so it is easier and safer to just work with files I create in the tests. It might some effort to ensure my tests put files in places where they won't collide with other tests, but this isn't hard once you have the idea in place. Since our plan was put into the code early on most people don't even consider bypassing it for some hard coded locatoin and so it is easy to use the plan on test files located in a different location.
Note that the above is specific to my domain. I understand that some of you deal with one database so there is no abstraction that lets you substitute a test database you create on the fly, something not worth fixing since you still cannot afford a license for every developer and CI machine. I'm sure there is a similar argument against using a file system.
The point: before saying don't use something expensive in tests do a benchmark: prove it actually is expensive. You may be lucky like me and discover that databases and filesystem access in tests are not expensive!
- dpc_pw 6y agoThe reason to mock out IO is not that much about the performance, but isolation. It's annoying to have to deal with setting up database instances, users, permissions and what not, just to run an otherwise standalone test. Also annoying when two test suites running in parallel conflict (though it can be avoided). If you use sqlite, it's much easier since it's all just working on standalone file. > I disagree - I always test a real database, never mock it. I always put an interface between the IO part and rest of the code, so I can either mock it or not. Ideally I would have a separate tests making sure that the real-IO implementation of the interface works, and not use in my normal test that are exercising logic. But it is indeed a lot of additional work, so I'm cutting the corners and let the test run against a real db implementation, only wrapping it in things like injecting faults etc.