Importing test code in elixir unit test
Asked Answered
B

2

15

I'm writing tests of some Elixir code that interacts with SSH. In my tests, I'd like to start an SSH server that I can run my code against. I'd prefer to store this code in it's own file in the test directory, and have it imported by various different tests.

I've not been able to get this to work too well though.

I've tried creating an test/ssh_server.ex file containing a SSHServer module, but when I add import SSHServer to my tests, I get:

(CompileError) test/end_to_end_test.exs:13: module SSHServer is not loaded and could not be found

Am I missing something? Is there some way to force mix test to import my test/ssh_server.ex file?

Buoy answered 4/6, 2015 at 19:26 Comment(0)
B
17

I've currently got around this by manually loading the code from my test_helper.exs file:

Code.load_file("test/ssh_server.ex")
Buoy answered 4/6, 2015 at 19:37 Comment(3)
This is a good solution. I would name the file .exs too though, as it is never compiled. Another alternative is to tell Mix to compile everything in a special directory too, like test/support. Phoenix does this here: github.com/phoenixframework/phoenix/blob/…Lemniscate
I'm using this for seeding before tests with Code.load_file("priv/repo/seeds.exs") in my test setup and it's working like a charm.Kristinakristine
I would go with Code.require_file instead of load. This will ensure that the file is only loaded once. I would also agree on the use of the .exs extension. This is the way ecto does it: github.com/elixir-ecto/ecto/blob/master/test/ecto/…Gentlemanfarmer
H
2

Compile the module, then it will be available.

This can be done in either iex

iex > c "test/ssh_server.ex"

or with elixirc

elixirc "test/ssh_server.ex"

http://elixir-lang.org/getting-started/modules.html#compilation

Hyphen answered 6/7, 2015 at 4:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.