Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

Test First Development

Really, to be fair, I should have developed the tests for ASpell.NET first, but I wanted to see if it was even feasible. Since it seems to be a useful component, now is the time to write the tests while the project is still pretty young. What is nice about developing the tests first is that is forces you to create your API and think about your contract with the outside world in a detailed way. After all, you cannot write the tests for components if you do not know what the signatures of the public methods are or what properties are available. For ASpell.NET, I did not have to spend too much time in the design of the component because it pretty much mimics ASpell's design. This was done for two reasons. First, I wanted ASpell.NET to be familiar to anyone who has used ASpell. Second, I realized the amount of man-hours and development put into ASpell and decided that I probably could not come up with a much better design.

I will start with the following tests:

  • What if I pass in a null or empty string to spell check?

  • What if I use numerals or punctuation?

  • What if the program can't find a dependant dll or dictionary?

  • Can it support multiple cultures?

  • What if there's nothing to return?

To perform these tests, I will add a reference to NUnit into the build and use it to create the tests. Listing 9.1 shows the simplest test if a null is passed into ASpell.NET.

Example 9.1. NUnit Test

[TestFixture]
   public class AspellNullTest
   {
      private aspell.net.SpellChecker spellCheck;

      [SetUp]
   public void Init()
   {
   spellCheck = new aspell.net.SpellChecker();
   }
         [Test]
   [ExpectedException(typeof(aspell.net.ASpellNullException))]
   public void NullCheck()
   {
   foreach(CultureInfo ci in spellCheck.CultureInfos)
   {
   spellCheck.CultureInfo = ci;
   spellCheck.checkword(null);
   }
   }
   
   [Test]
   [ExpectedException(typeof(aspell.net.ASpellNullException))]
   public void NullSuggest()
   {
   foreach(CultureInfo ci in spellCheck.CultureInfos)
   {
   spellCheck.CultureInfo = ci;
   spellCheck.suggest(null);
   }
   }
   }
   

NUnit version 2.1.4 was used for these tests.

This test will take no time at all. The hardest test will be removing the dictionaries and dependant dlls. In designing the tests, we must create them in a way that will not require you to rewrite them for every culture.

  • + Share This
  • 🔖 Save To Your Account