vic/test_async
{ "defaultBranch": "master", "description": "Make tests inside your ExUnit case to run async.", "fullName": "vic/test_async", "homepage": "", "language": "Elixir", "name": "test_async", "pushedAt": "2016-12-19T08:15:15Z", "stargazersCount": 5, "updatedAt": "2022-07-28T21:41:41Z", "url": "https://github.com/vic/test_async"}TestAsync
Section titled “TestAsync”Make tests inside your ExUnit case to run async.
Installation
Section titled “Installation”Available in Hex, the package can be installed
by adding test_async to your list of dependencies in mix.exs:
def deps do [{:test_async, "~> 0.1", only: :test, runtime: false}]endAs you might already know, ExUnit lets you define async test cases by
providing an async: true option, like this:
defmodule App.FineTest do use ExUnit.Case, async: true
test "one" do IO.inspect :one end
test "two" do IO.inspect :two endend
defmodule App.OtherTest do use ExUnit.Case, async: true
test "three" do IO.inspect :three endendAs described by the ExUnit documentation
async: true, runs the test case concurrently with other test cases. The individual tests within each test case are still run serially
That means, one and two will still be run one after the other.
If you’d want those two to be run concurrently, you’d have to create a new test case module for each.
This is exactly what TestAsync does, by providing a tiny macro test
macro that will turn your ExUnit case tests into async cases.
Just use TestAsync !
defmodule App.FineTest do use ExUnit.Case use TestAsync
test "one" do IO.inspect :one end
test "two" do IO.inspect :two endendGotchas
Section titled “Gotchas”Current module
Section titled “Current module”If you use __MODULE__ on a test body, it will be the name of the
generated case module and not the one of the parent module.
defmodule App.NamedTest do use ExUnit.Case
test "one" do assert __MODULE__ == App.NamedTest.OneTest endendSetup functions
Section titled “Setup functions”setup and setup_all are supported.
Note that now setup_all is run for every test
because it will be live on its own test case.
Private functions
Section titled “Private functions”Private functions defined on the parent module cannot be seen inside the generated async modules.
defmodule App.SomeTest do use ExUnit.Case use TestAsync
defp hidden, do: :ninja
test "cant be seen" do assert hidden() # compile error endendShared case template
Section titled “Shared case template”However you can give use TestAsync a do block, which will be
turned into a shared ExUnit.CaseTemplate.
When using your own templates, do so on the do block.
defmodule App.NinjaTest do use ExUnit.Case
use TestAsync do defp hidden, do: :ninja use App.MyTestTemplate end
test "can be seen" do assert hidden() endend