是否有与 Rails 固定装置等效的 .net 版本?

发布于 2024-07-13 16:51:10 字数 112 浏览 13 评论 0原文

我正在寻找一个项目/工具,可以在测试之前将数据插入数据库并在测试运行后将其回滚。

我知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月如刀 2024-07-20 16:51:10

有几种好方法可以为 .NET 中的测试提供数据。 一种是使用 NUnit 内置的功能,例如参数化测试和理论。

参数化测试:

TestCaseAttribute 允许您轻松提供硬性测试将数据编码到测试,如来自 nunit.org 的以下示例所示:

[TestCase(12,3, Result=4)]
[TestCase(12,2, Result=6)]
[TestCase(12,4, Result=3)]
public int DivideTest(int n, int d)
{
  return( n / d );
}

TestCaseDataAttribute 让您可以更轻松地提供数据(例如从数据库返回数据)。

用于回滚的事务

另一个经常使用的技巧是依赖事务。 基本上,在测试之前启动一个事务,然后在测试之后回滚。 这甚至可以使用基类自动化,因此您的测试根本不处理事务本身。 例如,您的测试装置可能有一个基类,如下所示:

public class TestBase
{
    private TransactionScope _transacation;

    [SetUp]
    public virtual void InitializeTest()
    {
        //NOTE: Base class TestInitialize methods are called before test Initialize methods in derived class.

        // Setup a DB transcation to roll everything back after the tests.
        if (_transacation != null)
            throw new Exception("old transacation still exists");
        // Give a long timeout on this transacation for debugging...
        _transacation = new TransactionScope(TransactionScopeOption.RequiresNew, TimeSpan.FromSeconds(60));   
    }

    [TearDown]
    public virtual void CleanupTest()
    {

        // Roll all the changes made during the test back.
        _transacation.Dispose();
        _transacation = null;
    }
}

由于基类上的 TestInitialize 修饰方法在派生类中的 TestInitialize 方法之前调用,因此您甚至可以向父类的 TestInitialize 方法中的数据库。

父类可能如下所示:

[TestFixture]
public class MyClassTests : TestBase
{
    [TestFixtureSetUp]
    public void InitializeUnit()
    {
            //Setup mocks...
    }

    [SetUp]
    public override void InitializeTest()
    {   
            base.InitializeTest();
            // Add test data to database
    }

    [Test]
    public void RealTest()
    {
            ...
    }
}

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:

[TestCase(12,3, Result=4)]
[TestCase(12,2, Result=6)]
[TestCase(12,4, Result=3)]
public int DivideTest(int n, int d)
{
  return( n / d );
}

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:

public class TestBase
{
    private TransactionScope _transacation;

    [SetUp]
    public virtual void InitializeTest()
    {
        //NOTE: Base class TestInitialize methods are called before test Initialize methods in derived class.

        // Setup a DB transcation to roll everything back after the tests.
        if (_transacation != null)
            throw new Exception("old transacation still exists");
        // Give a long timeout on this transacation for debugging...
        _transacation = new TransactionScope(TransactionScopeOption.RequiresNew, TimeSpan.FromSeconds(60));   
    }

    [TearDown]
    public virtual void CleanupTest()
    {

        // Roll all the changes made during the test back.
        _transacation.Dispose();
        _transacation = null;
    }
}

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:

[TestFixture]
public class MyClassTests : TestBase
{
    [TestFixtureSetUp]
    public void InitializeUnit()
    {
            //Setup mocks...
    }

    [SetUp]
    public override void InitializeTest()
    {   
            base.InitializeTest();
            // Add test data to database
    }

    [Test]
    public void RealTest()
    {
            ...
    }
}
独木成林 2024-07-20 16:51:10

我使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文