如何高效测试 SQL/JPQL/HQL?
人们常说,单元测试时不要测试数据库,因为这是集成测试
(参见第 4 点)。
但是,SQL/JPQL/HQL 封装了数据存储特定
逻辑,这些逻辑通常采用字符串格式来说明如何访问数据。这种自由形式的字符串数据访问命令很容易出错,因此需要进行测试。
我如何有效地测试这种逻辑?
It is often said when unit testing to dont test the database as that is an integration test
(see point 4).
However, SQL/JPQL/HQL encapsulate data store specific
logic which is often in string format on how to access data. This free form string data access command can easily go wrong and hence needs to be tested.
How do i efficiently test this sort of logic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最接近针对 SQL(或类似框架)查询运行单元测试的方法是在内存中设置 SQLite 数据库,然后对其运行。
虽然这在技术上仍然是一个集成测试,但它的运行速度几乎与单元测试应有的速度一样快。
如果您这样做,请注意 SQLite 和您的真实数据库之间的轻微差异,并尝试使您的查询与两者兼容。
希望这有帮助,
阿萨夫。
The closest you can get to running a unit test against an SQL (or similar framework) query, is to set up a SQLite database in memory, and run against it.
While that still is technically an integration test, it runs almost as fast as a unit test should.
If you do so, just take care to note the slight differences between SQLite and your real database, and try to make your queries compatible with both.
Hope this helps,
Assaf.
它不是单元测试,但是使用像 Nunit 这样的单元测试框架来测试你的 sql 并没有什么意义。但重要的是要将其与真正的单元测试分开。真正的单元测试速度很快,并且不与外部通信……也不尝试通过更新、删除和插入来更改它。
It is not a unit test, but there is nothing with using a unit testing framework like Nunit to test your sql. But it IS important that you keep it separated from the real unit tests. Real unit tests are fast and does not communicate with the outside ... nor do they attempt to alter it by updates, deletes and inserts.