Monday, 25 March 2013

Testing of Automated Tests?

I've often mentioned how important automated tests are throughout my blog. However, automated tests can also give false positives... This is where paired programming and testing of the automated tests come in to play. The example I am going to use employs the Page Object model with BDD, more of which can be read about here and here, until I get round to creating a new blog post on it...


Scenario Outline: Search for generic term
Given I am on the homepage
And I have entered <searchterm> into the search box
When I click search
Then search results for <searchterm> are displayed

Let me explain anyway, if we have an assertion in the step definition for the Then step (written in C# using NUnit) that does the following:






[Then(@"search results for (.*) are displayed")]
        public void SearchResultsForAreDisplayed(string searchTerm)
        {
            Assert.IsTrue(_searchPage.SearchTitleTextDisplayed(searchTerm));
        }

(_searchPage.SearchTitleTextDisplayed(searchTerm) is a method that lives in the page object for Search Page, that returns true if the SearchTitleText is displayed, the same can be assumed for the methods mentioned below)

It's asserting that the search results header is displayed on the page, signifying one would suspect that the search was successful.

However, upon closer inspection that search results header is also displayed when there are no search results:



So now, we have a test that looking at the Step Definition is asserting that the search results header is displayed, which it is, and passing the test as a success for having search results displayed. The problem, which I'm sure you've already guessed, is that the search results header is also displayed when there are no search results, so this means the test will pass regardless if search results are displayed or not.

A much better approach, would be to assert that products are displayed and that there are refinements as well as the search results header. So we now have:

        [Then(@"search results for (.*) are displayed")]
        public void SearchResultsForAreDisplayed(string searchTerm)
        {
            Assert.IsTrue(_searchPage.SearchTitleTextDisplayed(searchTerm)), "Search Title is not displayed";
            Assert.IsTrue(_searchPage.SearchResultsDisplayed()), "No Search Results are displayed";
            Assert.IsTrue(_searchPage.RefinementsDisplayed()), "There are no refinements displayed";
        }

These new assertions, mean that if one of them fail, the test fails, and you get information on the assertion that failed.

 You could argue the case for having one assertion, that correlated all of the above into one simple assertion at the step level:

        [Then(@"search results for (.*) are displayed")]
        public void SearchResultsForAreDisplayed(string searchTerm)
        {
            Assert.IsTrue(_searchPage.SearchPageResultsDisplayed(searchTerm)), "Search Results Page is not displayed correctly";
        }


However, out of the 2, I prefer the original method, with 3 different assertions, purely for 2 reasons:

  • The step is easy to read and understand what it is doing.
  • If one of the assertions fail, then the message displayed is more informative for debugging purposes than the second method (you could return the message that fails to the assertion, but it's still not as clear in my opinion)
There is the positive of the step definition getting larger as you want to assert more and more things on the step (with the earlier method), I would however say that ideally there shouldn't be more than 1 assertions per a step (although i am sure there are exceptions to the rule)....

So, based on the above (1 assertion per step and to keep sizes of steps to a minimum), we can improve this even further, you could have multiple steps, with one assertion in each, this is much cleaner, and probably my favoured method. 

        [Then(@"search results for (.*) are displayed")]
        public void SearchResultsForAreDisplayed(string searchTerm)
        {
            Assert.IsTrue(_searchPage.SearchResultsDisplayed()), "No Search Results are displayed";

        }
        [Then(@"search title text of (.*) are displayed")]
        public void SearchTitleTextOfDisplayed(string searchTerm)
        {
            Assert.IsTrue(_searchPage.SearchTitleTextDisplayed(searchTerm)), "Search Title is not displayed";

        }      
        [Then(@"search refinements are displayed")]
        public void SearchRefinementsAreDisplayed()
        {
            Assert.IsTrue(_searchPage.RefinementsDisplayed()), "There are no refinements displayed";

        }


This gives you the benefit of individual error messages should one fail, and at the same time they will fail the test if one of them fails and they keep the step definition down to a minimal size. This results in a new Scenario Outline of the following:

Scenario Outline: Search for generic term
Given I am on the homepage
And I have entered <searchterm> into the search box
When I click search
Then search results for <searchterm> are displayed
And search title text of <searchterm> is displayed
And search refinements are displayed

It's a lot more informative for the tester who is running the test to see exactly what it is doing, even without looking at the code, they can see instantly what checks are being performed.

We could add more checks, but for the sake of this exercise, it's important to grasp what the automated test is checking for, before it is being written. It's also important to structure the test with what you want to test in mind

This example highlights the importance of the tester either writing the steps themselves or pairing with a developer as they create the tests, as a developer may feel that the first example is enough, but obviously, as QA we know we have to test the automated tests themselves, to ensure that the tests don't give false positives. 

If I'm honest, i prefer the pairing with the developer approach, as then knowledge is shared, and the QA and Dev learn a little bit more about the others skill set and it helps build team relationships.


3 comments:

  1. can you share the code snippet of this example? I am new to Gherkin and specflow very keen to learn this, but having difficulties with the actual implementation using specflow, I have tried to search on the internet but none of them are not complete.

    Thanks,
    Amar

    ReplyDelete
    Replies
    1. Sure thing. It's here: https://github.com/gwaterhouse85/AsosSpecifications

      Delete
  2. I am also strongly motivated to learn the language but getting some difficulties in coding. I'm searching for those problems over Internet but coulds't find the solution. Maybe you can help me out.

    ReplyDelete