简体   繁体   English

如何隔离集成测试?

[英]How to isolate the integration Tests?

When having a database operations in many integration Tests, how to be sure that the state of the DataBase is the same before and after any Test in an automatic manner (with some framework) ? 在许多集成测试中都有数据库操作时,如何确保自动(使用某些框架)进行任何测试之前和之后的数据库状态相同?

I am not interested in the manual manner ( @Before @After ) 我对手动方式不感兴趣( @Before @After

What we do at my company is that 我们在我公司所做的是

  1. We use transactions to ensure that the DB is in the same state after the test as beforehand. 在测试之后,我们使用事务来确保数据库处于与先前相同的状态。
  2. We use test scripts that ensure valid test data (eg, inserting some additional rows to some tables for specific test scenarios, whereas updating some others). 我们使用确保有效测试数据的测试脚本(例如,针对特定的测试方案在某些表中插入一些其他行,而对其他表进行更新)。 You can execute these scriptes in setUp methods to reuse them by multiple test cases or even define them in utility classes that can be reused by multple test suites. 您可以在setUp方法中执行这些脚本,以在多个测试用例中重用它们,甚至可以在可被多个测试套件重用的实用工具类中定义它们。

This works finy in many scenarios, however, it may be problematic if you try to test parts of your application that are working with nested transactions. 这在许多情况下都能正常工作,但是,如果您尝试测试应用程序中处理嵌套事务的部分,则可能会出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM