数据库集成测试
当您仅使用数据访问层或大部分应用程序堆栈进行集成测试时。 如果在同一个数据库上运行多个测试,防止它们相互冲突的最佳方法是什么?
When you are doing integration tests with either just your data access layer or the majority of the application stack. What is the best way prevent multiple tests from clashing with each other if they are run on the same database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
还要在不同时间运行测试,以便它们不会影响彼此的性能或有效性。
Also run the tests at different times, so that they do not impact the performance or validity of each other.
虽然不像此处其他答案中的 Rails 单元测试框架那么聪明,但为每个测试或测试组创建不同的数据是另一种方法。 此解决方案的乏味程度取决于您拥有的测试用例的数量以及它们彼此之间的依赖程度。 如果每个测试或一组相关测试都有一个数据库,那么乏味的情况就会发生。
运行测试套件时,您在开始时加载数据,运行测试套件,卸载/比较结果,确保实际结果符合预期结果。 如果没有,请再次循环。 加载、运行套件、卸载/比较。
While not as clever as the Rails unit test framework in one of the other answers here, creating distinct data per test or group of tests is another way of doing it. The level of tediousness with this solution depends on the number of test cases you have and how dependant they are on one another. The tediousness will hold true if you have one database per test or group of dependant tests.
When running the test suite, you load the data at the start, run the test suite, unload/compare results making sure the actual result meets the expected result. If not, do the cycle again. Load, run suite, unload/compare.
对于简单的数据库应用程序,我发现使用 SQLite 非常有价值。 它允许您为每个测试拥有一个独特且独立的数据库。
然而,只有当您使用简单的通用 SQL 功能或者可以轻松隐藏 SQLite 和类后面的生产数据库系统之间的细微差异时,它才有效,但我总是发现这在我的 SQL 应用程序中相当容易发达。
For simple database applications I find using SQLite invaluable. It allows you to have a unique and standalone database for each test.
However it does only work if you're using simple generic SQL functionality or can easily hide the slight differences between SQLite and your production database system behind a class, but I've always found that to be fairly easy in the SQL applications I've developed.
只是为了添加到 Free Wildebeest 的答案中,我还使用 HSQLDB 进行类似的类型测试,其中每个测试都会获得一个干净的实例数据库的。
Just to add to Free Wildebeest's answer I have also used HSQLDB to do a similar type testing where each test gets a clean instance of the DB.
我想接受自由牛羚和猎户座爱德华兹的答案,但它不允许我。 我想这样做的原因是我得出的结论是,这是两种主要的方法,但选择哪一种取决于具体情况(主要是数据库的大小)。
I wanted to accept both Free Wildebeest's and Orion Edwards' answers but it would not let me. The reason I wanted to do this is that I'd come to the conclusion that these were the two main ways to do it, but which one to chose depends on the individual case (mostly the size of the database).
交易。
ruby on Rails 单元测试框架的作用是这样的:
这意味着
我认为这很酷
Transactions.
What the ruby on rails unit test framework does is this:
This means that
I for one think this is pretty cool