Not sure if I understand the concept!

May 29, 2008 at 1:45 AM

I am having a hard time grasping the concept of PEX. Why and when should I use it? It only works with parameterazied unit test. What if I don't want to make parameterized unit tests. Also, if the tests are parameterized then how can I find it my test succeeded or not. Take a look at this example. 

  [PexMethod]       
        public void should_be_able_to_calculate_average_of_n_numbers(int[] numbers)
        {
            Calculator cal = new Calculator();
            Assert.AreEqual(2,cal.ComputeAverage(numbers));
        }

 public double ComputeAverage(int[] numbers)
        {
            if (numbers == null) throw new ArgumentException(); 

            return  numbers.Average();        
        }

PEX tries to pass null instead of numbers and it fails. Even if I decorate the PEX METHOD with ExpectedException it fails. Also, if pex is generating numbers[] for me then how can I have my expectation set. 

Coordinator
May 29, 2008 at 11:38 AM
I think you should look at the PexAssume method. This allows you to insert conditions that Pex must satisfy for the generated input to a PUT. For example, adding "PexAssume.IsNotNull(numbers)" to your PexMethod will tell Pex not to pass in "null" for the array. (But then you are skipping an important test case).

Similarly if you add something like "PexAssume.AreEqual(2, cal.ComputeAverage(numbers)" Pex should only pass in arrays (which may be of length 1 or more) where the average (as determined by ComputeAverage) is 2. (But if ComputeAverage erroneously determines the average, a bug may still be hidden).

Note that Pex does not address all aspects of testing, but focuses on parameterized tests (the "P" in "Pex").