Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Software testing: test remote API or just use a mock?

Let say your website need to call Twitter API to perform some tasks, you have several options:

  1. Only use mock
  2. Use mock in unit test, but use production api in integration test
  3. Only call production api, never use mock

Which one is the best approach if your services depends on the external api?

like image 635
Howard Avatar asked Oct 28 '25 14:10

Howard


1 Answers

I would step back and ask yourself what you are trying to test.

If you are trying to test your other code in isolation, use a mock (that's the purpose of a mock after all).

If you are trying to really test end-to-end, use the production API (or a live test API if they have one).

So my answer is pretty close to your #2 choice, with the caveat that you should consider whether you need to test an API from a third party. Sometimes it makes sense to do so or is necessary, sometimes it doesn't make sense (they are known to be reliable or it's very inconvenient to do so).

like image 114
Phil Sandler Avatar answered Oct 31 '25 13:10

Phil Sandler