Fl4m3Ph03n1x

Fl4m3Ph03n1x

How do you setup up multiple test environments in your projects?

Background

So I have a personal project with 2 types of tests: unit tests and integration tests. It is my personal believe this is a rather common scenario for people who value testing or where testing makes sense.

How I do it

I use the config folder to put all of the configurations and my folder looks like the following:

├── config/
│   ├── config.exs
│   ├── dev.exs
│   ├── prod.exs
│   ├── integration.exs
│   └── test.exs
├── lib/

Each file has configurations for the env they use.
In my code, I make use of such configurations like the following:

defmodule MarketManager.Store.FileSystem do
 
  @products_filename Application.compile_env!(:market_manager, :products)

  def get_products_from_syndicate(syndicate),
    do:
      @products_filename
      |> File.read!()
      |> Jason.decode!()
      |> find_syndicate(syndicate)

# Rest of code...

end

This way if I run a test, I can pass in MIX_ENV=integration and it will fetch the values from integration.exs for example.

Drawabacks

The problem with this approach is that you either have to always prepend MIX_ENV=blabla to your commands and it encourages you to have a more convoluted mix.exs file if you want to avoid typing all the jargon before every command.

How do you do it?

I have seen some people in this forum mention they use a “Repo” file for this kind of thing, where they pass in dependencies. Others use a Settings file that is a wrapper for configuration.

How do you guys do it?

First Post!

ityonemo

ityonemo

I don’t do this anymore, I mark my integration tests @moduletag :integration, though I currently don’t have any tests that hit the headless browser, I have a separate repo for those because our frontend isn’t in elixir !!?!!

Most Liked

LostKobrakai

LostKobrakai

That’s imho a big misconception. But there are layers to the problem here:

Currently you’re changing the code executed by having a different config when running tests. This is fine if your tests need a single static setup of dependencies applied.
Now you’re wondering how to move to a place where the single static setup of dependencies is no longer enough for all your tests. One way to do that is changing the setup between sets of tests, which need a common setup.

You’re doing this via the MIX_ENV (could be something else you can check on in a config.exs file) + multiple separated testruns, which is the only way to affect config early enough so you can depend on it at compile time. This is great in terms of your application being statically configured by config and nothing is dynamic at runtime. This also means all the tests you run in one of such batches need to deal with the same setup.

If the above is not flexible enough you need to start setting up dependencies at runtime. And that point is reached earlier than one might expect. E.g. using Mox means you’re setting up a dependency at runtime (the module might be the same, but the functions are different) and you either need to run tests with async: false to have the correct mocked implementation called or you need to pass in something from your test to the callsite, which makes it call the mocked implementation. The same is true for any two concrete implementations you want to change between within the same testrun. If you’re actually using Mox or not is actually not relevant.

So unless you can configure everything statically at compile time you’re in a place where you’re handling multiple interchangeable implementations at runtime and you’re imo better of acknowledging that fact in your codebase instead of trying to ignore it – even if in production there’s only one implementation used.

LostKobrakai

LostKobrakai

I’d start with the question: Why do you need another mix env for your integration tests in the first place?
I’m aware that many things differ between unit and integration tests, but if config.exs seems like the only way to provide them you might want to re-evaluate your options on dependency injection.

LostKobrakai

LostKobrakai

All of those questions boil down to “how can I make my code do different things (at runtime)”. If you can do that the stubbing/predefined responses part becomes simple, as it’s just one of those “different things” (likely a mox module). To get to that point imagine building a system, where in production you need to be able to switch out those parts, which you want to be able to switch out in your tests. E.g. the code you posted in your first post won’t cut it, as the dependency is set at compile time, so it’s not changeable at runtime.

Last Post!

Fl4m3Ph03n1x

Fl4m3Ph03n1x

As a matter of curiosity, I do use Mox xD
I think I now understand a little bit better. After some research I have found this article which depicts several ways to run tests and do DI, at what I believe is runtime according to your definition (please correct me if I am wrong).

https://blog.carbonfive.com/lightweight-dependency-injection-in-elixir-without-the-tears/

I really good article I recommend. It makes it specially clear there is no silver bullet :smiley:

Where Next?

Popular in Discussions Top

sergio
There’s a new TIOBE index report that came out that shows Elixir is still not in the top 50 used languages. It also goes on to call Elix...
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
lorenzo
Hey everone! I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
pdgonzalez872
If this has been asked here before, please point me to where it was asked as I didn’t find it when I searched the forum. Maybe a mailing ...
New
chulkilee
Here are the list of HTTP client libraries/wrappers, and some thoughts on HTTP client in general. I’d like to hear from others how they w...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

Other popular topics Top

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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
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

We're in Beta

About us Mission Statement