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
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