views:

131

answers:

2

I want to write some unittests for an application that uses MySQL. However, I do not want to connect to a real mysql database, but rather to a temporary one that doesn't require any SQL server at all.

Any library (I could not find anything on google)? Any design pattern? Note that DIP doesn't work since I will still have to test the injected class.

+4  A: 

There isn't a good way to do that. You want to run your queries against a real MySQL server, otherwise you don't know if they will work or not.

However, that doesn't mean you have to run them against a production server. We have scripts that create a Unit Test database, and then tear it down once the unit tests have run. That way we don't have to maintain a static test database, but we still get to test against the real server.

Christopher
+1  A: 

I've used python-mock and mox for such purposes (extremely lightweight tests that absolutely cannot require ANY SQL server), but for more extensive/in-depth testing, starting and populating a local instance of MySQL isn't too bad either.

Unfortunately SQLite's SQL dialect and MySQL's differ enough that trying to use the former for tests is somewhat impractical, unless you're using some ORM (Django, SQLObject, SQLAlchemy, ...) that can hide the dialect differences.

Alex Martelli