Issue when running tests: (Mix.Error) The database for Copi.Repo couldn't be created: killed

Hello,

I’m new to Elixir development. I’ve started to contribute to a website written with the Phoenix framework.

When I run the command mix test, most of the tests are failing. I suspect it’s because my test environment is not set up properly. Indeed, when I run an individual test I’ve got the following error message:

Mix task exited with reason
an exception was raised:
    ** (Mix.Error) The database for Copi.Repo couldn't be created: killed
        (mix 1.16.0) lib/mix.ex:580: Mix.raise/2
        (elixir 1.16.0) lib/enum.ex:987: Enum."-each/2-lists^foreach/1-0-"/2
        (mix 1.16.0) lib/mix/task.ex:478: anonymous fn/3 in Mix.Task.run_task/5
        (mix 1.16.0) lib/mix/task.ex:544: Mix.Task.run_alias/6
        (debug_adapter 0.20.0) lib/debug_adapter/server.ex:1969: ElixirLS.DebugAdapter.Server.launch_task/1
returning code 1
11:15:00.361 [error] Process #PID<0.491.0> raised an exception
** (Mix.Error) The database for Copi.Repo couldn't be created: killed
    (mix 1.16.0) lib/mix.ex:580: Mix.raise/2
    (elixir 1.16.0) lib/enum.ex:987: Enum."-each/2-lists^foreach/1-0-"/2
    (mix 1.16.0) lib/mix/task.ex:478: anonymous fn/3 in Mix.Task.run_task/5
    (mix 1.16.0) lib/mix/task.ex:544: Mix.Task.run_alias/6
    (debug_adapter 0.20.0) lib/debug_adapter/server.ex:1969: ElixirLS.DebugAdapter.Server.launch_task/1

The dev environment is working fine. I can launch the website and add data to the database.

Do I miss something to set up the test environment?

Thank you for you help. :slight_smile:

1 Like

Seems like that the tests cannot reach the database. Is it running? The connection url for test env is correct?

If it works in dev mode you can try run the application in test env:

MIX_ENV=test iex -S mix

Doing so could help in finding the problem.

1 Like

Can you show us the aliases section of your mix.exs file? Also sometimes this happens when people have fallible code in their application initialization.

Look for <YOUR_APP>.Application module and its init function.

Hello both,

Thank you very much for taking the time to answer my question. I actually managed to “solve the problem”. At least I can run the tests individually. The reason for that is that I extracted the user and password of the database to another file as below:

username: System.get_env("POSTGRES_USER"),
password: System.get_env("POSTGRES_PASSWORD"),

When I changed back to a hardcoded value as below I works fine

username: "my_user",
password: "my_password",

One solution I’ve experienced is to make sure you’re exporting your environment variables.

For example, this might not work:

POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres

mix test

But this might:

export POSTGRES_USER=postgres
export POSTGRES_PASSWORD=postgres

mix test
1 Like

Thanks @arcanemachine for you reply. mix test is working fine as I source the file where I keep my environment variable before running mix test. The issue is when I run one test on its own from VSCode using the VSCode debugger. I actually wonder if it’s creating a subshell to run this test?

Yeah, sounds like a VS Code problem. Could also depend on how you’re setting the environment variables too.

I can’t help with the VS Code stuff… but to prevent suprises like this in the future, you could use System.fetch_env!/1 so that the system raises an exception if you haven’t actually set the environment variable. It’s nice when you know 100% that the env var will be set somewhere.