[英]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.