If you have multiple test_paths
defined, is it possible to run them in sequence instead of in parallel?
Is it possible to setup entire test suits that should run in sequence?
Some background information:
I have two kind of tests.
- Unit tests that tests a single unit in isolation using Mox to mock the modules dependency, these make up ~98% of the tests. All of these resides in the
test
directory. - Integration tests / End to end test. These test requires a environment where the rest of the system is up and running and is used to verify that the other team haven’t broken anything, these are mostly here for assurance.
These resides in thetest_integration
directory and are tagged with@tag :integration
.
Right now we are unit testing the api endpoints by calling the router and letting it route a Plug.Test.conn
request to the endpoint, due to this we can’t pass arguments directly to the endpoint plug. Instead we pass the mocked dependencies to the endpoint by changing the Application env and the endpoint plug fetches them with a Application.get_env
call in a helper plug that adds a dependency struct to the connection.
The test_helper
in the test
directory creates Mox objects and changes the application env to use them by doing Application.put_env(:api, Twitter, TwitterMox)
but all tests in the test_integration
directory should use the “real” modules.
I solved this right now by having two test commands, one which excludes integration tests and one that only runs integration tests and having the test_helper
use the code below but that means elixir overwrites the coverage files. If I could run the test_helpers in sequence the the helper would just need to change the application env to match the current testsuite.
# The `test/test_helper.exs` file.
alias Extra.Api.{Foo, Bar}
alias Extra.Task
require Logger
mock = fn (source) ->
target = :"#{source}Mox"
Mox.defmock(target, for: source)
Application.put_env(:api, source, target)
# Logger.info(fn -> "Added mox mock for #{source}."end)
end
case ExUnit.configuration |> Keyword.get(:exclude) |> Enum.member?(:integration) do
true ->
mock.(Foo)
mock.(Bar)
mock.(Task)
ExUnit.start(
capture_log: true,
)
false ->
IO.puts "Skipping unit test since integration tests will be running."
end