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.

Where Next?

Popular in Questions Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49084 226
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement