NUnit的回滚后测试 [英] NUnit Rollback After Test

查看:152
本文介绍了NUnit的回滚后测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我(和一般自动化测试)pretty新NUnit的。我最近做了一些Ruby on Rails的工作,并注意到在我的测试套件,当我创建对象(如一个新的用户)和套件的过程中提交他们,他们永远不会提交到数据库,这样我可以运行测试一遍又一遍,而不是担心该用户已经存在。

I am pretty new to NUnit (and automated testing in general). I have recently done some Ruby On Rails work and noticed that in my test suite, when I create objects (such as a new user) and commit them during course of the suite, they are never committed to the database so that I can run the test over and over and not worry about that user already existing.

我现在正在试图完成在NUnit的同样的事情,但我不太知道如何去这样做。难道我创建的建立和拆除块事务?谢谢你。

I am now trying to accomplish the same thing in NUnit, but I am not quite sure how to go about doing it. Do I create a transaction in the Setup and Teardown blocks? Thanks.

推荐答案

为什么你会在单元测试跟我们的数据库?这在默认情况下让你的单元测试整合的测试。相反,创建所有数据库通信的包装,和存根/在单元测试嘲笑它。然后,你不必前后担心数据库状态。

Why would you talk to the database during unit-tests? This makes your unit-test to integration-tests by default. Instead, create wrappers for all database communication, and stub/mock it during unit-tests. Then you don't have to worry about database state before and after.

现在,如果你不愿意的重构这一水平:与交易的问题是,你需要打开的连接。所以,如果你的目标测试方法处理它自己的所有通信,真的很难注入,你可以在安装程序创建和回滚在拆解事务。

Now, if you are not willing to that level of refactoring: The problem with transactions is that you need an open connection. So, if your method targeted for testing handles all communication on its own, it is really difficult to inject a transaction that you can create at setup and roll back at teardown.

这篇关于NUnit的回滚后测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆