一个全面完整的单元测试,是测试整个的代码流程及业务逻辑,当另外一个人在不清楚你的需求接手优化或者重写你的功能时,完整全面的单元测试,就像一份代码需求文档,开发者可以在不了解需求情况下,根据单元测试来复现你所编写的逻辑代码。
完整严谨的单元测试:一般具有一下特点
- 业务逻辑严谨,具有逻辑顺序性(如一条数据的添加>修改>删除)
- 测试的范围、条件 、维度广泛
- 测试的数据灵活,测试的数据不是固定或特定的
- 不占用数据资源,在保证测试通过的情况下又能保证不会有测试数据插入(即测试通过测试数据销毁)
- 对测试操作的数据验证
######业务逻辑严谨,具有逻辑顺序性
- 下面的单元测试首先是:
AddTests()添加>ApprovalTests()审批>DeleteTests()删除>IndexFilterTests查询
######测试的维度广泛
- 测试功能各种不满的条件;对每个条件进行验证
######测试的数据灵活
- 根据数据库符合的数据进行测试添加(而不是固定指定的参数)
ELECT TOP(1) Card_No FROM tbCRM_Card_Master WHERE Status_ID=0
var sql = @"SELECT TOP(1) Card_No FROM tbCRM_Card_Master WHERE Status_ID=0";
DataTable dt = DbHelperSql.Query(DbHelperSql.DefaultQueryConn, sql).Tables[0];
StaffWhiteListsDto whiteListDto4 = new StaffWhiteListsDto();
whiteListDto4.CardNo =int.Parse(dt.Rows[0]["Card_No"].ToString());
whiteListDto4.CreateUser = "10070367";
######不占用数据资源
- 测试通过测试数据销毁
只需要把需要事务包裹的逻辑块写在using (TransactionScope ts = new TransactionScope())中就可以了。从这种写法可以看出,TransactionScope实现了IDispose接口。除非显示调用ts.Complete()方法。否则,系统不会自动提交这个事务。如果在代码运行退出这个block后,还未调用Complete(),那么事务自动回滚了。在这个事务块中,u.ADD()方法和t.ADD()方法内部都没有用到任何事务类。
TransactionScope是基于当前线程的,在当前线程中,调用Transaction.Current方法可以看到当前事务的信息。具体关于TransactionScope的使用方法,已经它的成员方法和属性,可以查看 MSDN 。
TransactionScope类是可以嵌套使用,如果要嵌套使用,需要在嵌套事务块中指定TransactionScopeOption参数。默认的这个参数为Required。
该参数的具体含义可以参考http://msdn.microsoft.com/zh-cn/library/system.transactions.transactionscopeoption(v=vs.80).aspx
//普通业务场景用法
static void Main(string[] args)
{
using (TransactionScope ts = new TransactionScope())
{
userBLL u = new userBLL();
TeacherBLL t = new TeacherBLL();
u.ADD();
t.ADD();
ts.Complete();
}
}
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required,new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))
{
//TransactionScope
//使用事务自动回滚来实现单元测试
}
######验证操作的数据
- Assert.IsTrue(DateTests(“Query”, strSql, parameters));
//验证添加的数据是否存在
string strSql = @"
SELECT *
FROM tbCRM_Staff_White_List WL
INNER JOIN tbCRM_Card_Master CM ON CM.CRM_Domain = WL.CRM_Domain
AND CM.Customer_ID = WL.Customer_ID
AND CM.Status_ID = 0
WHERE CM.Card_No = @Card_No
AND WL.Create_User = @Create_User";
SqlParameter[] parameters = new[] {
new SqlParameter("@Card_No", SqlDbType.Int),
new SqlParameter("@Create_User", SqlDbType.VarChar, 50) };
parameters[0].Value = whiteListDto4.CardNo;
parameters[1].Value = whiteListDto4.CreateUser;
Assert.IsTrue(DateTests("Query", strSql, parameters));
######TDD单元测试
using CRM.Service.Dto;
using CRM.Service.Implement;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rdp.Core;
using Rdp.Core.Data;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Transactions;
namespace CRM.Service.Test.Implement
{
[TestClass()]
public class StaffWhiteListServiceTests
{
[TestInitialize()]
public void Init()
{
CRM.DBUtility.DbHelperSql.CrmQuery = Rdp.Core.Data.DbHelperSql.DefaultQueryConn;
CRM.DBUtility.DbHelperSql.CrmUpdate = Rdp.Core.Data.DbHelperSql.DefaultUpdateConn;
}
[TestMethod()]
public void AddTests()
{
using (TransactionScope ts = new TransactionScope(
TransactionScopeOption.Required,
new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))
{
StaffWhiteListsDto whiteListDto = new StaffWhiteListsDto();
var Tests = new StaffWhiteListService();
var result = new OperateResult();
whiteListDto.CardNo = 666666666;//不存在的卡号
whiteListDto.CreateUser = "10070367";
//预期效果:返回Status=1 则添加失败
result = Tests.Add(whiteListDto);
Assert.IsNotNull(result);
Assert.IsTrue(result.Status == 1);
StaffWhiteListsDto whiteListDto2 = new StaffWhiteListsDto();
whiteListDto2.CardNo = 0;
//预期效果:返回Status=1 则添加失败;添加的会员卡号不能为空
result = Tests.Add(whiteListDto2);
Assert.IsNotNull(result);
Assert.IsTrue(result.Status == 1);
var sql = @"SELECT TOP(1) Card_No FROM tbCRM_Card_Master WHERE Status_ID=0";
DataTable dt = DbHelperSql.Query(DbHelperSql.DefaultQueryConn, sql).Tables[0];
StaffWhiteListsDto whiteListDto4 = new StaffWhiteListsDto();
whiteListDto4.CardNo =int.Parse(dt.Rows[0]["Card_No"].ToString());
whiteListDto4.CreateUser = "10070367";
//预期效果:返回Status=0 则添加成功
result = Tests.Add(whiteListDto4);
Assert.IsNotNull(result);
Assert.IsTrue(result.Status == 0);
//验证添加的数据是否存在
string strSql = @"
SELECT *
FROM tbCRM_Staff_White_List WL
INNER JOIN tbCRM_Card_Master CM ON CM.CRM_Domain = WL.CRM_Domain
AND CM.Customer_ID = WL.Customer_ID
AND CM.Status_ID = 0
WHERE CM.Card_No = @Card_No
AND WL.Create_User = @Create_User";
SqlParameter[] parameters = new[] {
new SqlParameter("@Card_No", SqlDbType.Int),
new SqlParameter("@Create_User", SqlDbType.VarChar, 50) };
parameters[0].Value = whiteListDto4.CardNo;
parameters[1].Value = whiteListDto4.CreateUser;
Assert.IsTrue(DateTests("Query", strSql, parameters));
}
}
[TestMethod()]
public void ApprovalTests()
{
using (TransactionScope ts = new TransactionScope(
TransactionScopeOption.Required,
new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))
{
StaffWhiteListsDto whiteListDto = new StaffWhiteListsDto();
var Tests = new StaffWhiteListService();
var result = new OperateResult();
whiteListDto.StatusFlag = 0;
whiteListDto.ApprovalUser = "10070367";
whiteListDto.Remark = "单元测试";
whiteListDto.CustomerID = 399336321;
whiteListDto.CardNo = 666666666;
//预期效果:返回Status=1 审批失败
result = Tests.Approval(whiteListDto);
Assert.IsNotNull(result);
Assert.IsTrue(result.Status == 1);
StaffWhiteListsDto whiteListDto2 = new StaffWhiteListsDto();
whiteListDto2.CardNo = 0;
//预期效果:返回Status=1 审批失败 会员卡号不能为空
result = Tests.Approval(whiteListDto2);
Assert.IsNotNull(result);
Assert.IsTrue(result.Status == 1);
StaffWhiteListsDto whiteListDto3 = new StaffWhiteListsDto();
whiteListDto3.CardNo = 610001101;
whiteListDto3.ApprovalUser = null;
//预期效果:返回Status=1 审批失败 审批人不能为空
result = Tests.Approval(whiteListDto3);
Assert.IsNotNull(result);
Assert.IsTrue(result.Status == 1);
StaffWhiteListsDto whiteListDto4 = new StaffWhiteListsDto();
whiteListDto4.CardNo = 610001101;
whiteListDto4.ApprovalUser = "10070367";
whiteListDto4.CustomerID = 0;
//预期效果:返回Status=1 审批失败 会员编号不能为空
result = Tests.Approval(whiteListDto4);
Assert.IsNotNull(result);
Assert.IsTrue(result.Status == 1);
StaffWhiteListsDto whiteListDto5 = new StaffWhiteListsDto();
whiteListDto5.CardNo = 610001101;
whiteListDto5.ApprovalUser = "10070367";
whiteListDto5.CustomerID = 399336321;
whiteListDto5.StatusFlag = 3;
//预期效果:返回Status=1 审批失败 审批状态有误
result = Tests.Approval(whiteListDto5);
Assert.IsNotNull(result);
Assert.IsTrue(result.Status == 1);
var sql = @"
SELECT TOP(1) SWL.Seq ,CCM.Card_No ,
SWL.Customer_ID
FROM tbCRM_Staff_White_List SWL
INNER JOIN tbCRM_Card_Master AS CCM WITH(NOLOCK ) ON SWL.CRM_Domain = CCM.CRM_Domain
AND SWL.Customer_ID = CCM.Customer_ID
WHERE Status_Flag = 2 ;";
DataTable dt = DbHelperSql.Query(DbHelperSql.DefaultQueryConn, sql).Tables[0];
StaffWhiteListsDto whiteListDto6 = new StaffWhiteListsDto();
whiteListDto6.Seq= dt.Rows[0]["Seq"].ToString();
whiteListDto6.CardNo = int.Parse(dt.Rows[0]["Card_No"].ToString());
whiteListDto6.ApprovalUser = "10070367";
whiteListDto6.CustomerID = int.Parse(dt.Rows[0]["Customer_ID"].ToString());
whiteListDto6.StatusFlag = 0;
//预期效果:返回Status=1 审批成功
result = Tests.Approval(whiteListDto6);
Assert.IsNotNull(result);
Assert.IsTrue(result.Status == 0);
//验证审批成功的数据是否存在
string strSql = @"
SELECT *
FROM tbCRM_Staff_White_List
WHERE Seq=@Seq AND Customer_ID=@Customer_ID AND Approval_User=@Approval_User AND Status_Flag=@Status_Flag ";
SqlParameter[] parameters = new[] {
new SqlParameter("@Seq", SqlDbType.VarChar,100),
new SqlParameter("@Customer_ID", SqlDbType.Int),
new SqlParameter("@Approval_User", SqlDbType.VarChar,50),
new SqlParameter("@Status_Flag", SqlDbType.Int)
};
parameters[0].Value = whiteListDto6.Seq;
parameters[1].Value = whiteListDto6.CustomerID;
parameters[2].Value = whiteListDto6.ApprovalUser;
parameters[3].Value = whiteListDto6.StatusFlag;
Assert.IsTrue(DateTests("Query", strSql, parameters));
}
}
[TestMethod()]
public void DeleteTests()
{
using (TransactionScope ts = new TransactionScope(
TransactionScopeOption.Required,
new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))
{
StaffWhiteListsDto whiteListDto = new StaffWhiteListsDto();
var Tests = new StaffWhiteListService();
var result = new OperateResult();
var sql = @" SELECT TOP(1)* FROM tbCRM_Staff_White_List WHERE Status_Flag!=1 ORDER BY Customer_ID DESC ;";
var dt = DbHelperSql.Query(DbHelperSql.DefaultQueryConn, sql).Tables[0];
whiteListDto.Seq = dt.Rows[0]["Seq"].ToString();
whiteListDto.UpdateUser = "10070367";
whiteListDto.CustomerID = int.Parse(dt.Rows[0]["Customer_ID"].ToString());
whiteListDto.StatusFlag = int.Parse(dt.Rows[0]["Status_Flag"].ToString());
//预期效果:返回Status=0 则删除成功
result = Tests.Delete(whiteListDto);
Assert.IsNotNull(result);
Assert.IsTrue(result.Status == 0);
Assert.AreEqual("删除成功", result.Message);
StaffWhiteListsDto whiteListDto2 = new StaffWhiteListsDto();
whiteListDto2.CustomerID = 0;
//预期效果:返回Status=1 则删除失败
result = Tests.Delete(whiteListDto2);
Assert.IsNotNull(result);
Assert.IsTrue(result.Status == 1);
StaffWhiteListsDto whiteListDto3 = new StaffWhiteListsDto();
whiteListDto3.CustomerID = int.Parse(dt.Rows[0]["Customer_ID"].ToString());
whiteListDto3.StatusFlag = 1;
//预期效果:返回Status=1 则删除失败
result = Tests.Delete(whiteListDto3);
Assert.IsNotNull(result);
Assert.IsTrue(result.Status == 1);
//验证删除的数据状态是否已失效
string strSql = @"
SELECT *
FROM tbCRM_Staff_White_List
WHERE Seq=@Seq AND Customer_ID=@Customer_ID AND Update_User=@Update_User AND Status_Flag=@Status_Flag ";
SqlParameter[] parameters = new[] {
new SqlParameter("@Seq", SqlDbType.VarChar,100),
new SqlParameter("@Customer_ID", SqlDbType.Int),
new SqlParameter("@Update_User", SqlDbType.VarChar,50),
new SqlParameter("@Status_Flag", SqlDbType.Int)
};
parameters[0].Value = whiteListDto.Seq;
parameters[1].Value = whiteListDto.CustomerID;
parameters[2].Value = whiteListDto.UpdateUser;
parameters[3].Value = 1;
Assert.IsTrue(DateTests("Query", strSql, parameters));
}
}
[TestMethod()]
public void IndexFilterTests()
{
using (TransactionScope ts = new TransactionScope(
TransactionScopeOption.Required,
new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))
{
StaffWhiteListsDto whiteListDto = new StaffWhiteListsDto();
var Tests = new StaffWhiteListService();
var sql = @"SELECT TOP ( 1 )WL.*,CCM.Card_No, ISNULL(CUM.Mobile_Phone1, CUM.Mobile_Phone2) AS Mobile_Phone
FROM tbCRM_Staff_White_List AS WL WITH ( NOLOCK )
INNER JOIN tbCRM_Card_Master AS CCM
WITH ( NOLOCK ) ON CCM.CRM_Domain = WL.CRM_Domain
AND CCM.Customer_ID = WL.Customer_ID
AND CCM.Status_ID = 0
INNER JOIN tbCRM_Customer_Master AS CUM
WITH ( NOLOCK ) ON CUM.CRM_Domain = WL.CRM_Domain
AND CUM.Customer_ID = WL.Customer_ID
ORDER BY WL.Customer_ID DESC ;";
var dt = DbHelperSql.Query(DbHelperSql.DefaultQueryConn, sql).Tables[0];
if (dt.Rows.Count>0)
{
whiteListDto.CRM_Domain = int.Parse(dt.Rows[0]["CRM_Domain"].ToString());
whiteListDto.CustomerID = int.Parse(dt.Rows[0]["Customer_ID"].ToString());
whiteListDto.CardNo = int.Parse(dt.Rows[0]["Card_No"].ToString());
whiteListDto.MobilePhone = dt.Rows[0]["Mobile_Phone"].ToString();
whiteListDto.StatusFlag = int.Parse(dt.Rows[0]["Status_Flag"].ToString());
var pageParam = new Rdp.Core.Data.PageParam() { pageIndex = 1, pageSize = int.MaxValue };
DataTable resultdate = Tests.IndexFilter(whiteListDto, pageParam);
//预期效果:返回Status>0 则查询成功
Assert.IsTrue(resultdate.Rows.Count >0);
}
}
}
[TestMethod()]
public void AddFilterTests()
{
using (TransactionScope ts = new TransactionScope(
TransactionScopeOption.Required,
new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))
{
StaffWhiteListsDto whiteListDto = new StaffWhiteListsDto();
var Tests = new StaffWhiteListService();
var sql = @"
SELECT TOP (1)
CCM.Card_No ,
ISNULL(CUM.Chinese_Family_Name + CUM.Customer_Chi_Name,
CUM.English_Family_Name + CUM.Customer_Eng_Name) AS Customer_Name ,
ISNULL(CUM.Mobile_Phone1, CUM.Mobile_Phone2) AS Mobile_Phone
FROM tbCRM_Card_Master AS CCM WITH ( NOLOCK )
INNER JOIN tbCRM_Customer_Master AS CUM WITH ( NOLOCK ) ON CUM.CRM_Domain = CCM.CRM_Domain
AND CUM.Customer_ID = CCM.Customer_ID
WHERE CCM.Status_ID = 0
ORDER BY CUM.Customer_ID DESC;";
var dt = DbHelperSql.Query(DbHelperSql.DefaultQueryConn, sql).Tables[0];
if (dt.Rows.Count>0)
{
whiteListDto.CardNo = int.Parse(dt.Rows[0]["Card_No"].ToString());
whiteListDto.MobilePhone = dt.Rows[0]["Mobile_Phone"].ToString();
//预期效果:返回Status=0 则查询成功
var pageParam = new Rdp.Core.Data.PageParam() { pageIndex = 1, pageSize = int.MaxValue };
DataTable date = Tests.AddFilter(whiteListDto, pageParam);
Assert.IsTrue(date.Rows.Count > 0);
}
}
}
//验证是否存在操作的数据
public bool DateTests(string type, string TestsSql, SqlParameter[] parameter)
{
if (type == "Query")
{
DataTable DtTest = DbHelperSql.Query(CRM.DBUtility.DbHelperSql.CrmQuery, TestsSql.ToString(), parameter).Tables[0];
if ((DtTest.Rows.Count > 0))
return true;
else
return false;
}
else
{
if (DbHelperSql.ExecuteSql(CRM.DBUtility.DbHelperSql.CrmQuery, TestsSql.ToString(), parameter) > 0)
return true;
else
return false;
}
}
}
}