collegeimprovements

collegeimprovements

Testing Elixir (PragProg)

by @whatyouhide and @idlehands

Elixir offers new paradigms, and challenges you to test in unconventional ways. Start with ExUnit: almost everything you need to write tests covering all levels of detail, from unit to integration, but only if you know how to use it to the fullest—we’ll show you how. Explore testing Elixir-specific challenges such as OTP-based modules, asynchronous code, Ecto-based applications, and Phoenix applications. Explore new tools like Mox for mocks and StreamData for property-based testing. Armed with this knowledge, you can create test suites that add value to your production cycle and guard you from regressions.

Write Elixir tests that you can be proud of. Dive into Elixir’s test philosophy and gain mastery over the terminology and concepts that underlie good tests. Create and structure a comprehensive ExUnit test suite, starting from the basics, and build comprehensive test coverage that will provide safety for refactoring and confidence that your code performs as designed. Use tests to make your software more reliable and fault tolerant.

Explore the basic tool set provided by ExUnit and Mix to write and organize your test suite. Test code built around different OTP functionality. Isolate your code through dependency injection and by using Mox. Write comprehensive tests for Ecto projects, covering Ecto as a database tool as well as a standalone data validation tool. Test Phoenix channels from end to end, including authentication and joining topics. Write Phoenix controller tests and understand the concepts of integration testing in Elixir. Learn property-based testing with StreamData from the author who wrote the library.

Code with high confidence that you are getting the most out of your test suite, with the right tools that make testing your code a pleasure and a valuable part of your development cycle.

Don’t forget you can get 35% off the ebook with our Devtalk discount, use code ‘devtalk.com:023:

Most Liked

kokolegorille

kokolegorille

There is a new discount available now… devtalk.com

Update: it is not a link, it is the new discount code!

idlehands

idlehands

Author of Testing Elixir

That’s not what I am saying, sorry if there is confusion. We show several ways to isolate code and I specifically mentioned Mox, so if you want me to pick, I’d say we lean more towards London/Mockist but also don’t find value in forcing people to declare their style. Not every problem is the same and I don’t solve them all the same way.

There is plenty in the book about isolating code and you’re likely find that while our definition of a unit is intentionally vague (that section is about thinking about your unit and understanding the concept of a black box), our examples focus on small parts of an application, often just a single module.

As I mentioned, I have a feeling that you might pick up some useful stuff, but my sense is that you’re solid enough in your opinions that you either will know most of it and disagree with plenty.

We really did try to take a fairly neutral approach on a lot of things, though, so if you’re looking for “how do I test X?” you might be able to find answers.

idlehands

idlehands

Author of Testing Elixir

Sorry, I never actually answered your question. I personally would likely test the modules but have an endpoint test that also proved everything works all the way through (happy path) and at least one error case. A login endpoint is pretty straight forward but most of the endpoints I’ve ever written have plenty of branching possibilities below the hood. So, instead of writing outside tests to hit every one of those branches, I isolate that code using whatever is the correct tool and exercise the branching logic. Then, the endpoint test would be to make sure that all of the pieces connect.

As far as which tests I start with, it 100% depends on the scenario every time. Sometimes I start really low-level and sometimes I start on the endpoint. The only thing that I’m dogmatic about is that I write my tests first.

Where Next?

Popular in Books Top

shankardevy
Dear Elixir Alchemists I’m happy to share that I’m working on my 2nd series of books (See my first one here) and this time it’s about As...
New
CinderellaMan
Hands-on Elixir & OTP: Cryptocurrency Trading Bot by Kamil Skowron Want to learn Elixir & OTP by creating a real-world project...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
waseigo
by Isaak Tsalicoglou Why this book is being written A few years ago I got fed up with the menial manual labor I was engaged in in our f...
New
PragmaticBookshelf
Svilen Gospodinov edited by Jacquelyn Carter @jkcarter Learn different ways of writing concurrent code in Elixir and increase your appli...
New
AstonJ
by Joe Armstrong A multi-user game, web site, cloud application, or networked database can have thousands of users all interacting at th...
New
codestirring
Hey Elixir Forum, I’ve written a ebook called the Phoenix LiveView Cookbook which I’ve written in the hopes of distilling the knowledge ...
New
jack-s9
Simplify and streamline your Elixir Phoenix Deployments. Deploy with tools that are easy to use, maximize Elixir’s features, and have ex...
New
arpan
:waving_hand: Hey Elixir Enthusiasts! I’ve got something cool for you. It’s a free book on Elixir that dives into the nitty-gritty of co...
New
AstonJ
by Fred Hebert This book intends to be a little guide about how to be the Erlang medic in a time of war. It is first and foremost a col...
New

Other popular topics Top

mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement