victorolinasc

victorolinasc

Giving names to unit tests

Hello people! Just wanted to bring a discussion to the community that happened on my team.

When it comes to testing, naming the tests is itself an art. There are several approaches and all of them and none of them are right/wrong. I’ve seen several patterns through the community and some others on my team because of the different backgrounds each have on the subject. I’d like to know your opinions about it.

Different styles

Here is some styles I’ve seen:

  • The AAA approach (arrange - act - assert): gives names like test "given a proper user when it authenticates it receives a proper session"
  • The “it does some result” approach: test "it authenticates properly"
  • The “it capabilities” approach: test "it can authenticate an active user"
  • The “result verb” approach: test "succeeds authentication with an active user"
  • The “action” approach: test "authenticates an active user"

So on …

Describe and trace option

What gives me some anxiety is the way the sentence ends up when we use “describe” and the “–trace” option.

The pattern ExUnit uses is: "test #{describe_text} #{test_text}". So the end result could be something like:

“test some_function/1 it authenticates properly”

This is strange to read. But to make it more plausible we would have to add a strange clause to desribce like describe "that some_function/1" do ... end. So on the report it would be:

“test that some_function/1 succeeds with an active user”

We could use a different approach and use the action directly:

“test some_function/1 can authenticate an active user”

What are your thoughts on good unit testing naming? Sorry for English mistakes. Altough I am picking on the grammar here, English is not my mother’s tongue.

Most Liked

dimitarvp

dimitarvp

describe "MyModule.my_function/1" do

  test "authenticates successfully with valid parameters" do
  end

  test "fails authentication with invalid parameters" do
  end

end

Where Next?

Popular in Discussions Top

blackode
Elixir Upgrading is so Simple in Ubuntu and It worked for me Ubuntu 16.04 git clone https://github.com/elixir-lang/elixir.git cd elixir...
New
Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New
axelson
Decided against including more info in the title, but the gist is that Plataformatec sponsored projects will continue with the assets bei...
New
arcanemachine
https://nitter.net/josevalim/status/1744395345872683471 https://twitter.com/josevalim/status/1744395345872683471
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 19568 166
New
AstonJ
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

We're in Beta

About us Mission Statement