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?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #ai
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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
I guess you mean like
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
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
Hacky, but you could declare a module in your
test_helper.exsthat logs in and stores the token in a function. Something like:This would get executed once before all the tests.
You would call it as
Login.token()You could play around with
@tagand:includedinExUnit.configuration()to only conditionally do the login, so eg use a tag:externaland only really perform the login if it is included, otherwise return a dummy token.Anko
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
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
@adw632’s solution would work if all your tests that need the token are in the same testfile.
setup_allwould 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 testsafter_suite/1.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
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
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.