走进单元测试三:实战单元测试

发表于:2013-8-08 11:02

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:wangyong0921    来源:51Testing软件测试网采编

  ③ Assert.AreSame

  判断实际值跟预期值的类型是否一致!

  ④ Assert.AreNotSame

  跟上面正好相反,测试类型不一致!

  ⑤ Assert.IsNotNull,Assert.IsNull,Assert.IsTrue,Assert.IsFalse

  看这些方法名就知道什么意思了!

  ⑥ Assert.IsInstanceOfType,Assert.IsNotInstanceOfType

  判断指定的对象是否是指定的类型!

  ⑦ Assert.Fail

  迫使断言失败,不管前面的断言是否都成功了,但测试结果是错误的,因为我强制断言失败了!

  总结:一个好的测试案例,里面的断言至少是大于一个的,这样才能验证数据的准确性,保证验证数据的严谨性!

  例:如果实际值是个DataSet一般测试流程为:①判断是否为“null” → ②判断是否为“Empty”(验证是否有数据) →③ 接下来再验证数据的一致性,所以验证DataSet的基本流程就是这样的!

/// <summary>
/// Modify.
/// </summary>
[TestMethod()]
[RollBack()]
public void UpdateBondAssessApplicationConfiguration_ModifyData_DataUpdated() //注意命名规范,下一篇会着重讲解!
{
AssessApplicationConfigurationDataSet assessConfigurationDataSet = target.GetBondAssessApplicationConfiguration();
assessConfigurationDataSet.CM_LookupConfiguration[0].Description = "This is my modify";
AssessApplicationConfigurationDataSet actual = target.UpdateBondAssessApplicationConfiguration(assessConfigurationDataSet);
AssessApplicationConfigurationDataSet newAssessAppDataSet = target.GetBondAssessApplicationConfiguration();

Assert.IsNotNull(actual);                                                                       //第一步 验证是否为Null
Assert.IsTrue(actual.CM_LookupConfiguration.Rows.Count > 0);                                    //第二步 验证是否为Empty
Assert.IsTrue(newAssessAppDataSet.CM_LookupConfiguration[0].Description == "This is my modify");//第三步 验证数据的一致性
Assert.IsTrue(CompareToTable(newAssessAppDataSet.CM_LookupConfiguration, actual.CM_LookupConfiguration));
}

  4.测试异常 → ExpectedException(异常属性)

  当代码中有抛出异常的情况时,我们应该对这个异常的准确性进行测试,首先要捕获这个异常,然后再跟我预期定义的异常进行比较就行了!

  在VS自带的测试框架中提供了处理异常的测试!

  这个异常属性有两个构造函数重载:

  第一个参数:函数中出现异常的类型 →  [ExpectedException(typeof(NullReferenceException))]

  第二个参数:异常所提示的信息(Message) → [ExpectedException(typeof(NullReferenceException),"Don't is null.")]

  总结:在测试一些非法数据,边界值,异常测试是非常有用的,一旦发现异常,后面的一切断言和代码将跳过,然后系统将会把异常和你预期的异常进行比对,一致则表示通过,反之有错误!

/// <summary>
/// Is null.
/// </summary>
[TestMethod()]
[RollBack()]
[ExpectedException(typeof(NullReferenceException))]  //第一种只定义了异常类型!
public void UpdateLookupChequeNumberRegion_UpdateChequeNumberRegionAndDataSetIsNull_ThrowException()
{
LookupChequeNumberRegionDataSet actual = target.UpdateLookupChequeNumberRegion(null);
}
/// <summary>
/// Add.
/// </summary>
[TestMethod()]
[RollBack()]            //第二种定义了预期的异常类型还定义了异常信息!
[ExpectedException(typeof(BusinessException), "The category and code combination you have entered already exist. Please enter a different category and code combination.")]
public void UpdatePolicyConsideration_AddPolicyTheCODEIsSame_ThrowException()
{
string newGUID = Guid.NewGuid().ToString();
CodeTableDataSet expectedDataSet = target.GetPolicyConsideration();
expectedDataSet.T_IC_CODE.AddT_IC_CODERow(GetNewRow(expectedDataSet, null, ref newGUID));
CodeTableDataSet actual = target.UpdatePolicyConsideration(expectedDataSet);
}

52/5<12345>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号