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

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...

Start with the End in Mind - My first presentation at a tech meetup!

I was at a football coaching session the other night, and some other coaches put on a training session for us, so that we could learn and critique it. This is not an easy thing to do, to put something on for your peers and open yourself up to criticism is a difficult thing to do. One of the comments from the president of the club was that in order to develop yourself you need to push yourself and step outside of your comfort zone which it was evident that these coaches were doing. I took this to heart in many ways, a few weeks ago I signed up to do a presentation at a meetup that was only a couple of meetups old, The QE Roundabout . I was in contact with Zoe Canning (the event organiser) and I knew it was something I wanted to do, but it's like anything, saying you want to do something and then putting yourself in a position to do it are sometimes two very different things. Anyway, I volunteered to do one, the theme was Automation & Architecture, and we were free to ta...

Delusions of Testing

So I've got in touch with my old QA friend, Richard Lee and we spoke about guest blogging on each others blogs... Richard is an IT Professional for a FinTech based company in London. His activities vary from Release Manager, Build Manager, Database Administrator. Working in a Microsoft workshop, his expertise lies in MSBuild/Workflow/Powershell/SSAS/SSIS/SSRS/SQL, basically whatever isn’t anyone elses’ problem is Richard's problem! When not solving other peoples problems he can be found blogging at redphoenix.me , and jogging to and from home, where he lives with his heavily pregnant wife. Hello, my name is Richard, and I am a former tester. Like most people and their careers, I fell into testing; I first got into testing about 6 years ago, after I had graduated. I went to a university where the attitude was that you should try to get on a graduate scheme with one of the big companies. If you weren’t interested in that, well, good luck with getting any support from ...