Fl4m3Ph03n1x

Fl4m3Ph03n1x

Background

I have a test file with several describe blocks, each one with a custom setup block, like the following:

defmodule MyAppTest do
  use ExUnit.Case

  describe "group 1" do
    
    setup do
      :persistent_term.put("hello", :world)
      :ok
    end

    test "test 1" do
      assert true
    end

    test "test 2" do
      assert true
    end

  end

# describe group2 ....

end

Here I am setting up a persistent term that I will use along the app. Now, once I reach the end of all tests in the describe block, I want to clean up, meaning I want to delete the "hello" entry from :persistent_term.

To achieve this I am aware of on_exit, but according to my understanding, this callback is only executed once at the end of all tests from all describe blocks.

Questions

  1. Is my understanding of on_exit correct?
  2. How can I run a clean up function after the tests of each describe block?

Showing Posts 1 to 7

idi527

idi527

:waving_hand:

on_exit in a setup block is executed after each test, in my experience. I’ve never tried using on_exit in setup_all though.

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

According to my understanding and in my specific example, setup will only be executed once per describe block. What I am looking for is a on_exit that is executed once per describe block as well.

josevalim

josevalim

Creator of Elixir

setup is always executed per test and on_exit will always respect the context it is called in. On exit in setup runs after the test. On setup all, after all tests, etc.

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

Ahh, so in my example, I am actually running :persistent_term.put("hello", :world) each time I run a test, and not once per describe block, right?

Will the current code run :persistent_term.put("hello", :world) once per each test inside the given describe block, or once per each test independently?

josevalim

josevalim

Creator of Elixir

It will run once for each test in the describe block. But since it is a side-effect, it will be leaked to other tests.

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

I see. This open an interesting discussion (for another post).

Last set of questions:

  1. If I use setup_all inside a describe block, it will ignore it, correct? (meaning it will run as if it wasn’t inside a block, before all tests in the file)
  2. How do I use on_exit with setup all? Could someone give me an example? (I have trouble understanding the docs)
peerreynders

peerreynders

it will ignore it, correct?

If I understand the source correctly it won’t even compile.

Could someone give me an example?

i.e. it doesn’t look like it is any different from using on_exit/1 inside a regular setup block. on_exit registers a function to perform the teardown (and if the function is created inside the block is has access to anything inside of the block).

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews