Recently I tried to use unit tests in my code, and I like the idea in principle. However, the parts of my code that I most want to experience are areas that are prone to errors, which some tests do not handle very well; eg:
- Network code
- File system interoperability
- Database Interaction
- Communication with equipment (for example, specialized devices that talk via RS-232)
- Calls to fancy third-party libraries
I understand that mock objects are usually used in these situations, but I am looking for a way to make sure that the mock objects properly mimics the situations I want to test.
For example, suppose I want to write a layout that mimics what happens when the database server restarts. To do this, I would like to first verify that the database library I am using will actually throw a specific exception if the database server is restarted. Right now, I'm writing code like:
def checkDatabaseDropout():
connectToDatabase()
raw_input("Shut down the database and press Enter")
try:
testQuery()
assert False, "Database should have thrown an exception"
except DatabaseError, ex:
pass
It requires a fair amount of manual intervention, but it at least gives me a proven set of assumptions that I can work with in my code, and allows me to check these assumptions when I update the library, switch to another base database, and so on. .d.
My question is: are there any better ways to handle this? Is there a framework supporting such semi-automated testing? Or do people usually use other methods at this end of the testing spectrum?