Creating Comprehensive Test Coverage

Testing Elixir — by Andrea Leopardi, Jeffrey Matthias (15 / 80)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Organizing Your Tests | TOC | Testing Pure Functions 👉

We’ve covered some of the very basics of testing and delved into a bit of test theory. In order to provide solid coverage of a file, we’ll need to build on that knowledge by pulling in some features of Elixir itself, as well as some optional features of ExUnit’s assertions. These skills will help you write maximum test coverage with minimal code and without trade-offs.

Using a Setup Block

Let’s jump back to where we left our Soggy Waffle test, SoggyWaffle.WeatherAPI.ResponseParserTest. We’ll make a modification to our existing test before adding some more tests to our response parser. The data we used in our test was hand-coded. That’s a great way to start, but we don’t own the original source of the data, the weather API. While it’s easy to read the hard-coded data, it’s actually a significantly pared down version of the real data from the weather API. It would be nice to be working with data that’s as realistic as possible. To do this, we can use a fixture file. A simple curl call to the API can provide an actual JSON response payload. We’ve saved that to a file in the application, test/support/weather_api_response.json. Let’s modify our test to use that data instead of the handwritten values we used earlier:

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.