{"id":2016,"date":"2014-06-08T08:49:55","date_gmt":"2014-06-08T08:49:55","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=2016"},"modified":"2014-06-08T08:49:55","modified_gmt":"2014-06-08T08:49:55","slug":"introduction-to-using-pex-with-microsoft-code-digger","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/introduction-to-using-pex-with-microsoft-code-digger\/","title":{"rendered":"Introduction to using Pex with Microsoft Code Digger"},"content":{"rendered":"<p>This post is specific to the Code Digger Add-In, which can be used with Visual Studio 2012 and 2013.<\/p>\n<p><strong>Requirements<\/strong><\/p>\n<ul>\n<li>Download the <a href=\"http:\/\/visualstudiogallery.msdn.microsoft.com\/fb5badda-4ea3-4314-a723-a1975cbdabb4\" title=\"Microsoft Code Digger add-in\" target=\"_blank\">Microsoft Code Digger add-in<\/a><\/ul>\n<p>This will appear in Tools | Extensions and Updates and ofcourse can be downloaded via this dialog.<\/p>\n<p><strong>What is Pex ?<\/strong><\/p>\n<p>So Pex is a tool for automatically generating test suites. Pex will generate input-output values for your methods by analysing the flow etc. and arguments required by the method.<\/p>\n<p><strong>What is Code Digger ?<\/strong><\/p>\n<p>Code Digger supplies an add-in for Visual Studio which allows us to select a method and generate input\/outputs using Plex and display the results within Visual Studio.<\/p>\n<p><strong>Let&#8217;s use Code Digger<\/strong><\/p>\n<p>Enough talk, let&#8217;s write some code and try it out.<\/p>\n<p>Create a new solution, I&#8217;m going to create a &#8220;standard&#8221; class library project. Older versions of Code Digger only worked with PCL&#8217;s but now (I&#8217;m using 0.95.4) you can go to Tools | Options in Visual Studio, select Pex&#8217;s General option and change DisableCodeDiggerPortableClassLibraryRestriction to True (if it&#8217;s not already set to this) and run Pex against non-PCL code.<\/p>\n<p>Let&#8217;s start with a very simple class and a few methods<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic static class Statistics\r\n{\r\n   public static double Mean(double&#x5B;] values)\r\n   {\r\n      return values.Average();\r\n   }\r\n\r\n   public static double Median(double&#x5B;] values)\r\n   {\r\n      Array.Sort(values);\r\n\r\n      int mid = values.Length \/ 2;\r\n      return (values.Length % 2 == 0) ?\r\n         (values&#x5B;mid - 1] + values&#x5B;mid]) \/ 2 :\r\n         values&#x5B;mid];\r\n   }\r\n\r\n   public static double&#x5B;] Mode(double&#x5B;] values)\r\n   {\r\n      var grouped = values.GroupBy(v =&gt; v).OrderBy(g =&gt; g.Count());\r\n      int max = grouped.Max(g =&gt; g.Count());\r\n\t\t\t\r\n      return (max &lt;= 1) ?\r\n         new double&#x5B;0] :\r\n         grouped.Where(g =&gt; g.Count() == max).Select(g =&gt; g.Key).ToArray();\r\n      }\r\n   }\r\n}\r\n<\/pre>\n<p>Now you may have noticed we do not check for the &#8220;values&#8221; array being null or empty. This is on purpose, to demonstrate Pex detecting possible failures.<\/p>\n<p>Now, we&#8217;ll use the Code Digger add-in.<\/p>\n<p>Right mouse click on a method, let&#8217;s take the Mean method to begin with, and select <em>Generate Inputs \/ Outputs Table<\/em>. Pex will run and create a list of inputs and outputs. In my code for Mean, I get two failures. Pex has executed my method with a null input and an empty array, both cases are not handled (as mentioned previously) by my Mean code. <\/p>\n<p>If you now try the other methods you should see more similar failures but hopefully more successes with more input values.<\/p>\n<p>Unfortunately (at the time of writing at least) there doesn&#8217;t appear to be an option in Code Digger to generate either unit tests automatically or save the inputs for my own unit tests. So for now you&#8217;ll have to manually write your tests with the failing inputs and implement code to make those work.<\/p>\n<p><em>Note: I did find at one time the Generate Inputs \/ Outputs Table menu option missing, I disable and re-enabled the Code Digger Add-In and restarted Visual Studio and it reappeared.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is specific to the Code Digger Add-In, which can be used with Visual Studio 2012 and 2013. Requirements Download the Microsoft Code Digger add-in This will appear in Tools | Extensions and Updates and ofcourse can be downloaded via this dialog. What is Pex ? So Pex is a tool for automatically generating [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[28],"tags":[],"class_list":["post-2016","post","type-post","status-publish","format-standard","hentry","category-testing"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=2016"}],"version-history":[{"count":25,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2016\/revisions"}],"predecessor-version":[{"id":2049,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2016\/revisions\/2049"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=2016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=2016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=2016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}