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

  • 2
    Any test which requires call to such API is definitely not an unit test ;) Jan 27, 2013 at 15:54

1 Answer 1


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)

  • 1
    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.
    – asthasr
    Jan 27, 2013 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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