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’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:
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.
Click OK, you should see a new Service References folder in your solution. Lets begin formulating a test for our API Testing suite.
Scenario Outline: Get Cities
Given I query the API
Then I can retrieve <City> from <Country>
| 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.
private readonly GlobalWeatherSoap globalWeather = new GlobalWeatherSoapClient();
public string GetCitiesByCountry(string country)
var x = globalWeather.GetCitiesByCountry(country);
public void CheckCityNameIsPresent(string countryName, string cityName)
So our step definition calls this like so:
[Then(@"I can retrieve (.*) from (.*)")]
public void ThenICanRetrieveFrom(string cityName, string countryName)
And that in brief is how we can leverage the use of Specflow for API Testing.