MSTest Shows Partial Code Coverage on Compound Boolean Expressions

此生再无相见时 提交于 2019-12-22 04:04:54

问题


From Microsoft's documentation, partially covered code is "...where some of the code blocks within the line were not executed." I'm pretty stumped on this one (simplified for brevity):

Given this method:

public List<string> CodeUnderTest()
{
    var collection = new List<string> { "test1", "test2", "test3" };
    return collection.Where(x => x.StartsWith("t") && x == "test2").ToList();
}

And this test:

[TestMethod]
public void Test()
{
    var result = new Class1().CodeUnderTest();
    CollectionAssert.Contains(result, "test2");
}

Code coverage results shows that the expression x.StartsWith("t") && x == "test2 is only partially covered. I'm not sure how that's possible unless the compiler or CLR has some sort of eager condition matching stuff, but maybe I just need to have it explained.


回答1:


The conditional-AND operator (&&) performs a logical-AND of its bool operands, but only evaluates its second operand if necessary.

http://msdn.microsoft.com/en-us/library/2a723cdk(v=vs.100).aspx

so you would expect both sides to be covered

perhaps what it is complaining about is that you haven't tested the -ve paths i.e. if your collection is

var collection = new List<string> { "test1", "test2", "test3", "not_this_one" };

this way you test the x.StartsWith("t") being T/F because currently only the T path is being tested for that condition.



来源:https://stackoverflow.com/questions/12947507/mstest-shows-partial-code-coverage-on-compound-boolean-expressions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!