coen.bakker

coen.bakker

What are "good" and "bad" unit tests (in Elixir)?

In an interview of José by Theo Browne, they mention the importance of “good” tests, in contrast to “bad” ones.

Specifically, they started talking about this from this timestamp on.

It’s not the focus of the interview, however, and it’s more of a short tangent. They don’t go into detail about what makes a test good or bad.

But now I am actually curious about the answer.

What are your ideas about this topic? And are there any learning resources you can recommend that cover this topic?

What I try to make sure when writing tests is that I am testing behaviour, rather than implementation details. But maybe there are still a ton of ways to mess the tests up, besides that?

Most Liked

cevado

cevado

I have a few rules of thumb when it regards tests:

  • test should stress edge cases, i’ll never know all edge cases at a glance, but everytime you find one, you should add a new test or at least change an existing test to cover that.
  • test shouldn’t have only the happy path.
  • test shoudn’t check library behaviour. libraries are not always 100% safe but if you don’t trust it to do what it says it does, you’d be better not using the library.

I think other stuffs apply, but they’re usually related with context of your codebase, what is more important and what is crucial for your application.

dimitarvp

dimitarvp

I always had difficulties putting the “bad” label on tests. The only truly bad tests I can think of is checking if your libraries are working at all, and even that is not 100% because at one point you want any guarantee you can get so it does make sense to have integration-like tests that also check if your 3rd party dependencies do what you want them to do.

The objectively good tests to me are property tests. As @cevado said above, your tests should check the edge cases and property tests are really good at that (though within limits; it depends how well you have modeled your property test generators).

I’d say the best tests you can get are those that you feel are validating your app’s / library’s functionality about which you care the most. Don’t let others tell you what’s good or bad.

D4no0

D4no0

I’m not a big guru on writing good tests yet, however testing goes hand in hand with design, bad unit tests point to bad design. This is one of the reasons why TDD or BDD are such a great thing, you design the system from the start with testability in mind, and you will also get benefits like isolation and separation of concerns out of the box for free (if you write unit tests correctly ofc).

One of the lessons I learned the hard way is to always keep unit tests isolated, if you start to couple them you will have brittle tests that will start to collapse once you make some small changes, same goes for the detail implementations.

I didn’t watch the video, however this discussion popped a lot of times on this forum. Writing tests to check the types is in my opinion in category of those bad unit tests that check implementation details, however I have never seen those kind of tests in my career so maybe the wording is misleading.

LostKobrakai

LostKobrakai

I think this section of an earlier keynote by jose goes into more details on the point he’s trying to make:

sodapopcan

sodapopcan

This is a can-of-worms topic :worm: I love talking about this stuff and could talk about it for hours (and I have!) even though I would still not call myself anything close to an expert.

This talk is an oldie but a goodie.

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement