Day 11 - Run your tests with your wifi off

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.

We actually resolved this a while ago, when our internet went down for a couple of hours - maybe it was partly because of a previous CQC. It looks like we’ve kept up with this, as no tests failed. Huzzah!

I thought this was going to be a no-op for me as we’re usually pretty diligent about making sure that network requests are mocked, etc in tests.

As it turned out though, our settings code for one of our projects uses the boto3 library to reach out to AWS to read a configuration value (which isn’t needed for the tests, as it defaults to an environment variable setting if the value isn’t set in AWS). As a result when wifi is disabled, an exception would get thrown as the DNS entry for AWS wouldn’t resolve causing all tests to fail. I added an exception handler for that exception and now all works as expected.

Nice little win!

Thanks for pointing me towards Webmock to run the tests with blocking all outgoing requests! Was able to clean up our controllers folder with 3 stubbed api calls, but wasn’t able to attack our feature specs. Definitely going to bring this up in our retrospective next week though!

Looks like all of our tests pass without wifi!

This was a no-op for us. I run this test suite on airplanes alllllll the time while working. I’d be dead in the water if it didn’t work with internet.

Does anyone recommend a library for Python to do request capture? I’ve seen things like the VCR module in Ruby and Javascript but haven’t found any good ones in Python.

A bit late to the party but today I came across a handy macOS tool called “Network Link Conditioner” that immediately reminded me of this challenge (and might be useful in some other scenarios too).

It’s part of the Additional Tools for Xcode bundle (free or paid Apple Developer login required) and it gives you a simple Preference Pane that let’s you wreak havoc with your Internet connection for the purpose of simulating slow or poor quality network connections: