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 Switch mode

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

Last Post!

dwahyudi

dwahyudi

Test user can authenticate normally
Test authentication via google account
Test registration via google account
Test user cannot authenticate when wrong username / password
Test user cannot register with invalid mail format
Test user can add item to cart
Test user cannot add out of stock item to cart
Test calculation of order discount
Test completed order not to be editable
Test driver can pickup customer
Test driver can complete customer’s order
Test blocked user cannot comment on user’s post
Test unregistered visitor can see products

And so on.. I kinda think that test is equal to specify in rspec.
it and specify in rspec is just the same, but it is usually followed by verbs.

it should
it must
it allows
it forbids

Where Next?

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2976 91332 914
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
AstonJ
Just a general thread to post chat/news/info relating to AI/ML stuff that may be relevant for Nx now or in the future. Got anything to sh...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
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
zachdaniel
Introducing AshStorage! Attachment and file management that slots directly into your resources :smiling_face_with_sunglasses: I had hope...
New

We're in Beta

About us Mission Statement