【问题标题】:In Elixir, placing test files alongside their related modules在 Elixir 中,将测试文件与相关模块放在一起
【发布时间】:2018-07-26 23:39:46
【问题描述】:

一个典型的 Elixir 项目有这样的结构:

project
|-- lib
|   `-- my_module.ex
`-- test
    |-- my_module_test.exs
    `-- test_helper.exs

在编写节点项目时,我喜欢将我的测试模块放在他们正在测试的模块旁边。这可以使用 Jest 等工具轻松配置:

project
|-- lib
|   |-- my_module.ex
|   `-- my_module.test.exs
`-- test
    `-- test_helper.exs

是否可以在 Elixir/Exunit 中进行类似的设置?

【问题讨论】:

标签: elixir ex-unit


【解决方案1】:

好的,为了以后的访问者,我将其作为答案。

混合文档explicitly states

从命令行调用mix test 将在每个文件中运行与您项目的*_test.exs test 目录中的模式 匹配的测试。

看起来ExUnit 作者强烈不鼓励开发人员将测试放在其他地方。正如@harryg 发现的那样,尽管如此,它仍然是可能的。 project 设置有tests_path 变量that is checked for where tests are to be found(和test_pattern 用于模式,)默认为"test" 和 分别为"*_test.exs"

要将其重置为其他值,只需更新 project 回调添加以下两行:

  def project do
    [
      app: @app,
      version: @version,
      elixir: "~> 1.6",
      start_permanent: Mix.env() == :prod,
      ...
      test_paths: ".",
      test_pattern: "*_test.exs" # or something else
    ]
  end

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    相关资源
    最近更新 更多