Skip to main content

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.


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
  3. REALLY VERY EXCELLENT INFORMATION. I AM VERY GLAD TO SEE YOUR BLOG FOR THIS INFORMATION. THANKS FOR SHARING. KEEP UPDATING.

    NO.1 AQM Services | Application Quality Managment Services | Austere Technologies

    ReplyDelete
  4. wow...nice blog, very helpful information. Thanks for sharing.

    NO.1 API DEVELOPMENT SERVICES | MASSIL TECHNOLOGIES

    ReplyDelete

Post a Comment

Popular posts from this blog

Treating Test Code as Production Code

It's important when writing automated tests to remember that the code you write should be up to production standards, meaning any conventions that you have in place should be adhered to and that it should follow good design patterns. Too many people often say why does it have to be as good as production code, it's "Only" a test, so long as it passes then that's fine... To answer this we need to look at why we want our tests to be written in such a structured and efficient manner: - Maintainability - by making the test code structured and efficient, it becomes far easier to maintain and in doing so changes in the future can happen quickly as the test isn't linked to anything that it shouldn't be and it's easy to understand for a new set of eyes. - Durability - Again by making the tests structured they should be resistant to changes, if you change a variable name for instance then it shouldn't effect the unit test unless it absolutely has to....

Testers: Be more like a Super-Villain!

Who doesn't love a Super Hero? Talk to my son, and he'll tell you how much he loves them, talk to many adults and they'll say the same! Deep down, we all love to be the Super Hero, we all want to save the day! However, I want to talk about the flip side of Super Heroes, the Super Villains... I often play Imaginext with my son, and I (unfortunately?) am nearly always the Super Villain! Be it Lex Luthor, Joker, Two Face, Mr Freeze or The Riddler! These are all great characters and great Super Villains, but why would I want to write about Super Villains? A while ago where I worked, we had a few Super Heroes, people who would be able to come in and "fix" things that had broken and help deliver projects on time. We then shifted, we decided to do away with the Super Hero culture and try and prevent from being in that position in the first place, whilst we didn't go as far as wanting to hire Super Villains, it's definitely a story that has stuck with me and t...

Using BDD and gherkinising your Acceptance Tests

In my post Testing of Automated tests , I mention about a BDD framework which involves using BDD to drive your acceptance tests. BDD stands for Behaviour Driven Development.  One effective method of writing BDD tests are by using a format known as Gherkin language. These consist of Given, When, Thens. The main advantage of the gherkin language is that it's readable by the business, and in an ideal world forms part of the Conditions of Acceptance around a PBI. Also, using a Visual Studio plugin of SpecFlow , you can integrate your Gherkinised COAs into your solution with feature files, and then drive the automated tests, however, for this post I will focus solely on how to effectively gherkinise your acceptance tests. A Feature file consists of a feature outline, which details what the feature file is testing followed by Scenarios and examples (parameters).  The BDD scenarios are made up of a Given, When, Then... These are effectively an initial state (Given), an action (W...