Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing third party API's

Tags:

unit-testing

I would like to test a third party API such as forecast.io, but I am not quite sure how to accomplish what I want to achieve.

I have read all over the internet that I should use mock objects. However, the concept of mock objects is not what I need, as I do not want to test my implementation of the parsing rather than the network call itself.

I want to test for example if the URL is still working, if my API key is still working, if the request is still in the expected format so GSON does not crash or other things directly related to the network call itself.

Is there any good way to do this?

Many thanks

TLDR; I don't need mock objects!

like image 248
ph1lb4 Avatar asked Oct 30 '22 01:10

ph1lb4


1 Answers

I am going to try to answer this question as generally as it was asked. I understand OP wants to avoid testing each type of response, but if you are relying on this data for continuity of users and/or revenue, you may consider creating an api caller so you can look at each part of the api response(s) as well as test the URL, api key, etc. I would use an OO language, but I'm sure there are other ways.

In general:

  1. create a process/library/software that can call the api
  2. serialize/store the data you are expecting (GSON in OP's case)
  3. unit test it with xUnit or NUnit
  4. automate it to run every x time period and generate an email with success/change/fail message

This is no small project if you are counting on it - need to tighten all the screws and bolts. Each step deserves its own question (and may already have one).

[I can add some sample code in C# here if that will help]

How to automate this to run and email you is a completely different question, but hopefully this gives you the idea of how an object oriented library can help you test every piece of data your own software is planning to use. Not every api host will let you know in a timely manner when/if changes are taking place and you may even know before they do if something breaks.

like image 146
smurtagh Avatar answered Nov 15 '22 12:11

smurtagh