自动将单元测试包装在数据库事务中?
[编辑(Haren):重复]
我'我正在寻找一种方法来自动将我的 NUnit 集成测试包装在数据库事务中,以便在测试结束时自动回滚测试所做的更改。 理想情况下,我会使用自定义属性来装饰某些测试方法,这将导致 NUnit 在测试开始时创建一个事务,并在测试结束时回滚它......我可以编写该属性,但我不知道在哪里添加一张支票。
测试数据库的大小为 3GB,因此无法在每次测试开始时恢复它。 我知道我可以在每个测试的主体中手动创建一个事务,但我正在寻找更优雅的东西。
也许有一个 IL 重写工具(也许是一个 AOP 框架)可以为我做到这一点? 有人有什么建议吗?
[Edit (Haren): Duplicate]
I'm looking for a way to automatically wrap my NUnit integration tests in a DB transaction, so that changes made by the test are automatically rolled back when the test ends. Ideally, I would decorate certain test methods with a custom attribute that would cause NUnit to create a transaction when the test starts and roll it back when the test ends... I can write the attribute, but I don't know where to add a check for it.
The test DB is 3GB in size, so restoring it at the start of each test isn't an option. I know I can manually create a transaction in the body of each test, but I'm looking for something more elegant.
Perhaps there's an IL-rewriting tool (an AOP framework, maybe) that can do this for me? Anyone have any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不会在这里遇到嵌套事务的麻烦吗? 据我了解,大多数数据库不支持事务中的事务。 因此,如果被测试的代码根本使用事务(我认为如果您使用支持事务的数据库,您就会使用它们),那么您真正想要的是嵌套事务,(我被告知)这是最重要的DB 不直接支持。
Aren't you going to run into trouble with nested transactions here? As I understand it most DB's don't support transactions within transactions. So if the code under test uses transactions at all (I'd think if you are using a DB that supports transactions, you'd be using them), then what you really want is nested transactions, which (I'm told) most DBs don't directly support.
有两个建议。 第一种是使用 SetUp 和 TearDown 属性来启动事务并在完成后回滚。 在 NUnit 中执行此操作的文档位于:
http:// www.nunit.org/index.php?p=setup&r=2.4.8
如果您不想使用它,我看到的另一个选项是在每个方法中对其进行编码,但使用 TransactionScope using 语句中的实例来创建事务(提供程序应自动登记)。
当然,您也可以在SetUp和TearDown方法中创建TransactionScope实例。
There are two recommendations. The first is to use the SetUp and TearDown attributes to start the transaction and roll it back when done. The documentation to do so in NUnit is here:
http://www.nunit.org/index.php?p=setup&r=2.4.8
If you don't want to use that, the other option I see is to code it in every method, but use a TransactionScope instance in a using statement to create the transaction (the provider should auto-enlist).
Of course, you can create the TransactionScope instance in the SetUp and TearDown methods as well.
春网?
我有 Java/JUnit 背景,但我知道在 Java 中你可以使用 Springframework 来做到这一点。
(您还必须从 ApplicationContext/BeanFactory 获取测试才能对其应用 AOP)。
Spring.net?
I come from a Java/JUnit background, but I know that in Java you could do this using Springframework.
(You also will have to obtain your test from the ApplicationContext/BeanFactory in order to apply AOP to it).