4 ms·
I prefer to mock below the level of the 3rd party client library. In rubyland we use VCR to stub out the lower level network libraries to record the requests/re
by jacobsenscott 4y ago
I prefer to mock below the level of the 3rd party client library. In rubyland we use VCR to stub out the lower level network libraries to record the requests/responses. Then during the test runs your network libraries don't make real connections to the APIs - they just play back the recorded responses.
Python has ports of VCR (betamax and VCR.py).
The benefits of this are numerous. You don't need to manually build and verify mocks. You don't need to build a facade for every api client (the api client is already a facade). You exercise the as much of the 3rd party library code as possible on each test - this is very helpful during library upgrades. You can verify your recordings by switching your test to "live" mode every now and then (just configure your CI env do that every day or every week).
- hynek 4y ago> You don't need to build a facade for every api client (the api client is already a facade). I feel like this is covered by > Every rule and principle can be broken once you’ve fully understood its purpose. For example if an object already does have an idiomatic API, it’s probably not worth wrapping in an identical façade, just so it belongs to you. ? I personally prefer pytest-httpserver that is a bit more low-level, but I still prefer idiomatic calls over sprinkled http requests on business code.
- jacobsenscott 4y agoYes - it is covered by that. I do build facades for apis that don't have good client libraries, but for things like s3, or stripe - with solid libraries I don't. Even when I build those facades I don't mock them out - I just rely on VCR. I've never used test server like pytest-httpserver. I don't think that would buy much vs using stubbing your low level network libraries like VCR does. The killer feature of VCR is that it records the real network calls on first usage. If pytest-httpserver can do that in a proxy mode it seems like a good way to go though.