Skip to content

Commit 1aa15fd

Browse files
committed
expand out vcr.py section
1 parent 5570962 commit 1aa15fd

1 file changed

Lines changed: 53 additions & 8 deletions

File tree

_posts/2020-01-25-testing_external_api_calls.md

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,6 @@ have one of these each time we introduce a new option.
275275

276276

277277

278-
**TIP** I want to give a quick nod to [vcr.py](https://vcrpy.readthedocs.io/en/latest/)
279-
here. It's a very neat solution, and I've used it successfully, but it does
280-
have limitations. This really deserves a blog post of its own, but in brief,
281-
it can't simulate state-dependent interactions, and it can
282-
also be quite confusing to new team members. Still, give it a try!
283-
284-
285-
286278
## SUGGESTION: Build an Adapter (a wrapper for the external API)
287279

288280

@@ -425,6 +417,59 @@ You'll need to think about:
425417
* integration tests may be slow and flakey
426418

427419

420+
## OPTION: vcr.py
421+
422+
I want to give a quick nod to [vcr.py](https://vcrpy.readthedocs.io/en/latest/)
423+
at this point.
424+
425+
VCR is a very neat solution. It lets you run your tests against a real
426+
endpoint, and then it captures the outgoing and incoming requests, and
427+
serializes them to disk. Next time you run the tests, it intercepts your HTTP
428+
requests, compares them against the saved ones, and replays past responses.
429+
430+
The end result is that you have a way of running integration tests with
431+
realistic simulated responses, but without actually needing to talk to
432+
an external third party.
433+
434+
At any time you like, you can also trigger a test run against the real API,
435+
and it will update your saved response files. This gives you a way of
436+
checking whether things have changed on a periodic basis, and updating
437+
your recorded responses when they do.
438+
439+
As I say it's a very neat solution, and I've used it successfully, but it does
440+
have some drawbacks:
441+
442+
* Firstly the workflow can be quite confusing. While you're still evolving
443+
your integration, your code is going to change, and the canned responses too,
444+
and it can be hard to keep track of what's on disk, what's fake and what's not.
445+
One person can usually wrap their head around it, but it's a steep learning
446+
curve for other members of the team. That can be particularly painful if
447+
it's code that only gets changed infrequently, because it's long enough for
448+
everyone to forget.
449+
450+
* Secondly, `vcr.py` is tricky to configure when you have randomised data in
451+
your requests (eg unique ids). By default it looks for requests that are
452+
exactly the same as the ones it's recorded. You can configure "matchers" to
453+
selectively ignore certain fields when recognising requests, but that
454+
only deals with half the problem.
455+
456+
* If you send out a POST and follow up with a GET for the same ID, you might be
457+
able to configure a matcher to ignore the ID in the requests, but the
458+
responses will still contain the old IDs. That will break any logic on your
459+
own side that's doing any logic based on those IDs.
460+
461+
#### vcr.py tradeoffs
462+
463+
##### Pros:
464+
* gives you a way of isolating tests from external dependencies by replaying canned responses
465+
* can re-run against real API at any time
466+
* no changes to application code required
467+
468+
##### Cons:
469+
* can be tricky for team-members to understand
470+
* dealing with randomly-generated data is hard
471+
* challenging to simulate state-based workflows
472+
428473

429474

430475
## OPTION: Build your own fake for integration tests

0 commit comments

Comments
 (0)