1

I am building an application using YouTube's API. This is my first time using a proper API, so I don't fully understand how it works (hence I am here).

Every time I come to code for the application, I am finding that my unit tests run into the limit pretty quickly. How do people normally handle situations like this? Is it a case of not unit testing? Or there something I can do instead to prevent this issue?

For anyone familiar with YouTube's API, based on this, I am only making requests to retrieve 1-2 videos per test, and I currently have around 10 tests. If I am correct, assuming that I am requesting 2 videos per call, I should be able to run the tests 10,000/20 = 500 times, no?

Jake Jackson
  • 1,055
  • 1
  • 12
  • 34
  • 1
    I'd recommend against running unit tests (and automated test suites for that matter) on the *real* API; that because each *real* endpoint call has a [quota cost](https://developers.google.com/youtube/v3/determine_quota_cost) attached. Instead run your test suite on a *mocked* API (you don't have to mock the entire set of API endpoints, but only those that you're actually using). – stvar Mar 07 '21 at 13:31
  • That's cool, thank you, Stvar. Yeah, I was just looking for advice on what you think is best, so in that case I will form some mock data. – Jake Jackson Mar 07 '21 at 13:36
  • Just wondering, in terms of what I wrote about how YT's API works, am I correct my thinking? It's OK if you don't know or I didn't give enough information, I won't lose any sleep. – Jake Jackson Mar 07 '21 at 13:37
  • Please read [this answer of mine](https://stackoverflow.com/a/66471485/8327971). – stvar Mar 07 '21 at 13:38
  • Thank you again, Stvar, it makes sense. Yeah, I think I will make some mock data/cache what I get sent and reuse that. I appreciate the info – Jake Jackson Mar 07 '21 at 13:45
  • For what concerns caching JSON data obtained from the API, please consider getting acquainted with the specifications of [DTOS](https://developers.google.com/youtube/terms/developer-policies). See [my comment](https://stackoverflow.com/questions/66516916/how-to-handle-api-call-rate-limit-when-unit-testing#comment117589084_66517144) below. – stvar Mar 07 '21 at 13:47

1 Answers1

3

I'd run the unit tests slightly differently. I'd run a job every hour or so to hit 4-5 most common scenarios or endpoints and save them to their respective files.

When your unit test runs, they don't make a call to the API for all the tests. Instead, they load the output from the file and process it. That way, you are still checking whether the code that processes API's response are still performing alright. You are just not getting API responses live. That's the way I'd do it to reduce hits to the API endpoints.

zedfoxus
  • 35,121
  • 5
  • 64
  • 63
  • Unfortunately, @zedfoxus, what you're proposing is very much against the specifications of the document that governs the usage of YouTube Data API: [DTOS](https://stackoverflow.com/a/66471485/8327971), section III.E.4, *Refreshing, Storing, and Displaying API Data*. – stvar Mar 07 '21 at 13:45
  • 1
    Thanks, @stvar. https://developers.google.com/youtube/terms/developer-policies are the policies I looked at based on your comment. OP has to decide for themselves what they infer and how to do unit testing. Based on my inference of those policies, I'd do unit testing like I answered. – zedfoxus Mar 07 '21 at 13:59