locked
Constrain PEX code-under-test to a single method? RRS feed

  • Question

  • Can the code-under-test in PEX be constrained to a single method? I am aware you can constrain it to assemblies and classes, but what about members?

    Essentially I am trying to achieve 100% code coverage for the following method:

    public virtual bool GetLastSymbol(string symbolHint, out string symbol)
    {
        if (symbolHint == null)
        {
            throw new ArgumentNullException("symbolHint");
        }

        IEnumerable<string> symbols;
        symbol = this.VariableHints.TryGetValue(symbolHint, out symbols) ? symbols.Last() : null;

        return symbol != null;
    }

    The following PUT achieves 12/15 code coverage, because I’m only testing for 1 of the possible 2 values it can return:

    found = symbolManager.GetLastSymbol(symbolHint, out symbol);
    PexAssert.IsFalse(found);

    To achieve full coverage for this PUT, I need to change the object’s state so that the method hits both branches. I could satisfy this by using separate PUTs using a factory method to setup the different states, but this would leave me with 2 PUTs with incomplete code coverage, rather than 1 PUT with full coverage. I realise in theory the 2 PUTs would have a combined coverage of 100%, but I need that 100% figure in practice so I can setup CI properly.

    So, to reach the other branch in the same PUT, I must append the following code to the above 2 lines:

    symbolManager.CreateSymbol(symbolHint); // Ensure next call returns true.
    found = symbolManager.GetLastSymbol(symbolHint, out symbol);
    PexAssert.IsTrue(found); 

    Presumably the code coverage for the GetLastSymbol method is now 100%, but because I’ve introduced another method call to the type under test, the code coverage now drops to 20/29

    How can I constrain a PUT to only measure code coverage for a single method? I realise I may have misunderstood this metric entirely, so please explain why if this is the case :)




    • Edited by ljwagerfield Thursday, September 22, 2011 11:08 AM
    Thursday, September 22, 2011 11:06 AM

Answers