Sunday, June 26, 2011

Quick Example using TestDrivenDesign

using TestDrivenDesign;

[TestClass]
public class QuickExampleTest : TestBase<QuickExample>
{
    [TestMethod]
    public void ShouldSayHelloMultipleTimes()
    {
        // Arrange
        Subject.Count = 3;      
        // TestBase<T> provides us with a default constructed
        // QuickExample (think: "Test Subject")
        var path = TextPath();
        // Gives us a path in the test run directory like: 
        // QuickExampleTest.ShouldSayHelloMultipleTimes.txt

        // Act
        Subject.SayHello(path);

        // Assert
        TestContext.AddResultFile(path);
        // TestBase provides the TestContext property. 
        // AddResultFile adds a hyperlink to our file on the
        // test result page
        TextFileAssert.Contains(path, "hello hello hello");
        // TextFileAssert helps test writing text files.
        // Yes, there is a BinaryFileAssert too!
    }
}

Let's take a moment to review the code that you aren't writing:
  • No TestContext boilerplate
  • No initialization of your test subject. Every test method gets a newly constructed subject, which you are free to overwrite
  • No coming up with your own unique file name and combining it with the deployment directory
  • No reading of a text file into a string or collection before you can do an assert
This was also posted on the TestDrivenDesign wiki.  Get the code at https://github.com/jfoshee/TestDrivenDesign

No comments:

Post a Comment