0
我想測試以下兩種(無關)的方法和使用OpenCover 2.0.802.1我將如何獲得完整的OpenCover分支機構覆蓋這些代碼示例?
public class Methods
{
public static void MethodWithDelegate(SynchronizationContext context)
{
context.Send(delegate { Console.Beep(); }, null);
}
public static string MethodWithSwitchStatement(Type value)
{
string output = string.Empty;
if (value != null)
{
switch (value.ToString())
{
case "System.Int32":
output = "int";
break;
default:
output = "other type";
break;
}
}
return output;
}
}
我寫了下面的(NUnit的)的測試中,一個用「起訂量」模擬對象實現全面分支和語句覆蓋:
[Test]
public void Test_ShouldCoverMethodWithDelegate()
{
var context = new Mock<SynchronizationContext>();
Methods.MethodWithDelegate(context.Object);
context.Verify(x => x.Send(It.IsAny<SendOrPostCallback>(), It.IsAny<object>()));
}
[Test]
public void Test_ShouldCoverSwitchStatement()
{
Assert.That(Methods.MethodWithSwitchStatement(null), Is.EqualTo(string.Empty));
Assert.That(Methods.MethodWithSwitchStatement(typeof(int)), Is.EqualTo("int"));
Assert.That(Methods.MethodWithSwitchStatement(typeof(float)), Is.EqualTo("other type"));
}
然而,通過OpenCover運行測試後,coverage.xml
文件總是包含一個帶有兩個測試零訪問計數的分支點。序列覆蓋顯示100%。
不是一位IL專家,我不確定我會怎麼去編寫進一步的測試來使分支覆蓋率達到100%。
有趣的...非常感謝您的答案 - 和OpenCover也是,太棒了! –
不客氣 - 我也做了一篇關於序列報道的文章,以及你可能會覺得有用的http://scubamunki.blogspot.com/2011/08/problem-with-sequence-coverage.html –
好文章, ReportGenerator不支持(但)完全支持分支覆蓋的顯示是一件令人遺憾的事情。在瀏覽coverage.xml文件時,我發現上面提到的'問題'。 –