First, a warning: today’s exercise is likely to generate tasks for you that will take longer than 20 minutes to fix. This doesn’t mean something bad has happened, or that you’ve failed. Just file a ticket to remind you to complete the work later.
Today’s exercise: run your test suite with your internet connection disabled.
Turns out, it’s fairly easy to accidentally rely on an external service during test runs (I’ve done it many times). This won’t just make your test suite slow, but brittle.
Chances are, you’ll see a few cryptic failures when you try this.
In general, I recommend reaching for a tool like Webmock to fix issues like these.
As a bonus, Webmock has a setting to disable external web requests during test runs. If you attempt to make a connection to the outside world, you’ll get an exception. This will prevent you from writing internet-hitting tests in the future.
Give it a try and let us know how it goes.
I am glad that all my tests passed with wifi off. I am already using WebMock. Its a great tool.
That’s an interesting one. I never thought of doing that. Turns out my tests pass with wifi off, but I will definitely try that on other projects
All green on the 3 code bases i tried, next task
I’m also using WebMock/VCR in all my Rails apps
No problem today
Used the time allocated to do research on how to mock API’s, never done it. This is definitely something that would be a nice addition to my projects but will take some time to figure out.
However, I assume people don’t mock all api calls because how would you verify the api hasn’t changed and is actually still working as it should?
Do you mock the majority of calls and only leave a few real api calls in your test suite?
We use VCR as well. No failures when running in airplane mode.
On Laravel, so no WebMock. Done this in the past and I know that we have outgoing network requests.
We try to mock them as much as possible, but sometimes (I’m looking at you Azure) it is realy hard to do.
Another interesting challenge! With wifi off, 4 tests failed.
I’m using webmocks with
WebMock.disable_net_connect!(allow_localhost: true, allow: 'chromedriver.storage.googleapis.com')
Whole test suite is passing with WiFi off.
We’re also using VCR, so it’s somewhat guaranteed that no external connection are made during testing… but, to my surprise, our feature specs which are run through selenium failed with a
net::ERR_INTERNET_DISCONNECTED… is it not designed to work in airplane mode?
All green tests! We’ve had tests going to the network in the past but they’ve been reworked. No new ones introduced since then apparently.
All tests green in my current project’s repositories. We do make this somewhat of an requirement to not call out to external services.
Spent the time today to work on stubbing out interactions with S3 to avoid having to setup it for tests. Quite a long way to go until we are free from external services.
No issues for me on a Laravel project. For anyone looking to up their testing game with Laravel, I’d highly recommend Adam Wathan’s Test Driven Laravel course. I learned a ton from it.
No tests, no problem… I am just kidding. I don’t have any external API calls or dependencies.
Like virtually all professional Rails apps I worked on, my current codebase uses webmock to disable external requests. But it doesn’t hurt to be paranoiac, so I ran the specs offline and everything was still green.
This one was easy since we are already using Webmock!
Living in world of Python.
I have used in past moto and I was amazed how much it can provide.
Regarding mocking requests - I did some experiments in past and it worked well but all my projects are testing against locally running service (which is subject of my tests) so I did not find a need to mock it on regular bases.
About a year ago I have experimented with https://mitmproxy.org/ which amazed me with how much it can do. Apart of allowing me to do “F12” on command line I was also able to mock server responses using it.