View Source ExUnit.CaseTemplate (ExUnit v1.15.7)
Defines a module template to be used throughout your test suite.
This is useful when there are a set of setup callbacks or a set of functions that should be shared between test modules.
Let's imagine that you create a MyCase module that calls use ExUnit.CaseTemplate. When a test case module calls use MyCase, the
following things hold true:
All the functionality that
MyCasewould have had available fromuse ExUnit.Caseis available (same as ifMyCasecalleduse ExUnit.Casedirectly)The
setupandsetup_allcallbacks that you define inMyCaseget used in the test case module
The options that you pass to use MyCase get also passed to use ExUnit.Case under the hood. This means you can do things like use MyCase, async: true. You can also access this options in using/2.
use ExUnit.CaseTemplateWhen you
use ExUnit.CaseTemplate, it will import the functionality fromExUnit.Assertions,ExUnit.Callbacks, and this module itself. It will also define a__using__callback, so the module itself can be used as a template instead ofExUnit.Case.
Example
defmodule MyCase do
use ExUnit.CaseTemplate
setup do
IO.puts("This will run before each test that uses this case")
end
end
defmodule MyTest do
use MyCase, async: true
test "truth" do
assert true
end
endIf you need to "hook" into use MyCase and do other things as well,
you can use the using/2 macro. See its documentation for more
information and examples.
defmodule MyCase do
use ExUnit.CaseTemplate
using do
quote do
import MyApp.TestHelpers
end
end
end
Summary
Functions
Allows a developer to customize the using block when the case template is used.
Functions
Allows a developer to customize the using block when the case template is used.
You can use an optional var argument when calling using/2. ExUnit
will pass whatever argument you pass to use MyCase as this var argument. See the examples below for clarification.
Example
defmodule MyCase do
use ExUnit.CaseTemplate
using do
quote do
# This code is injected into every case that calls "use MyCase"
alias MyApp.FunModule
end
end
endYou can specify an argument to using/2:
defmodule MyCase do
use ExUnit.CaseTemplate
using options do
quote do
if unquote(options)[:import_helpers] do
import MyApp.TestHelpers
end
end
end
endThe second argument passed to use MyCase gets forwarded to using/2 too:
defmodule SomeTestCase do
use MyCase, async: true, import_helpers: true
test "the truth" do
# truth/0 comes from MyApp.TestHelpers:
assert truth()
end
endSharing options with
use ExUnit.CaseThe second argument that you pass to
use MyCaseis also passed as the second argument touse ExUnit.Case.