I currently use the Google trends API and I implemented a class to integrate with its services.

As you probably know, Google trends tracks statistics which will change over time.

So, when I write a test now, after some time, it is very likely that my test will break because the statistics will change.

How can I write I test that will keep working over time and keep the quality of the class?

  • 2
    Possible duplicate of Is it actually worth unit-testing an API client? – James Fenwick Dec 19 '18 at 19:58
  • Ask yourself, what do I need to test? Are you testing whether the API calls complete successfully, whether the results contain expected time range and categories? Don't evaluate results that you don't need to test. – joshp Dec 19 '18 at 20:19

First, decide what you need to test.

  • Are you testing to find breakage in your code after changes?

  • Are you testing to catch unexpected changes to the Google API?

  • Are you testing accuracy of the Google's Trends Service itself? (probably not a good idea)

Then write the tests to evaluate only things you need to test and ignore everything else.

  1. If you only need to test whether the API calls complete successfully and do not return error responses, then the tests should just look for success status and ignore response content.

  2. If you need to test whether the API responses include expected categories of data then have the tests check whether these categories are present, but ignore the values.

  3. You may want to test whether API calls are successful and whether your code can still deserialize the results. This should be possible without evaluating the specific results.

  4. If you actually want to verify the values returned by Google Trends, stop and ask whether this is even possible. Not only is it likely impossible to test the accuracy of their API or their data in this case, but it's often a pretty bad use of time to try to test the accuracy of an external API you have chosen to use.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.