0

我运行它时,其中一个单元测试似乎随机通过或失败。对我来说,唯一有意义的是,如果数据库中的数据在每次运行测试时都进入不同的状态,但我使用事务来回滚每个测试中的数据库 - 除非它没有正常工作。这是我的基本单元测试类和有问题的单元测试类。你能看到我可能会失踪的任何东西,或者我应该寻找什么?单元测试通过或失败结果不一致

这发生在TestDriven.Net Visual Studio单元测试框架中。

Partial Public MustInherit Class TestBase 

    Private _scope As Transactions.TransactionScope 

    <DebuggerStepThrough()> _ 
    <TestInitialize()> _ 
    Public Sub Setup() 

      //'Start the Distribution Transaction Coordinator, if it's not already running. 
      Using dtcService As New System.ServiceProcess.ServiceController("Distributed Transaction Coordinator", My.Computer.Name) 
        If dtcService.Status = ServiceProcess.ServiceControllerStatus.Stopped Then 
          dtcService.Start() 
        End If 
      End Using 

      _scope = New TransactionScope(TransactionScopeOption.RequiresNew, New TimeSpan(0)) 
    End Sub 

    <DebuggerStepThrough()> _ 
    <TestCleanup()> 
    Public Sub Teardown() 
     If _scope IsNot Nothing Then 
      _scope.Dispose() 
     End If 
    End Sub 

    <System.Diagnostics.DebuggerStepThrough()> _ 
    Public Shared Function ExecSql(ByVal sql As String) As System.Data.DataTable 

     Dim connStr = GlobalSettings.GetConnectionString() 
     Using conn As New System.Data.SqlClient.SqlConnection(connStr) 
      conn.Open() 

      Dim cmd As New System.Data.SqlClient.SqlCommand(sql.ToString, conn) 
      Dim adapter As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter(cmd) 
      Dim dt As System.Data.DataTable = New System.Data.DataTable 
      adapter.Fill(dt) 

      Return dt 

      If conn.State <> System.Data.ConnectionState.Closed Then 
       conn.Close() 
      End If 
      conn.Dispose() 
     End Using 
    End Function 
End Class 

这是我的单元测试:

Partial Public Class CampaignEmailSendLimitServiceTests 
    Inherits TestBase 

    Private _service = ServiceManager.Current.MyService 

    <TestMethod()> _ 
    Public Sub MyTest() 
     //' Set the pre-test condition. 
     ExecSql("update base.dbo.tblTableA set someDate = '2010-06-28' where id = 56937 ") 

     //' Run the service 
     _service.Process() 

     //' Verify the expected result 
     Dim dt = ExecSql("select deliveryDate from tblTableB ") 
     Assert.AreEqual(False, dt.Rows(0).IsNull("deliveryDate")) 

    End Sub 
End Class 
+1

正在运行一整套测试或者只是这个测试一遍又一遍吗?您可能想要反复运行此测试以查看它是否仍然不一致。如果它在独立运行时工作正常,那么您可能会有另一个测试不适当地清理数据库状态。 – 2011-05-16 18:10:35

+0

不,我一次又一次地运行这个测试。 – adam0101 2011-05-16 18:19:29

回答

0

我终于搞清楚发生了什么事。它与交易无关。这一切都很好。这是我的过程造成了不一致的行为 - 按设计。如果没有找到其他排名,则有一部分流程有“随机排名”来确定deliveryDate。单元测试需要重写,以更好地反映业务规则。

1

这一贯致力于对我罚款。我看到的主要区别是使用TransactionScopeOption.Required。

[TestClass()] 
public class MyTest: DatabaseTestClass 
{ 

    public MyTest() 
    { 
     InitializeComponent(); 
    } 

    TransactionScope ambientTransaction; 

    [TestInitialize()] 
    public void TestInitialize() 
    { 
     ambientTransaction = new TransactionScope(TransactionScopeOption.Required); 
     base.InitializeTest(); 
    } 

    [TestCleanup()] 
    public void TestCleanup() 
    { 
     ambientTransaction.Dispose(); 
     base.CleanupTest(); 
    } 
} 
+0

不幸的是,如果我将它更改为Required,它仍然会发生。 – adam0101 2011-05-17 13:32:23

0

为什么使用DataAdapter.Fill执行更新?它旨在用选择语句填充DataTable。

我的猜测是,你没有写任何东西到数据库首先使用ExecSql。

而第二件事。该断言尽可能无法阅读。 我会改变

Assert.AreEqual(False, dt.Rows(0).IsNull("deliveryDate"));

Assert.IsFalse(dt.Rows(0).IsNull("deliveryDate"));

Assert.That(dt.Rows(0)("deliveryDate"), Is.Not.Null));

不可读测试的原因,有人说单元测试是不好的,因为它会降低你的一个。你必须使单元测试尽可能容易阅读和理解。所以他们没有针对单元测试的争论;)

由于我不使用VB.NET,可能会有一些错别字。断言示例来自NUnit。

+0

感谢格式化提示,但这真的不能回答我的问题。 – adam0101 2011-05-17 12:59:30

+0

格式化评论是一项奖励。我说了我认为你做错了。但你不听。 DataAdapter.Fill不适用于将数据发送到数据库。如果你没有显示服务中有什么,你如何期待好的答案。过程?我认为downvote在这里很粗鲁。 – 2011-05-17 17:07:55