title: Organizing unit tests : classes of classes or behavioural? Where to start?
body: <blockquote>
    <p><strong>Summary</strong></p>
  </blockquote>
  
  <p>I have written a <code>RandomValues</code> class which provide multiple methods to generate random values per value type. This static class shall be used later on as a tool for unit testing other project classes and whatsoever.</p>
  
  <p>I did unit test each of the methods in a TDD manner.</p>
  
  <p>Now, I see there could be some refactor to promote code reuse, which conducts me to ask this questions after having searched the Internet different kind of results. Among them, one keeps my attention.</p>
  
  <p><a href="http://haacked.com/archive/2012/01/02/structuring-unit-tests.aspx/" rel="nofollow"><code>Structuring Unit Tests</code></a></p>
  
  <p>Aside, I really did like the idea of structuring the unit tests in a behavioural manner such as this: </p>
  
  <p><a href="http://behavioral.codeplex.com/" rel="nofollow"><code>Behavioral</code></a></p>
  
  <p>Now, I wonder how to apply this to my case.</p>
  
  <blockquote>
    <p><strong><code>RandomValuesTests</code></strong></p>
  </blockquote>
  
  <pre><code>[TestFixture]
  public class RandomValuesTest {
      [Test]
      public void RandomInt_ReturnsRandomIntegerValues() {
          int unexpected = RandomValues.RandomInt();
          int actual = RandomValues.RandomInt();
          Assert.AreNotEqual(unexpected, actual);
      } 
  
      [Test]
      public void RandomInt_ReturnsInRangeIntegerValues() {
          int minValue = 12;
          int maxValue = 20;
          int actual = RandomValues.RandomInt(minValue, maxValue);
          Assert.IsTrue(minValue &lt;= actual &amp;&amp; actual &lt;= maxValue);
      }
  
      [Test]
      public void RandomDateTime_ReturnsRandomDateTimeValues() {
          DateTime unexpected = RandomValues.RandomDateTime();
          DateTime actual = RandomValues.RandomDateTime();
          Assert.AreNotEqual(unexpected, actual);
      }
  
      [Test]
      public void RandomDateTime_ReturnsInRangeIntegerValues() {
          DateTime minValue = new DateTime(2000, 1, 1);
          DateTime maxValue = new DateTime(2000, 12, 31);
          DateTime actual = RandomValues.RandomDateTime(minValue, maxValue);
          Assert.IsTrue(minValue &lt;= actual &amp;&amp; actual &lt;= maxValue);
      }
  
      [Test]
      public void RandomEnum_ReturnsRandomEnumValues() {
          RandomValuesEnum unexpected = RandomValues.RandomEnum&lt;RandomValuesEnum&gt;();
          RandomValuesEnum actual = RandomValues.RandomEnum&lt;RandomValuesEnum&gt;();
          Assert.AreNotEqual(unexpected, actual);
      }
  }
  </code></pre>
  
  <blockquote>
    <p><strong><code>RandomValues</code></strong></p>
  </blockquote>
  
  <pre><code>public static class RandomValues {
      public static int RandomInt() { return RandomInt(int.MinValue, int.MAxValue); }
      public static int RandomInt(int minValue, int maxValue) {
          return generator.Next(minValue, maxValue);
      }
  
      // And the other Random[Type] here...
  
      private static Random generator = new Random();
  }
  </code></pre>
  
  <blockquote>
    <p><strong>Further thoughts</strong></p>
  </blockquote>
  
  <p>I have thought of organizing my tests using a base class which would provide two abstract methods: <code>ReturnsRandomValues</code> and <code>ReturnsInRangeRandomValues</code>, and make a test class per type overriding these two methods as follows.</p>
  
  <blockquote>
    <p><strong><code>RandomValuesTestBase&lt;T&gt;</code></strong></p>
  </blockquote>
  
  <pre><code>public abstract class RandomValuesTestBase&lt;T&gt; where T : IComparable&lt;T&gt; {
      public abstract void ReturnsRandomValues();
      public abstract void ReturnsInRangeRandomValues(T minValue, T maxValue);
  }
  
  [TestFixture]
  public class RandomInt : RandomValuesTestBase&lt;int&gt; {
      [Test]
      public override void ReturnsRandomValues() {
          // Use the same code as shown above for RandomInt_Returns...
      }
  
      [TestCase(10, 20)]
      public override void ReturnsInRangeRandomValues(int minValue, int maxValue) {
          // Use the same code as shown above for RandomInt_ReturnsInRange...
      }       
  </code></pre>
  
  <p>}</p>
  
  <p>But then, I have to create a test class per type: <code>bool</code>, <code>byte</code>, <code>char</code>...</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/ya5y69ds.aspx" rel="nofollow"><code>Built-In Types Table (C# Reference)</code></a></p>
  
  <p>This doesn't sound good at all. </p>
  
  <p>I'm now trying to figure out how I could start from the two references mentioned in the summary, and make a viable organization out of it.</p>
  
  <p>Or else, I keep the old plain way of a single test file per class for this one...</p>