I'm interested to know what approach people are taking in developing automated unit tests that exercise the database
Do you Install a QA database (known-starting point) before the test suite is run.
OR
Do you build database stub that stand-in whenever a database call occurs?
EDIT: Related question, but not a duplicate, though quite important for the matter at hand: How do I unit-test persistence?
It is meant to make sure that definable modules of code work as expected. To test an application it is not enough to use unit tests. You must also perform functional testing and regression testing. Database access falls outside the scope of unit testing, so you would not write unit tests that include database access.
Unit tests shouldn't depend on infrastructure For example, your database functions with all their SQL and driver libraries should be separate from the code that ensures your user's passwords are secure. There's no way to test this function without having a database connection available at the time of testing.
Yes, absolutely! Because our code that talks to the real DB is already tested carefully in the previous lecture. So all we need to do is: make sure that the mock DB implements the same interface as the real DB. Then everything will be working just fine when being put together.
The "database stub" that stands in is usually referred to as a "fake repository" or "mock repository". They are a good idea. You can code them by hand (not hard for simple cases) or use a framework like Rhino Mocks to generate them. You don't mention what language you are working in. Rhino mocks is for .Net .
If you use mock repositories then you can run tests against code that works with data, without actually using a database for the data. This makes the tests run very fast, which is a good thing.
Of course, you still have to test the real repository at some stage, and this is more of an issue. These tests will run slower, because they use a real database. Some would classify then as "integration tests" not unit tests because of the speed and dependency issues.
I don't mind what you call them so much, but it's a good idea to keep these tests separate.
A good idea here for data consistency is to begin a db transaction before your test, and roll it back afterwards. That way the database state is reverted to what it was before the test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With