Unit Test creation using Pex

Ambily.raj
Posted by in Others category on for Beginner level | Points: 250 | Views : 8050 red flag
Rating: 5 out of 5  
 2 vote(s)

Pex is one of the Visual Studio add-in that provides a runtime code analysis tool for .NET Framework code. The output of the Pex helps you understand the behavior of the code, and it also builds automated tests with high code coverage.

Pex

Pex is one of the Visual Studio add-in that provides a runtime code analysis tool for .NET Framework code. The output of the Pex helps you understand the behavior of the code, and it also builds automated tests with high code coverage. Along with Pex, we have Moles, which is used for mocking the objects in Unit test. In this article, we will discuss about Pex and how we can create Unit tests using Pex.
 
You can find the details of Pex and how to install the same from http://research.microsoft.com/en-us/projects/pex/downloads.aspx

Run Pex

For our sample, I have a Utility class with two methods, Sum and Greater. For finding various values that cover all the code paths of our code, right click on the method and select “Run Pex” from the context menu.



 
This will open the Pex Exploration Results window with all possible combination of input values.

For our sample Greater method, Pex added 6 test cases. We can save these unit test cases into a file using the Save Test option.



Also, Pex gives the code coverage information. Click on the hypherlink appeared on top of the Test cases in Pex explorer to get the code coverage report.


 

Parameterized Unit Test

We can create Parameterized unit tests using Pex. If our method is expecting any custom types, Pex may not be able to generate the expected values. In this case, we will create parameterized unit tests and pass the values using either Moles or Factory. We will discuss about Moles and factories latter.
 
For creating Parameterized Unit test, right click on the class and select the Pex -> Create Parameterized Unit Tests option from the context menu.


 
This will open the Create Parameterized Unit Tests window, where we can specify the Selection filters and the test Framework. As you can see, our selection includes the namespace filter and type name filter indicating the class. For this demo, I am using the Visual Studio Unit Test framework as the Test framework.


 
As we are going to add a new test project, it will open the Add new Test project window. Here, we can specify the Test project location and name. 

 

Pex generated unit test for our sample class consist of one parameterized unit tests for each of our methods, Sum and Greater.

namespace SampleApp

{

    /// <summary>This class contains parameterized unit tests for Utility</summary>

    [PexClass(typeof(Utility))]

    [PexAllowedExceptionFromTypeUnderTest(typeof(InvalidOperationException))]

    [PexAllowedExceptionFromTypeUnderTest(typeof(ArgumentException), AcceptExceptionSubtypes = true)]

    [TestClass]

    public partial class UtilityTest

    {

        /// <summary>Test stub for Greater(Int32, Int32)</summary>

        [PexMethod]

        public int Greater(

            [PexAssumeUnderTest]Utility target,

            int n1,

            int n2

        )

        {

            int result = target.Greater(n1, n2);

            return result;

            // TODO: add assertions to method UtilityTest.Greater(Utility, Int32, Int32)

        }

 

        /// <summary>Test stub for Sum(Int32, Int32)</summary>

        [PexMethod]

        public int Sum(

            [PexAssumeUnderTest]Utility target,

            int n1,

            int n2

        )

        {

            int result = target.Sum(n1, n2);

            return result;

            // TODO: add assertions to method UtilityTest.Sum(Utility, Int32, Int32)

        }

    }

}


Now, let us add the Unit tests with expected input values. Run the Pex Exploration against the Unit test class.


 
This will add the Unit tests with Pex generated values.  For our sample, Pex explorer added separate classes for each of the methods.




Conclusion

Pex makes it very easy for a developer to create unit tests with high code coverage. Pex generate different parameter values to cover all code paths. We will look deeper into Pex and Moles latter.

Page copy protected against web site content infringement by Copyscape

About the Author

Ambily.raj
Full Name: Ambily KK
Member Level: Silver
Member Status: Member,Microsoft_MVP,MVP
Member Since: 5/18/2010 1:05:25 AM
Country: India
Thanks Ambily K K http://ambilykk.com/
http://ambilykk.com/
I have over 9 years of experience working on Microsoft Technologies. I am carrying the passion on Microsoft technologies specifically on web technologies such as ASP .Net and Ajax. My interests also include Office Open XML, Azure, Visual Studio 2010. Technology adoption and learning is my key strength and technology sharing is my passion.

Login to vote for this post.

Comments or Responses

Posted by: Madhu.b.rokkam on: 2/25/2011 | Points: 25
Thank you for a very nice article Ambily.. Keep up the good work
Posted by: Tripati_tutu on: 2/25/2011 | Points: 25
Very good article...
Posted by: Samarmir on: 2/26/2011 | Points: 25
Very nice article
Thanks a lot for this.

Posted by: Madhu.b.rokkam on: 2/26/2011 | Points: 25
Hey, I tried this and this is good one.
Posted by: Ambily.raj on: 2/27/2011 | Points: 25

Thanks to all.

Regards
Ambily

Login to post response

Comment using Facebook(Author doesn't get notification)