Suggestions to avoid DB deps when using an in-memory sqlite DB to speed up unit tests

喜你入骨 提交于 2019-12-11 02:32:29

问题


I've been using sqlite::memory: for unit tests: it is quick, and makes test clean-up automatic. But I'm concerned my tests could be missing bugs due to SQL server assumptions. (This particular code is supposed to be DB-neutral, but pragmatically it must work with MySQL.)

As a specific example, I think date string inputs to sqlite get stored as strings (*), so they get given back to me in exactly the same format. Whereas in MySQL they appear to get parsed, normalized, and returned in a consistent format. My bigger concern is that there are differences I'm not so conscious of.

What is the best way to handle this? E.g. repeat all unit tests for mysql too (slow - is there a way to make an in-memory MySQL DB?). Just do mysql tests for some key areas of concern? Rely on functional tests that use MySQL (this is my current approach, but the coverage is much spottier). Something else I've not thought of?

*: If I give "2012-12-25 09:12:34" I get that exact string back. If I give "2012-12-25 09:12:34 UTC" I get back that exact string (i.e. with the timezone this time). If I give "2012-12-25 09:12:34 hello world" I get back "2012-12-25 09:12:34 hello world" :-(


In this particular case I'm using PHP, PDO and PHPUnit. I'm after language-independent strategies, but it is worth noting that PDO is a quite thin abstraction, more to do with abstracting the connection string and low-level API calls. It doesn't try to abstract data type differences between the databases.


回答1:


If you want to test MySQL-specific SQL details, you just have to test on MySQL.

MySQL has the MEMORY storage engine, but this is not fully compatible with other engines. You should put the database on a RAM disk (tmpfs).

SQLite does not have dedicated data types for dates/times.




回答2:


What we typically do where I work is, split the tests in:

  • Unit tests: very fast, using an in-memory database (H2 in my case).

  • Integration test: slower, use the 'real' database, plus additional (longer running) tests

During development, we just run the unit tests. The automated build runs the integration tests as well (but you can run them yourself if you want to).



来源:https://stackoverflow.com/questions/14423613/suggestions-to-avoid-db-deps-when-using-an-in-memory-sqlite-db-to-speed-up-unit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!