数据访问集成测试......你是怎么做到的?

时间:2008-11-21 08:10:59

标签: .net testing tdd continuous-integration integration

public class RollBack : OnMethodBoundaryAspect // or another AOP for meth interception
{
    public override void OnEntry(MethodExecutionEventArgs eventArgs)
    {
        try
        {
            ServiceConfig cfg = new ServiceConfig();
            cfg.Transaction = TransactionOption.RequiresNew;
            cfg.TrackingAppName = "Application Unit Tests";
            cfg.TransactionDescription = "Application Unit Tests Transaction";
            cfg.TransactionTimeout = 0x2710;
            ServiceDomain.Enter(cfg);
        }
        catch (Exception exception)
        {
            Console.WriteLine("Could not enter into a new transaction:\n" + exception);
        }
    }

    public override void OnExit(MethodExecutionEventArgs eventArgs)
    {
        try
        {
            if (ContextUtil.IsInTransaction)
            {
                ContextUtil.SetAbort();
            }
            ServiceDomain.Leave();
        }
        catch (Exception exception)
        {
            Console.WriteLine("Could not leave an existing transaction:\n" + exception);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我可以看到你在交易中进行战术测试,以便在测试后回滚。

我个人从头开始创建测试表并在之后删除它们。另一种常见技术是将数据库恢复到已知状态 - 尽管我建议这表明如果它们依赖于数据库中的大量状态,则测试范围太宽。

我为PHP写了这篇文章,但我在.NET中经常工作。

http://www.stevefenton.co.uk/Content/Blog/Date/201110/Blog/Database-Integration-Testing-With-Enhance-PHP/