[英]Code Coverage in Visual Studio on Time
我在Visual Studio 2013中編寫單元測試,但代碼覆蓋率分析遇到問題。 語言為C#,並使用默認的Microsoft.VisualStudio.TestTools.UnitTesting。
if (exsistingNode.NodeState > NodeState.Edit)
{
if (existingNode.NodeName != updatedNode.NodeName)
{
throw m_Diagnostics.ThrowCannotUpdateAfterEditState(subject, existingNode.NodeId, "NodeName");
}
if (existingNode.StartDate != updatedNode.StartDate)
{
throw m_Diagnostics.ThrowCannotUpdateAfterEditState(subject, existingNode.NodeId, "StartDate");
}
if (existingNode.Description != updatedNode.Description)
{
throw m_Diagnostics.ThrowCannotUpdateAfterEditState(subject, existingNode.NodeId, "Description");
}
}
我已經為每個if語句進行了單元測試。 我的第一個成功的單元測試通過了所有這些測試,並且沒有引發錯誤,然后我有三個單獨的單元測試,分別測試每個if語句並引發錯誤。 我的問題是代碼分析顯示以下語句:
if (existingNode.StartDate != updatedNode.StartDate)
因為僅進行了部分測試,我無法理解為什么。 這是因為它需要時間嗎?
在下面的代碼中也是如此,其中代碼覆蓋了引發,但是部分if語句覆蓋了所有代碼。
if (updatedNode.StartDate != existingNode.StartDate)
{
if (updatedNode.StartDate.HasValue && updatedNode.StartDate < DateTime.Now)
{
throw m_Diagnostics.ThrowUpdateNodeException(subject, existingNode.NodeId, new InvalidOperationException("Cannot set the start date less than now."));
}
if (updatedNode.StartDate.HasValue && updatedNode.EndDate.HasValue && updatedNode.EndDate < updatedNode.StartDate)
{
throw m_Diagnostics.ThrowUpdateNodeException(subject, existingNode.NodeId, new InvalidOperationException("Cannot set the end date less than the start date."));
}
}
if (updatedNode.EndDate != existingNode.EndDate)
{
if (updatedNode.EndDate.HasValue && updatedNode.EndDate < DateTime.Now)
{
throw m_Diagnostics.ThrowUpdateNodeException(subject, existingNode.NodeId, new InvalidOperationException("Cannot set the end date lessthan now."));
}
有人要求提供單元測試的一些示例。 接下來是與第一種情況有關的兩個。 如果您在其他情況下需要它們,請告訴我。
[TestMethod] // Start Date Test that is only Partially covered
[ExpectedException(typeof(Exception))]
public void ChangeStartDate() // Unit Test where the Start Date is changed when not in Edit Mode
{
//Arrange
Node nodeToUpdate = new Node
{
NodeName = "nodeName",
StartDate = DateTime.Now.AddDays(1),
NodeState = NodeState.Open
};
collectionNode.Add(nodeToUpdate);
Node updatedNode = new Node
{
NodeName = "nodeName",
StartDate = DateTime.Now.AddDays(2),
NodeState = NodeState.Open
};
INodeStore stubNodeStore = new CENSORED.StubINodeStore
{
GetNodeString = (NodeId) =>
{
return nodeToUpdate;
}
};
server.NodeStoreStubs = stubNodeStore;
INodePrivilegeStore stubNodePrivilegeStore = new CENSORED.StubINodePrivilegeStore
{
GetPrivilegeStringString = (NodeId, subject) =>
{
return new NodePrivilege
{
Permissions = NodePermissions.Administrator
};
}
};
server.NodePrivilegeStoreStubs = stubNodePrivilegeStore;
m_NodeManager = ThrowHelper.GetServiceOrThrow<INodeManager>(server);
//Act
m_NodeManager.UpdateNode(updatedNode);
//Assert
}
[TestMethod] // Unit Test that is fully covered
[ExpectedException(typeof(Exception))]
public void ChangeStartDate() // Unit Test where the Start Date is changed when not in Edit Mode
{
//Arrange
Node nodeToUpdate = new Node
{
NodeName = "nodeName",
StartDate = DateTime.Now.AddDays(1),
Description = "description,
NodeState = NodeState.Open
};
collectionNode.Add(nodeToUpdate);
Node updatedNode = new Node
{
NodeName = "nodeName",
StartDate = DateTime.Now.AddDays(1),
Description = "updatedDescription"
NodeState = NodeState.Open
};
INodeStore stubNodeStore = new CENSORED.StubINodeStore
{
GetNodeString = (NodeId) =>
{
return nodeToUpdate;
}
};
server.NodeStoreStubs = stubNodeStore;
INodePrivilegeStore stubNodePrivilegeStore = new CENSORED.StubINodePrivilegeStore
{
GetPrivilegeStringString = (NodeId, subject) =>
{
return new NodePrivilege
{
Permissions = NodePermissions.Administrator
};
}
};
server.NodePrivilegeStoreStubs = stubNodePrivilegeStore;
m_NodeManager = ThrowHelper.GetServiceOrThrow<INodeManager>(server);
//Act
m_NodeManager.UpdateNode(updatedNode);
//Assert
}
在進行“如果”測試時,您應該考慮所有組合可能出現的所有情況。 如果不確定if的哪一部分未正確測試,請嘗試將其分開,請查看未對兩個值都測試過的'if'語句。
public bool IsTimeBeforeNow(Nullable<DateTime> time)
{
if (time.HasValue && time.Value < DateTime.Now)
return true;
return false;
}
[TestMethod]
public void TestIsTimeBeforeNow()
{
//test time.HasValue with null
Assert.AreEqual(false, this.IsTimeBeforeNow(null));
//test time.HasValue with true
//and test time.Value > DateTime.Now
Assert.AreEqual(false, this.IsTimeBeforeNow(DateTime.Now.AddDays(1)));
//test time.HasValue with true
//and test time.Value < DateTime.Now
Assert.AreEqual(true, this.IsTimeBeforeNow(DateTime.Now.AddDays(-1)));
}
關鍵是測試每個邏輯路徑。
如果具有“ if”語句,則必須測試if == true和if == false方案,以具有完整的代碼覆蓋率。
當您具有“ switch”語句時,必須測試所有可能的情況,包括默認情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.