利用COM+对数据库操作进行单元测试

博客介绍了程序编译时添加强名称的相关内容。若编译程序,需添加强名称,可在命令行使用 sn -k test.snk ,再将 test.snk 拷贝到程序目录,设置 [assembly: AssemblyKeyFile(@..\..\..\test.snk)] 。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当单元测试需要对数据库执行CRUD(Create,Retrieve,Update,Delete)操作时,测试过后会在我们的数据库中留下大量重复的垃圾数据,这些垃圾很碍眼不是吗?而且我们的下一个测试有可能因为这些垃圾产生一些错误。

那么我们要如何处理这些垃圾数据和保证测试的稳定的呢?显然,我们需要在每次测试之前和测试完成之后让数据库都保持相同的状态。换句话说,就是我们需要"undo"这些在测试中对数据库执行的CRUD操作。

对于我们需要的这种"undo"操作,你以前是怎么做的呢?手动的移除还是使用ADO.NET的事物处理呢?这些方法都可行,但对我们来说还不够好。因为它们都需要我们编写更多的代码,影响我们的开发效率。

现在,就要开始说本文的重点了,利用COM+的自动事务处理功能来帮助我们实现我们的"undo"。

首先,写一个基类,此类能够在每一个方法完成后自动回滚对数据库的操作:
None.gif using  System;
None.gif
using  NUnit.Framework;
None.gif
using  System.EnterpriseServices;
None.gif
None.gif
namespace  TransactionTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [TestFixture]
InBlock.gif    [Transaction(TransactionOption.Required)]
InBlock.gif    
public class DatabaseFixture:ServicedComponent
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        [TearDown]
InBlock.gif        
public void TransactionTearDown()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(ContextUtil.IsInTransaction)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ContextUtil.SetAbort();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

下面再写一个使用此基类的示例程序:
None.gif using  System;
None.gif
using  NUnit.Framework;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
None.gif
namespace  TransactionTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class CategoryTests:DatabaseFixture
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string CONN = @"Server=192.168.0.58\sun;Database=Northwind;uid=帐号;pwd=密码";
InBlock.gif
InBlock.gif        [Test]
InBlock.gif        
public void InsertTest()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string categoryName = Insert("Test category");
InBlock.gif            VerifyRowExists(categoryName, 
true);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Test]
InBlock.gif        
public void DeleteTest()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string categoryName = Insert("Test category");
InBlock.gif            Delete(categoryName);
InBlock.gif            VerifyRowExists(categoryName, 
false);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 新增一条记录
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="categoryName"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        private string Insert(string categoryName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
using (SqlConnection conn = new SqlConnection(CONN))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string strSQL = "Insert Categories (CategoryName) values('" + categoryName + "')";
InBlock.gif                SqlCommand cmd 
= new SqlCommand(strSQL, conn);
InBlock.gif                conn.Open();
InBlock.gif                cmd.ExecuteNonQuery();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return categoryName;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 删除一条记录
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="categoryName"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        private string Delete(string categoryName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
using (SqlConnection conn = new SqlConnection(CONN))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string strSQL = "Delete from Categories Where CategoryName='" + categoryName + "'";
InBlock.gif                SqlCommand cmd 
= new SqlCommand(strSQL, conn);
InBlock.gif                conn.Open();
InBlock.gif                cmd.ExecuteNonQuery();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return categoryName;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 对执行的数据库操作进行验证
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="categoryName"></param>
ExpandedSubBlockEnd.gif        
/// <param name="shouldExist"></param>

InBlock.gif        private void VerifyRowExists(string categoryName,bool shouldExist)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlConnection conn 
= new SqlConnection(CONN);
InBlock.gif            conn.Open();
InBlock.gif
InBlock.gif            
string strSQL = "Select * from Categories where CategoryName='" + categoryName + "'";
InBlock.gif            SqlCommand cmd 
= new SqlCommand(strSQL, conn);
InBlock.gif            SqlDataReader dr 
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
InBlock.gif            Assert.AreEqual(shouldExist,dr.HasRows);
InBlock.gif            dr.Close();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


如果编译上面的程序,还需要给程序加一个强名称,否则是无法通过的

 

在命令行使用 sn -k test.snk
然后把 test.snk 拷贝到程序目录中,再设置
[assembly: AssemblyKeyFile(@"..\..\..\test.snk")]





原文: http://weblogs.asp.net/rosherove/articles/dbunittesting.aspx

转载于:https://www.cnblogs.com/netflu/archive/2005/07/29/202805.html

1. 测试环境的搭建(DBunit+HSQLDB) 1 1.1. DBunit的简介 1 1.1.1. DBunit简单介绍和原理 1 1.1.2. DBunit的三大核心组件 1 1.1.3. DBunit的安装使用 2 1.2. HSQLDB简介 3 1.2.2. 什么是HSQLDB 3 1.2.3. HSQLDB安装和使用 5 1.2.4. HSQLDB使用 7 1.2.5. HSLDB的使用注意事项 8 2. 数据库单元测试测试流程介绍 1 2.1. 数据库单元测试的原因 1 2.2. 测试关注点 1 2.3. 测试流程 1 3. 数据库单元测试最佳实践 2 3.1. 数据库单元测试最佳实践 2 3.1.1. 从“易测试的”应用程序体系结构开始。 2 3.1.2. 使用精确的断言。 2 3.1.3. 外化断言数据。 3 3.1.4. 编写全面的测试。 4 3.1.5. 创建稳定、有意义的测试数据集。 5 3.1.6. 创建专用的测试库。 6 3.1.7. 有效地隔离测试。 7 3.1.8. 分割测试套件。 8 3.1.9. 使用适当的框架(如 DbUnit)简化过程。 9 3.2. DBunit使用最佳实践 9 3.2.1. 每一个开发人员需要搞一个数据库实例。 9 3.2.2. 使用XML文件作为DataSet 9 3.2.3. DBUnit的最佳实践是尽可能使用最小的数据集。 10 3.2.4. DatabaseOperation.CLEAN_INSERT 策略 10 3.2.5. 为相互关联的测试场景创建多个种子文件是一个很有效的策略. 10 3.3. 自我总结 10 3.3.1. 对各种类型的方法的测试策略: 10 3.3.2. 写一个基TestCase。 10 3.3.3. 测试数据库的有效方法。 10 3.3.4. seed文件的设置 10 3.3.5. 一般的测试的步骤 11 4. 常见问题 1 4.1. DBUnit使用问题 1 4.1.1. 在运行测试用例的时候,出现org.dbunit.dataset.DataSetException: java.net.MalformedURLException at……………类似的异常? 1 4.1.2. 在运行测试用例的时候,出现SQLException,并且对应的sqlstate:23504,这是为什么? 1 4.2. HSQLDB相关的问题 1 4.2.1. 抛出:java.sql.SQLException: socket creation error 1 4.3. 其他问题 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值