mercredi 22 avril 2015

Structuring TextFixtures in nUnit

Upon unit testing of some class, there is a need to test same methods for different combination of data in a class. This can be achieved by writing several TextFixtures and repeating same test methods for each one, as below.

 [TextFixture]
 public class WhenBuildingLongFoo
 {
     private Foo foo;

     [Setup]
     public void Creating()
     {
          foo = new Foo(){Height = new FooHeight(200)};
     }

     [Test]
     public void ReturnsValidHeight()
     {
     Assert.AreEqual(200, foo.Height);
     }
  }

  [TextFixture]
  public class WhenBuildingShortFoo
  {
  private Foo foo;
  [SetUp]
  public void Creating()
  {
       foo = new Foo(){Height=newFooHeight(2);
   }
   [Test]
   public void ReturnsValidHeight()
   {
        Assert.AreEqual(2, foo.Height);
    }
}

  1. What is the best way to structure such fixtures, so that method ReturnsValidHeight is written only once?

I came up with creating a public static class Utilities in a common namespace, which has this method; but could you please advise something better?

Something like create an abstract class with such method and then derive fixtures from it?

  1. How to reuse setup among fixtures, just varying values?

Aucun commentaire:

Enregistrer un commentaire