Automated Tester

.science

This content shows Simple View

February 2016

API Testing with Specflow

I’m a big fan of Specflow/ Cucumber, it’s great for creating tests to run against a front end with webdriver but what if we want to keep that same plain English BDD approach and apply it against an API for automated API Testing? It’s certainly possible and this article will cover the basics to get you up and running.

I am going to assume you have a framework set up and in place, you can even use a webdriver based solution as described previously, although you may wish to run it with a headless driver.

I’m also going to add a SOAP endpoint to my solution and write a test in Gherkin, this endpoint is the publicly available Global Weather SOAP endpoint available here:

http://www.webservicex.com/globalweather.asmx?wsdl

This WSDL generates 2 service operations, GetCitiesByCountry (to get a list of all the cities for any country) and GetWeather (to get weather for any city/country combination).

Let’s add the WSDL somewhere we can use it in Visual Studio. If you right click your project and go to Add > New > Service Reference, you will be presented with a dialogue in which you can paste in the WSDL, click Go and give it an appropriate name.

Add Service Reference for API Testing

Click OK, you should see a new Service References folder in your solution. Lets begin formulating a test for our API Testing suite.

@API
Scenario Outline: Get Cities
Given I query the API
Then I can retrieve <City> from <Country>

Examples:
| City        | Country        |
| Reims       | France         |
| London      | United Kingdom |
| Agra        | India          |
| Porto Santo | Portugal       |
| Cancun      | Mexico         |
| Pretoria    | South Africa   |
| Melbourne   | Australia      |

We’re going to need a helper class which I will call ‘GeoHelper’ where we can reference the endpoint and then make some methods we can call from the step definition. Below I am passing a string value to the ‘GetCitiesByCountry’ extension on the endpoint and returning the result. Then I have created another helper method which takes that result and asserts the city is present in the response.

public class GeoHelper
{
     private readonly GlobalWeatherSoap globalWeather = new GlobalWeatherSoapClient();

     public string GetCitiesByCountry(string country)
     {
          var x = globalWeather.GetCitiesByCountry(country);
          return x;
     }

     public void CheckCityNameIsPresent(string countryName, string cityName)
     {
          Thread.Sleep(200);
          Assert.IsTrue(GetCitiesByCountry(countryName).Contains(cityName));
     }
}

So our step definition calls this like so:

private readonly GeoHelper geo = new GeoHelper();

[Then(@"I can retrieve (.*) from (.*)")]
public void ThenICanRetrieveFrom(string cityName, string countryName)
{
     geo.CheckCityNameIsPresent(countryName, cityName);
}

And that in brief is how we can leverage the use of Specflow for API Testing.




top