nealrichardson / httptest

A Test Environment for HTTP Requests in R
https://enpiar.com/r/httptest/
Other
80 stars 10 forks source link

Feature: Bypass mock #8

Closed mwaldstein closed 7 years ago

mwaldstein commented 7 years ago

It would be useful to be able to run the same code without the mock response instead making the actual call - either for recording captures or due to concerns about API changes, but also running with the mock for fast testing

Currently, this can be accomplished by writing the tests separately and then sourcing withing different contexts - with and without the mock, or keeping duplicate code for online and offline versions of the same tests

I'd propose a global option - along the lines of "httptest.bypass.mock" which is checked at the start of the context and prevents overridding GET, etc.

I can write this feature, but wanted to get feedback before pursuing it.

nealrichardson commented 7 years ago

That's an interesting idea, but I don't think we need an option to control that. You can just as easily set with_mock_API to another function in your test file (or in helper.R) to achieve it. So

with_mock_API({
    a <- GET("https://httpbin.org/get")
    print(a)
})

looks for the mock file, but

with_mock_API <- force
with_mock_API({
    a <- GET("https://httpbin.org/get")
    print(a)
})

just evaluates the code with no mocking and makes the request, and

with_mock_API <- capture_requests
with_mock_API({
    a <- GET("https://httpbin.org/get")
    print(a)
})

would record the request as a mock file. So instead of setting an option, you'd add/uncomment one of those lines. Does that meet your needs? If so, I can document this. I do see the value in being able to do this without copy/pasting your code around.

mwaldstein commented 7 years ago

I think what you suggest is a good approach - there is one place I could still use some feedback.

For my packages, I'd like to put a few options into the makefile -

Names a work in progress, but you get the idea.

Environment variables would make this pretty easy, just adding the following in my tests, following your suggesion -

if (Sys.getenv("MOCK_BYPASS") == "true") {
  with_mock_api <- force 
} else if (Sys.getenv("MOCK_BYPASS") == "capture") {
  with_mock_api <- capture_requests
}

If there were only one test file, there would be no problem, but as I get to 10+ test files needing the wrapping, that's a lot of boilerplate. Have any thoughts/suggestions of handling this case?

nealrichardson commented 7 years ago

testthat always runs helper.R before test files, so if you put your env var checking there, it should apply globally. Does that work for you?

nealrichardson commented 7 years ago

I incorporated this discussion into the FAQ on README.md: https://github.com/nealrichardson/httptest/commit/72f4a6c640d2c1ce6707befd2ebd58c5f5987a21

mwaldstein commented 7 years ago

Using helpers seems to be the way to go thanks!