ropensci-books / http-testing

HTTP testing for R
https://books.ropensci.org/http-testing/
Creative Commons Zero v1.0 Universal
52 stars 17 forks source link

Workflow for testing real interactions using httptest #62

Closed mfherman closed 3 years ago

mfherman commented 3 years ago

Hi and thank you so much for your work on this book. I'm just digging in and it's already been a great resource. I'm interested in exploring the workflow for testing against a live API when you have already written tests that work via mocking.

In Section 7.3 you discuss included a folder of "real tests" that hit the API directly and don't use the mocked responses. I'm wondering if you have any thoughts on how best to implement this when there are a large number of mocked tests. Would you advocate for duplicating every single mocked test as a real test as well? My concern here is that it would require a lot of maintenance to ensure that for every new or modified mocked test you also write or change a real test as well.

sckott commented 3 years ago

Thanks for your question!

If you are using vcr it supports environment variables to make this use case easier - So you can have all tests that do HTTP interactions use vcr (so no real HTTP requests after the first recordings) - then if you want to run tests with real HTTP requests set the appropriate environment variable. In my R pkg development, I use Makefile's, so a make command to run tests is

make test
# which is just Rscript -e "devtools::test()"

Then if I want to run all tests with real HTTP interactions I can simply do

VCR_TURN_OFF=true make test

And you can do equivalent of that inside of R, or set the env var on your CI system, etc.

@maelle Maybe you can comment on how it would work with the other pkgs

mfherman commented 3 years ago

Thanks for the reply! This seems super easy with vcr :)

I guess I am thinking about this question in the context of httptest as for my usecase, it will be easier to have mock API responses that I can edit and create by hand.

maelle commented 3 years ago

This is an excellent topic and I think it might warrant its own chapter, I'll probably make a PR later.

Here are some notes, also mentioning vcr and webfakes to keep it exhaustive.

maelle commented 3 years ago

(also, thanks for reading! :pray: )

mfherman commented 3 years ago

Thank you for these notes - so helpful!

One of my winter goals is to implement a test suite into tidycensus, so I'm certain I will be referring to the book through the whole process and will share any additional questions or suggestions as I work through it.

maelle commented 3 years ago

Fantastic, your feedback including questions will be most useful!