Insights and outlooks on software development

S'true

Writing good tests

Wednesday, November 19, 2008 by Thomas L

Colin Jack has a couple of blog posts on testing using the BDD-ish context/specification type of tests. Check them out!

I often like to write my (NUnit) tests like so:

namespace BlogpostBehaviour
{
  [TestFixture]
  public class AfterSavingABlogPost
  {

    private BlogRepository _blogRepository;

    [SetUp]
    public void SetUp()
    {
      _blogRepository = new BlogRepository();
      Blog blog = new Blog()
      {
        Id = 12,
        Title = "My new post",
        Text = "Some text with a specific word"
      };
      _blogRepository.Save(blog);
    }

    [Test]
    public void ShouldBeAbleToSearchForBlogByContents()
    {
      Assert.AreEqual(blog.Id, _blogRepository.Search("specific")[0].Blog.Id);
    }

    //more assertions...
  }
}

This way I can easily use the normal toolchain with NUnit and such, and have new developers grasping the test setup, but still tricking the old-school unit test developers into thinking context/spec.

Technorati Tags: ,,,,

Filed under , having  

3 kommentarer:

Colin Jack said...

Yeah I definitely think NUNit is a reasonable option, I'd definitely recommending aliasing the attributes ("using Observation = ...TestAttribute") and a few extension methods to sweeten the deal though.

Olof Bjarnason said...

Cool.
SetUp = GivenWhen = Context
Test = Then = Should

Thomas L said...

Yup, and the nice thing (like I wrote) is that we can fool the world we are doing tdd, when we actually are doing bdd!