是否有与 Rails 固定装置等效的 .net 版本?
我正在寻找一个项目/工具,可以在测试之前将数据插入数据库并在测试运行后将其回滚。
我知道 ruby on Rails 有 yaml 固定装置,所以我希望有一个针对 .net 项目的项目。
I'm looking for a project/tool that will insert data into a database before a test and roll it back after a test has run.
I know that ruby on rails has yaml fixtures, so I was hoping there is a project out there for .net projects.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几种好方法可以为 .NET 中的测试提供数据。 一种是使用 NUnit 内置的功能,例如参数化测试和理论。
参数化测试:
TestCaseAttribute 允许您轻松提供硬性测试将数据编码到测试,如来自 nunit.org 的以下示例所示:
TestCaseDataAttribute 让您可以更轻松地提供数据(例如从数据库返回数据)。
用于回滚的事务
另一个经常使用的技巧是依赖事务。 基本上,在测试之前启动一个事务,然后在测试之后回滚。 这甚至可以使用基类自动化,因此您的测试根本不处理事务本身。 例如,您的测试装置可能有一个基类,如下所示:
由于基类上的 TestInitialize 修饰方法在派生类中的 TestInitialize 方法之前调用,因此您甚至可以向父类的 TestInitialize 方法中的数据库。
父类可能如下所示:
There are a couple good ways to provide data to tests in .NET. One is to use features built into NUnit such as parameterized tests and theories.
Parameterized Tests:
The TestCaseAttribute allows you to easily supply hard-coded data to a test as in the following example from nunit.org:
The TestCaseDataAttribute lets you get much more fancy in supplying data (such as returning data from a database).
Transactions for Rollback
Another trick often used is relying on transactions. Basically, start a Transaction before the test, and roll it back after. This can even be automated using a base class so you're tests don't deal with the transactions themselves at all. For example, you might have a base class for your test fixture like the following:
Since the TestInitialize decorated methods on the base class are called before TestInitialize methods in derived class, you can even add some data to the database in your parent class' TestInitialize method.
A parent class might look like:
我使用 Sql Server Compact Edition 并每次重新生成数据库的新副本(只需复制初始数据库即可),如果在只读模式下使用,多个测试可以共享相同的数据库文件。
有一些问题,并且不支持可编程性,但它非常适合我需要的基础知识。 它的速度也快得惊人。
I use Sql Server Compact Edition and regenerate a new copy of the database each time (simply copying an initial database is fine) and if used in read only mode multiple tests can share the same database file.
There are a few gotchas, and programmability is not supported but it works well for the basics that I needed it for. It is also surprisingly fast.