③ Assert.AreSame
判断实际值跟预期值的类型是否一致!
④ Assert.AreNotSame
跟上面正好相反,测试类型不一致!
⑤ Assert.IsNotNull,Assert.IsNull,Assert.IsTrue,Assert.IsFalse
看这些方法名就知道什么意思了!
⑥ Assert.IsInstanceOfType,Assert.IsNotInstanceOfType
判断指定的对象是否是指定的类型!
⑦ Assert.Fail
迫使断言失败,不管前面的断言是否都成功了,但测试结果是错误的,因为我强制断言失败了!
总结:一个好的测试案例,里面的断言至少是大于一个的,这样才能验证数据的准确性,保证验证数据的严谨性!
例:如果实际值是个DataSet一般测试流程为:①判断是否为“null” → ②判断是否为“Empty”(验证是否有数据) →③ 接下来再验证数据的一致性,所以验证DataSet的基本流程就是这样的!
/// <summary> Assert.IsNotNull(actual); //第一步 验证是否为Null |
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); } |