Write Some Basic Tests
Learn a few basic tests in a Mix-created project.
Elixir comes with a wonderful and simple testing framework called ExUnit.
Have a look at this code:
defmodule IssuesTest do
use ExUnit.Case
doctest Issues
test "greets the world" do
assert Issues.hello() == :world
end
end
It acts as a template for all the test files we write. We just copy and paste the boilerplate into separate test files as we need them. So, let’s write tests for our CLI
module, putting those tests into the file test/cli_test.exs
. (Test file names must end with _test
.) We’ll test that the option parser successfully detects the -h
and --help
options and that it returns the arguments otherwise. We’ll also check that it supplies a default value for the count if only two arguments are given.
Note: We may get a loading module/bad file error on executing the
mix test
command the very first time because it can take some time to compile this code. We won’t see it when we rerun this command.
Run the mix test
command to execute both codes given below.
Get hands-on with 1400+ tech skills courses.