andym

andym

Elixir unit tests next to the source file

i prefer to have my unit tests next to the source file instead of a separate folder. when i run mix test it runs the tests only within the test folder. if i remove the test folder, move the test files, and the give the following configuration in the mix.exs configuration

      test_paths: ".",
      exclude: "deps",
      test_pattern: "*_test.exs",

then it seems to do something different and fails with the following error

(Protocol.UndefinedError) protocol Enumerable not implemented for "." of type BitString. This protocol is implemented for the following type(s): DBConnection.PrepareStream, DBConnection.Stream, Date.Range, Ecto.Adapters.SQL.Stream, File.Stream, Floki.HTMLTree, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, Jason.OrderedObject, List, Map, MapSet, Range, Stream, Timex.Interval
    (elixir 1.14.3) lib/enum.ex:1: Enumerable.impl_for!/1
    (elixir 1.14.3) lib/enum.ex:166: Enumerable.reduce/3
    (elixir 1.14.3) lib/enum.ex:4307: Enum.each/2
    (mix 1.14.3) lib/mix/tasks/test.ex:529: Mix.Tasks.Test.do_run/3
    (mix 1.14.3) lib/mix/task.ex:421: anonymous fn/3 in Mix.Task.run_task/4
    (mix 1.14.3) lib/mix/cli.ex:84: Mix.CLI.run_task/2

i also tried using tag filters

  @tag mustexec: true
  test "test1" do
  end

and then running ix test --only mustexec but i still get the same error

First Post!

moogle19

moogle19

test_paths expects a list of path, so ["."] should work. But you need a test_helper.exs file in your project directory then.
Also not sure why you want to break with the convention as it makes it hard for other people to find your test and understand your project.

Most Liked

namxam

namxam

I completely agree with you. And I would add another point. Having them next to the module which is tested, has three more advantages:

  1. I use tests as a documentation tool. They are nice code examples and often better transfer meaning than words. When a library does not have extended documentation, but a good test suite, it helps to check it out.

  2. Code / test navigation is much easier, as you do not have to switch between deeply nested folders.

  3. When remove obsolete modules, with colocated tests, you only have two remove 2 files or a folder. No need to search in multiple places.

The only thing which is bugging me, that for integration or e2e tests, you still need a test folder or something similar to it.

Right now, I stay with elixirs / phoenix default structure and put all tests in the test folder. But I would still prefer to have them colocated :slight_smile:

sodapopcan

sodapopcan

This must be a people who use tree-explorers for navigation versus those who don’t. Any semi-decent modern editor is going to have a “jump to test file and back” hotkey so finding 1:1 tests is a non-issue. I do use a tree-explorer but it’s primarily for getting an overview of module structure so I find it so noisy with file names doubled-up most of the time.

I could bikeshed on this forever so I’ll leave it at that :upside_down_face:

ericdouglas

ericdouglas

Also not sure why you want to break with the convention as it makes it hard for other people to find your test and understand your project.

It is much easier to find the test files in that way since it is colocated with the file containing the implementation.

Ex:

lib/
    module/
        file.ex
        file_test.exs
        another_file.ex

Another advantage is that it becomes super clear which files have tests and which don’t.

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement