I have recently developed a small module that performs queries against a database. At the end of each test that contains code performing modifying queries against the database, I added an assert that checks that a record irrelevant to the query did not get affected. My reasoning is that I want to prevent a future developer from, for example, omitting the where clause in an update query and causing catastrophic side effects which won't be caught in tests. I created an "irrelevant record" in my database setup and a function that asserts the record hasn't changed. I call this function at the end of each test.
I have several things I am not comfortable with in this solution:
- Each test case doesn't only test its described test case, but also the unwanted side effect case. The alternative is duplicating each test twice which I think will pollute the test suite and make it hard to maintain and add new tests. I feel this is wrong as a methodology, as for example if in the future I'll want to do
nwide assertions on each test case, this will entail makingncopies of each test, which sounds horrible. - I have the feeling that testing this is overkill, but on the other hand without such an assertion there is not guarantee of unwanted side effects not happening.
My question is how to approach this kind of problem, specifically checking for unwanted side effects in a module that has database access involved, but more generally when wanting to test a common assertion for all test cases.
Thanks.