简体   繁体   中英

How to configure test_pattern for mix test

I'd like to change the test_pattern for mix test but I'm having trouble figuring out the right way to perform this configuration. I tried a number of variants of configuration in config/test.exs but never figured it out. Eventually I found that changing mix.exs (which I think is the highest level config file) worked:

defmodule Server.Mixfile do
  use Mix.Project

  def project do
    [
      app: :server,
      version: "0.0.1",
      elixir: "~> 1.4",
      elixirc_paths: elixirc_paths(Mix.env),
      compilers: [:phoenix, :gettext] ++ Mix.compilers,
      start_permanent: Mix.env == :prod,
      aliases: aliases(),
      deps: deps(),
      test_pattern: "*_test.ex", # ----- this is what worked!
    ]
  end

  # more config stuff
end

What's the right way to perform configuration for mix test ?

I came across this hack by looking at this LOC and trying to get my project config to conform to that check. I suspect there's a better way to achieve what I want.

Here's what my config/test.exs file looks like right now (with some comments)

use Mix.Config

# We don't run a server during test. If one is required,
# you can enable the server option below.
config :server,
       ServerWeb.Endpoint,
       http: [
         port: 4001
       ],
       server: false

# Print only warnings and errors during test
config :logger, level: :warn

config :server,
       Server.Repo,
       adapter: Ecto.Adapters.Postgres,
       username: System.get_env("POSTGRES_USER") || "postgres",
       password: System.get_env("POSTGRES_PASSWORD") || "postgres",
       database: System.get_env("POSTGRES_DB") || "postgres",
       hostname: System.get_env("POSTGRES_HOST") || "localhost",
       pool: Ecto.Adapters.SQL.Sandbox
       # test_pattern: "*_test.ex" ---- this doesn't get picked up

#config :project, ------------- this complains about the application "project" not being available
#       test_pattern: "*_test.exs?"

Why do you expect that changing anything in config :server would result in side-effects? Mix.Config.config/3 is a simple macro that stores the value(s) in the configuration storage for future use. It does not execute any code nor has any side-effects.

Even more, the section you were trying to put the test_pattern key is for Server.Repo , which is read by Ecto.Repo and the latter knows nothing about how to handle test_pattern key and effectively ignores it.

As a side note I'd say that changing test pattern to include compiled *.ex files does not sound like a very good idea in general; these files will be 1) compiled and 2) included in the release by default. They have *.exs extension on purpose: the trailing “s” stands for “script,” providing the files will be treated as scripts.

Also, if you are still positive you want this pattern, configuring it through project configuration is exactly where it belongs. This is not a hack by any means, this is the intended way to change the pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM