Creating our API Test Scenarios

Get Recent Tweets from our Home Timeline

Creating the SpecFlow Scenario

When creating our BDD scenarios, we want to keep them simple and write them in a declarative style, sticking simply to the business logic, rather than writing them in an imperative style.  A great article on this can be found at http://itsadeliverything.com/declarative-vs-imperative-gherkin-scenarios-for-cucumber.

  1. Open up the ‘BaseApiScenarios.feature’ file and add the following test scenario. We can also add a SpecFlow tag of @Api either above the ‘Scenario’ (to apply to the specific scenario) or above the ‘Feature’ (to apply to all scenarios within the feature), which will set the BaseUri and OAuth for us before we run our test scenario/s…

    You should notice that the ‘Given’, ‘When’ and ‘Then’ lines are highlighted. This is because they do not yet have any step definitions attached to them.

Creating the Undefined Step Definitions

  1. Right-click on any of the three scenario lines in the Feature file and select ‘Generate Step Definitions’
  2. Ensure all the steps are checked in the new window that appears
    • Ensure the Class name is correct (e.g. ‘BaseApiScenariosSteps’) and click ‘Copy Methods to Clipboard’
  3. Open up ‘BaseApiScenariosSteps.cs’ step definition class file and paste in your undefined step definitions. It should look similar to below once done…
  4. Rename the string parameters to something more suitable in each of the undefined step definition methods…

Creating the Test Methods

PostTweet() Method

In this method, we will make a POST request to the ‘/update.json’ resource with a message we have passed in from our ‘Given’ scenario line. We will then execute that request in order to get a response.

  1. Open up the ‘BaseApiTests.cs’ class file in the ‘Apis’ namespace and add the following method…

GetResponseOfResource() Method

In this method, we will again create a new RestRequest() and set its resource to the ‘apiResource’ string value we pass in from our ‘When’ scenario line.

  1. Open up the ‘BaseApiTests.cs’ class file again and add the following method…
GetResponse() Method

You will notice that we have some repeated lines of code between the ‘PostTweet()’ method and the ‘GetResponseOfResource()’ method. To try and follow DRY (Do not Repeat Yourself) principle, let’s create a private method for the Client to execute the Request, then we can simply call it in the methods we need to.

  1. Open up the ‘BaseApiTests.cs’ class file again and add the following private method…

    We can then edit the ‘PostTweet()’ and ‘GetResponseOfResource()’ methods to call the ‘GetResponse()’ private method…

    The whole thing should look similar to below…

Connecting the ‘Given’ and ‘When’ Step Definitions to the Test Methods

We will now map the undefined step definitions to the appropriate methods we have added to post a tweet and get the response of a resource.

  1. Open up the ‘BaseApiScenariosSteps.cs’ step definition class file and refactor the ‘Given’ step definition so it correctly posts a tweet with a string that we pass into the method…

    Also ensure that the correct using directive was added so that it can find where our ‘BaseApiTests.cs’ class file is…
  2. Open up the ‘BaseApiScenariosSteps.cs’ step definition class file again and refactor the ‘When’ step definition so it correctly gets a response from a specific resource that we pass into the method…

Models

Deserialization

In order to assert our tweet was posted successfully, we need to return a response from our ‘/home_timeline.json’ and assert that the value of the ‘text’ key is equal to the tweet that we posted. For us to be able to do this successfully, we need to use Deserialization and create a class and constructor for the API resource with properties for any keys in our response that we want to interact / do something with.

  1. Open up the ‘HomeTimeline.cs’ class file in the ‘Models’ nested namespace and edit it so the class is public, contains an empty constructor, and also contains a property for the ‘text’ key that we want to be able to interact with in our code. The whole thing should look similar to below…
DeserialiseResponse<T>() Method

We will now add a method that uses a generic and will deserialise the JSON of any ‘Models’ class that we pass into it.

  1. Open up the ‘BaseApiTests.cs’ class file again and add the following private method, which we will use later in our Assert method for our final ‘Then’ scenario line…

    Ensure the correct using directives have also been added for the use of ‘JsonDeserializer’…

Adding the Final Method for Asserting our Tweet was Posted

We will now create our test method to assert that the ‘text’ key in the JSON response correctly has a value of the tweet/message we posted. Our Twitter Home Timeline returns a List of most recent tweets.  Because we only care about the most recent tweet, it would be the one at index 0 that we want to assert.

  1. Open up the ‘BaseApiTests.cs’ class file again and add the following method…

    Ensure the correct using directive for the usage of NUnit asserts has been added too…

    The whole thing should look similar to below when done…

Connecting the Step Definition to the Assert Method

  1. Open up the ‘BaseApiScenariosSteps.cs’ step definition class file again, and refactor the ‘Then’ step definition so it correctly calls the Assert method we just made…

Go back to the Feature file and run the scenario. CONGRATULATIONS, IT SHOULD NOW PASS!  If you also login to Twitter and check your Home Timeline, you should see your tweet has been posted successfully 😀

 

 

 

Digiprove sealCopyright secured by Digiprove © 2018
Liked it? Take a second to support Thomas on Patreon!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.