Iām looking for some general advice with an API framework I am setting up using C# and RestSharp as the client library. Iāve successfully set up an initial test, but Iām now looking at creating a series of assertions to ensure that certain values are returned in the response
My test looks as below:
[Test]
public void ShouldHaveDataAttributes()
{
var restClient = new RestClient("http://ergast.com/api/f1");
var restRequest = new RestRequest("2016/circuits.json", Method.GET);
var restResponse = restClient.Execute(restRequest);
dynamic jsonResponse = JsonConvert.DeserializeObject(restResponse.Content);
dynamic jsonObject = jsonResponse.MRData.CircuitTable;
int circuitId = (int)jsonObject.season;
System.Console.WriteLine(jsonObject);
}
The jsonObject returns the below data:
{ āseasonā: ā2016ā, āCircuitsā: [ { ācircuitIdā: āalbert_parkā, āurlā: āAlbert Park Circuit - Wikipediaā, ācircuitNameā: āAlbert Park Grand Prix Circuitā, āLocationā: { ālatā: ā-37.8497ā, ālongā: ā144.968ā, ālocalityā: āMelbourneā, ācountryā: āAustraliaā } }] }
My question is how would I specifically check that, for example the correct ācircuitIdā is returned or that the ācircuitNameā value is āAlbert Parkā? Iām keen to carry on utilising RestSharp for my framework so was hoping there was an extension to allow me to do that
If you add a reference in to your test project for āNewtonsoft.Json.Linq;ā then the below snippet Iāve tested and would allow you to extract out values from your response.
var returnedJson =
"{ \"season\": \"2016\", \r\n\"Circuits\": [ { \"circuitId\": \"albert_park\", \"url\": \"http://en.wikipedia.org/wiki/Melbourne_Grand_Prix_Circuit\", \"circuitName\": \"Albert Park Grand Prix Circuit\", \"Location\": { \"lat\": \"-37.8497\", \"long\": \"144.968\", \"locality\": \"Melbourne\", \"country\": \"Australia\" } }] }"; // I've escaped the response but you would just set the returnedJson to the response you get back
// parse the json response so that we can get at the key/value pairs
dynamic api = JObject.Parse(returnedJson);
// grab the values and do your assertions :-)
var season = api.season;
var circuits = api.Circuits;
var circuitId = api.Circuits[0].circuitId;
var circuitName = api.Circuits[0].circuitName;
The slight issue I have is that the returnedJson variable is pretty big as there are 21 different circuits being returned in the response. Would there be an alternative way that I could return the data?
just managed to figure it out as you replied to me, thanks Viv! In reality, Iāll only be asserting against a small amount of data coming back so have come up with a solution.
In general, for API testing Iām struggling to figure out what assertions need to be done? Are there certain assertions that you should run every time(e.g. that the appropriate status code comes back)? Other assertions, I assume, would be based on the data that has been consumed and the values we are expecting to be returned?
Iāve come from a background of UI automation and whilst I am a firm believer in testing at the API layer where we can, Iām finding it a bit of a mindshift to get used to
As a general bit of advice, when coming up with an API framework I am looking at writing this in C# as it makes sense to utilise the same programming language that the company Iām working at use. Something Iām trying to work out is why weād write an API test framework in code(be it using c# and RestSharp or Java and RestAssured), as opposed to setting a framework up in Postman or SoapUI. What would be the proās and conās of each?
Hello Andrew,
in our project, we use RestSharp for API testing. Our approach is to specify the type of returned response for the service, it allows to get the class or collection of known type instead of dynamic.
Then we just generate the expected outcome object in test and compare it to the object we received in response. For objects comparison, we use CompareNETObjects package by KellermanSoftware.
So the test looks like:
[Test]
public void Segment_Get_AnyActiveSegment()
{
MarketingSegment segment = // here we request test data from SQL DB
Ensure.Found(segment, Is.Not.Null, "Couldn't find a segment for the test.");
// Inside the following method via some wrapper we call restClient.Execute<MarketingSegmentDto>(JsonRequest)
IRestResponse<MarketingSegmentDto> getRequestResult = Steps.GetMarketingSegmentRequestStep.SendRequest(segment.Id);
ServiceResponseValidation.ValidateOkResponse(getRequestResult);
Steps.EntityModificationValidator.ValidateEntityNotChanged(expected: new MarketingSegmentDto(segment), actual: getRequestResult.Data);
}