Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What's the recommended method for setting up tests on code that requires a private key to make calls to external web services?

Full on mock objects or is there a way of distributing open source unit tests with a neat method to add in a tester's own private key at test time?

I'm using python but any general best practice

share|improve this question
Any test which requires call to such API is definitely not an unit test ;) – Matěj Zábský Jan 27 '13 at 15:54
up vote 5 down vote accepted

full on mock objects. The idea of unit tests is that you're testing the unit of code that you've written - not, in this case, the external webservice. So you mock that webservice so the mock object returns data you expect and you use that the see if your code works with that data.

Now later when you come to do integration tests, you'll need to have the webservice running and accessed (so you can test that the webservice returns the data you expect it to, given the inputs you provide) but you'll have a high confidence that your unit will work correctly when this happens, because you've already tested that part in isolation!

(of course, your mileage may vary depending on your codebase and how much effort would be required to unit test that you'd get anyway with the integration testing, but the above is the concept of unit testing)

share|improve this answer
Not to mention, your unit tests must be idempotent. If they are calling real, external services, you can't guarantee that. You can set up mocks to return the same things that you've seen the external service return in the past, but making external calls means that idempotency is not necessarily a guarantee. – syrion Jan 27 '13 at 15:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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