WatiN Patterns #2: One Assertion and a Name to Match

One way to keep your WatiN tests maintainable is to keep them small and focused. WatiN Pattern #2, then, is a way to do just that.

Your tests should assert one thing. Just one. And the name of the test should describe whatever it is you’re asserting. Looking at our example from last time, we see that we’ve got the one assertion part right, but the name doesn’t match.

[TestMethod]
public void SearchPageTest()
{
using (IE ie = new IE())
{
ie.GoTo("http://www.google.com/");
ie.TextField(Find.ByName("q")).TypeText("Richard Lawrence");
ie.Button(Find.ByName("btnG")).Click();
Assert.IsTrue(ie.ContainsText("www.richardlawrence.info"));
}
}

In a nod to the BDD approach (more BDD to show up in future patterns), I like to name the test class for the thing we’re testing and use test names starting with “Should” that specify what the thing we’re testing ought to do. So, we could change our example to something like this:
// using statements, namespace declaration, etc.
// ...
[TestClass]
public class GoogleSearchPage
{

// ...

[TestMethod]
public void ShouldReturnSearchResultsSuccessfully()
{
using (IE ie = new IE())
{
ie.GoTo("http://www.google.com/");
ie.TextField(Find.ByName("q")).TypeText("Richard Lawrence");
ie.Button(Find.ByName("btnG")).Click();
Assert.IsTrue(ie.ContainsText("www.richardlawrence.info"));
}
}
}

Now we have one assertion, and our test name indicates that it’s asserting that the Google search page should return search results successfully. If that test fails, we know what’s gone wrong.

It’s not yet transparent that the assertion on line 17 does what the test name indicates, but we’ll deal with that in future patterns.

Related Articles

Responses

  1. Richard,

    Thanks for the inofrmation, are you still planning on adding more patterns.

    Regards,
    Eric

  2. @Eric – I have a backlog of other long-overdue WatiN pattern posts to write. Your comment is a much-needed reminder to get to it. Watch for new posts on the topic soon. (I recommend subscribing via RSS or email if you haven’t already so you don’t have to check back.) Thanks!