Pex: Allowing of an Exception

Damir Dobric Posts

Next talks:

 

Damir@Phone    

Follow me on Twitter: #ddobric



 

 

Archives

During discovering of the code (method) Pex may find a number of different parameter variations. For each variation of input parameters the program will step through a different execution paths. In the example below one method is shown, which implements three different paths 1, 3 and 4. 

public string Method1(int a, int b, string st)
{
           string res = "result: ";

           if (a > b)
           {
               res += "a > b";
           }
           else if (b > a)
           {
               res += "a < b";
           }
           else
           {
               res += "a = b";
           }

           return res + st.Length;
       }

 

In the line 4 at picture below, PEX has found that for st=null, the method will throw a NullReferenceException. At this point there are in general two thing you can do:

a) You should define a precondition, which specifies a kind of contract for the method.
In this case caller should be aware of your assumption in precondition. To do this select "Add Precondition". This will create following code in the method:

public string Method1(int a, int b, string st)
{
         CodeContract.Requires(st != (string)null);

         // Contract.Requires(st != (string)null); In. NET40
          . . .
}

 

b) You leave the code as it is.
In this case you can disable PEX to take a care about this exception. The PEX would just ignore it while executing the test.
Tp do this select "Allow Exception" and PEX will inject following peace of code in PexAssemblyInfo.cs file which has been generated in TestProject by PEX.

[assembly: PexAllowedExceptionFromAssembly(typeof(NullReferenceException), "Microsoft.ExtendedReflection")]


If you want to prevent this behavior in the future, just remove this line.

image


Posted Feb 14 2009, 04:15 PM by Damir Dobric
Filed under:
developers.de is a .Net Community Blog powered by daenet GmbH.