Pex: For String.IsNullOrEmpty Pex generates only two test methods
I have a simple method with a single condition like this.
if (String.IsNullOrEmpty(FirstName))
{
success = false;
}
return success;
When I run Pex it generates only one test case which assigns Null to FirstName property and the other with assigns "\0" to the FirstName.
Why开发者_StackOverflow社区 is it not generating a third method which will assign string.Empty to the FirstName property?
As I understand it, Pex just tries to achieve 100% test coverage in your application code. From the code you posted, it would only take two tests to trace all the branches of that method.
- The string is not null or empty.
- The string is null or empty.
I'm guessing that Pex is not configured to examine the internals of .Net libraries so it doesn't know that empty string will be special value for the IsNullOrEmpty
function. Null and the null character ('\0') are its two favorite choices for testing strings if it isn't able to examine how the string is used.
You can create a parametrized unit test to check the empty string if you want.
As Joshua Dale says, Pex attempts to generate tests that cover as many code branches as possible. As it says in the first paragraph of the Pex Reference Manual:
Given a method, the [sic] Microsoft Pex generates inputs which exercise many different code paths. In order [sic] words, Microsoft Pex aims at generating a test suite that achieves maximum code coverage.
(As you can see, this document could do with some proof-reading!)
It's important to bear this in mind, so Pex will generate test inputs designed to execute all your code branches, not generate test inputs with semantic value (expect as where that is general). It's important to realise this and not assume that the test suite that Pex generates means that your tests have covered all the possible failure conditions. It could potentially cover very few of them- the test inputs are designed to hit edge-cases (e.g. null/ the null-character), which is obvious if you consider that the purpose is to exercise as many code branches as possible.
Pex attempts to explore code branches that your own tests don't discover. It's a complement to your intelligence- as a human you are good at figuring out what the code should do, as a Turing machine, Pex is good at picking through every possible code branch (though it often needs help.)
精彩评论