单元测试的测试用例编写方法

本文通过一个实际例子详细介绍了如何使用基本路径测试法进行单元测试。包括分析模块函数、绘制程序流程图、计算圈复杂度等步骤,并给出了具体的测试用例。








                 单元测试的测试用例编写方法
        我在这里用最常用的方法:基本路径测试法来进行单元测试,因为我要用一个实际的例子来进行说明,所以就编写了下面一个程序模块,就暂且命名为“详细查询模块”吧。
     我先写一下基本过程:
    1  分析模块函数;
    2  在模块中找到相应的关键点(函数);
    3  根据第二点,画出模块程序流程图;
    4  计算圈复杂度;
    5  根据圈复杂度算出测试用例的最优个数;
    6  根据路径测试法和圈复杂度写出具体测试用例;
    7  进行测试。   
 
InBlock.gifvoid CXIANGXIDLG::OnOK() 
InBlock.gif
InBlock.gifCoInitialize(NULL);//初始化COM环境 
InBlock.gif_ConnectionPtr m_pConnection;//连接对象 
InBlock.gif  HRESULT hr;    
InBlock.gif 
InBlock.gif  try 
InBlock.gif  { 
InBlock.gifhr = m_pConnection.CreateInstance("ADODB.Connection");//创建Connection对象    
InBlock.gifif(SUCCEEDED(hr))    
InBlock.gif    { 
InBlock.gifhr=m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=shouji.mdb","","",adModeUnknown);//连库 
InBlock.gif    } 
InBlock.gif  } 
InBlock.gif  catch(_com_error e) 
InBlock.gif  { 
InBlock.gifAfxMessageBox("数据库连接失败,确认数据库连接字符串是否正确"); 
InBlock.gif  } 
InBlock.gif  //操纵表 
InBlock.gif_RecordsetPtr m_pRecordset; //记录集对象 
InBlock.gif  UpdateData(TRUE); 
InBlock.gif  CString strSQL; 
InBlock.gifif (m_name=="")                         //路径1 
InBlock.gif  { 
InBlock.gifMessageBox("用户名不能为空!"); //函数A 
InBlock.gif  } 
InBlock.gif  else                                                                                                                                                         {UpdateData(TRUE);        //函数B 
InBlock.gif  int lenth=0; 
InBlock.gif  lenth=m_name.GetLength(); 
InBlock.gifif (lenth>12 || length<2)        //路径2 
InBlock.gif  {MessageBox("输入的用户名不正确或没有该用户!请重新输入!");                 //函数C 
InBlock.gif  } 
InBlock.gif  Else                                                                                                                                                                                 
InBlock.gif  { 
InBlock.gifif(m_pipei)                        //路径3 
InBlock.gif  { 
InBlock.gifstrSQL="SELECT * FROM sj_T_ShouJiKa where 用户姓名 = '"+m_name+"'"//函数D 
InBlock.gif  } 
InBlock.gif  Else    
InBlock.gif  { 
InBlock.gifstrSQL="SELECT * FROM sj_T_ShouJiKa where 用户姓名 like '%"+m_name+"%'";//函数E 
InBlock.gif  } 
InBlock.gif 
InBlock.gif  try 
InBlock.gif  { 
InBlock.gif    hr=m_pRecordset.CreateInstance("ADODB.Recordset"); 
InBlock.gif    if(SUCCEEDED(hr))    
InBlock.gif    { //从数据库中打开表 
InBlock.gifm_pRecordset->Open(_bstr_t(strSQL),m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText); 
InBlock.gif    } 
InBlock.gif    else 
InBlock.gif    { 
InBlock.gif  AfxMessageBox("查询不成功!"); 
InBlock.gif    } 
InBlock.gif  } 
InBlock.gif  catch (_com_error e) 
InBlock.gif  { 
InBlock.gif    CString strError; 
InBlock.gifstrError.Format("警告:打开数据表时发生异常。 错误信息: %s",e.ErrorMessage()); 
InBlock.gif  AfxMessageBox(strError); 
InBlock.gif  return
InBlock.gif  } 
InBlock.gifwhile(!m_pRecordset->adoEOF)     //路径4 
InBlock.gif  {                                                                                 //函数F 
InBlock.gif    _bstr_t name=""; 
InBlock.gif    _bstr_t shoujikahao=""; 
InBlock.gif    _bstr_t tongxinzhishi=""; 
InBlock.gif    _bstr_t fuwushang=""; 
InBlock.gif 
InBlock.gif    int i=0; 
InBlock.gif    m_list5.DeleteAllItems(); 
InBlock.gif 
InBlock.gif    while(!m_pRecordset->adoEOF) 
InBlock.gif    { 
InBlock.gifname=(_bstr_t)m_pRecordset->GetCollect("用户姓名"); 
InBlock.gifshoujikahao=(_bstr_t)m_pRecordset->GetCollect("手机卡号"); 
InBlock.giftongxinzhishi=(_bstr_t)m_pRecordset->GetCollect("通信制式"); 
InBlock.giffuwushang=(_bstr_t)m_pRecordset->GetCollect("服务商"); 
InBlock.gif  m_list5.InsertItem(i,name);    
InBlock.gifm_list5.SetItemText(i,1,shoujikahao);//设置该行的不同列的显示字符 
InBlock.gifm_list5.SetItemText(i,2,tongxinzhishi); 
InBlock.gifm_list5.SetItemText(i,3,fuwushang); 
InBlock.gifm_pRecordset->MoveNext(); 
InBlock.gifi=i+1;     
InBlock.gif    }    
InBlock.gif  }    
InBlock.gif  //关闭连接、释放com资源m_pRecordset->Close();     //路径5 
InBlock.gif  m_pRecordset.Release(); 
InBlock.gif  m_pConnection->Close(); 
InBlock.gif                 m_pConnection.Release(); 
InBlock.gif  CoUninitialize(); 
InBlock.gif  } 
InBlock.gif  } 
InBlock.gif
InBlock.gif 
     我们根据这个程序来画出它的程序控制流图,如下,是我画好的:
        有了图以后我们就要知道到底我们要写多少个测试用例,才能满足基本路径测试。
这里有有了一个新概念——圈复杂度。
圈复杂度是一种为程序逻辑复杂性提供定量测试的软件度量。将该度量用于计算程序的基本独立路径数目。为确保所有语句至少执行一次的测试数量的下界。
公式圈复杂度    V(G)=E-N+2,E是流图中边的数量,N是流图中结点的数量。
从图中我们可以看到,
V(G)=8条边-6结点+2=4
上图的圈复杂图是4。这个结果对我们来说有什么意义呢?它表示我们只要最多4个测试用例就可以达到基本路径覆盖。
下一步我们就要导出程序基本路径。
程序基本路径:基本独立路径就是从程序的开始结点到结束可以选择任何的路径遍历,但是每条路径至少应该包含一条已定义路径不曾用到的边。
我们可以得到基本路径是:
1:  A
2:  B->C
3:  B->D->F
4:  B->E->F
下面我们开始写测试用例。
     “详细查询测试”做完了吗?没有,因为对于上表的每一个路径,如果结果有不同的,即:结果有对的,也有不对的。那么,我们就还需要进行进一步的测试,下面的工作我就不做了,照搬就是。


     本文转自阿龙哥 51CTO博客,原文链接:http://blog.51cto.com/ililong/303128,如需转载请自行联系原作者







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值