Skip to main content

Posts

Showing posts from May, 2018

xUnit.net - testing for each value of an enum

Recently, I had to come up with a unit test to test a certain functionality for each value of an enum. Let's say the enum is something like this:          public   enum   Number         {              One   =   1 ,              Two ,              Three ,              Four ,              Five ,              Six         } The unit test I wrote first was:         [ Theory ]         [ InlineData ( Number . One )]         [ InlineData ( Number . Two )]         [ InlineData ( Number . Three )]         [ InlineData ( Number . Four )]         [ InlineData ( Number . Five )]         [ InlineData ( Number . Six )]          public   void   TestNumber ( Number   number )         {              Assert . Equal ( true , ( Convert . ToInt32 ( number )  >   0 ));         } Though it worked fine for the given situation, I wanted a better way to do this so that that even if more values were added to the enum in the future, the test would still work without needing changes. Fortunate