Anko

Anko

I’m doing evaluation of using elixir for some e2e api tests for a distributed application not written in elixir.

My plan is to use ExUnit to run these tests concurrently with the Req library. I think it will showcase elixir’s strength in parallelism.

The problem i’m facing at the moment is I’d like some global state which contains the bearer tokens for the rest of the tests. I can do a login in a setup_all function, in each test module, but that means we login a bunch more times than is needed. The applicaiton will create a new bearer token each login, and i really want to share one between modules for the same user.

I just wondered if anyone has encountered this type of thing before and how they solved it? Ideally ExUnit.start would have an option of a state, which gets merged with the state from setup/setup_all

Am I thinking about this wrong?

Showing Posts 1 to 10

adw632

adw632

You should be able to setup req with the required auth headers once then all your test cases just use that req struct to do their requests:

Anko

Anko OP

I guess you mean like

req = Req.new(base_url: "https://api.github.com", auth: "token")

or something, but then how do i get this global req into all test modules? That’s really the guts of my question.

In my case specifically, login is particularly expensive so i’d like to do it once globally per user, and push this into some sort of global state

adw632

adw632

It might help if you read the ExUnit documentation, it provides example of setting up the test context and shows how it is available to all your tests.

Hermanverschooten

Hermanverschooten

Hacky, but you could declare a module in your test_helper.exs that logs in and stores the token in a function. Something like:

defmodule Login do
@token login_me_in()
  def token, do: @token

  def log_me_in do
      Your login code here
  end
end

This would get executed once before all the tests.

You would call it as Login.token()

You could play around with @tag and :included in ExUnit.configuration() to only conditionally do the login, so eg use a tag :external and only really perform the login if it is included, otherwise return a dummy token.

Anko

Anko OP

Hermanverschooten, you sir, are a legend. This is exactly the sort of hack i was looking for.

Ideally ExUnit would cover this type of use case but i realise it might be fringe, so this will work in the meantime. Thank you!

Anko

Anko OP

sorry andrewh but i don’t understand how what you linked to sets up a global context, just the context for a module. Please let me know if i’m mistaken.

I read the docs several times before posting this.

Hermanverschooten

Hermanverschooten

@adw632’s solution would work if all your tests that need the token are in the same testfile. setup_all would run just once before all these tests, but as far as I know there is currently no way to set a “before” hook to run once before all the tests, there is one for after all tests after_suite/1.

adw632

adw632

Yes ExUnit is designed to isolate tests and test suites so you don’t get leakage and side effects between tests.

It even randomizes the order to help catch unintended dependencies and side effects to make testing robust.

You could use the test_helper compile time “evil” that @Hermanverschooten showed to create a globally fixed value.

You could also use many of the nicely explained flakey test approaches to undo all the goodness that ExUnit provides and make your tests flakey and evil too.

Such evil can include stashing a Req structure in the Application environment or Ets. So in your setup_all callback you try and get the Req struct and only create and set it if it is doesn’t yet exist.

You will likely introduce a race condition on the create which you thoughly deserve for doing such evil things.

I would just use setup_all to do the authentication once per test suite. How many test suites do you have?

Perhaps you need to think about Mocking?

Hermanverschooten

Hermanverschooten

You are right, though maybe a bit strong in wording.
I’d consider (this case) more as a seed.
Mocking may indeed be something to look into, I use it extensively.

Anko

Anko OP

If i end up using elixir for this (currently they are a mix of karate and postman tests) then it is likely that i’d have 80+ suites/modules

I fully understand the issues with race conditions and such -I think it’s somewhat par for the course with system testing.

I will look at mocking but we already have application mocks - that’s generally integration testing or service level testing if the library mocks in the general sense. the point of system/e2e testing is to not mock, eg. to be as close to our deployed system as possible, and have all the real components under test.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews