MSTest shows partial code coverage on compound Boolean expressions

From the Microsoft documentation, the partially covered code "... where some of the code blocks in the line did not execute." I'm pretty dumb on this (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"); } 

The code coverage results show that the expression x.StartsWith("t") && x == "test2 is only partially covered. I'm not sure how possible it is if the compiler or the CLR does not have some element that satisfies the requirements, but maybe I just need to explain it.

+8
c # unit-testing code-coverage mstest
source share
1 answer

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

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

so you expect both sides to be covered

perhaps what he complains about is that you have not tested the -ve paths, i.e. if your collection

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

this way you test x.StartsWith("t") as T / F, because currently only the T path is checked for this condition.

+9
source share

All Articles