Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.9", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Upon completing this lesson, a student should be able to answer the following questions.
- How do we run tests by line number?
- How do we use tags to include or exclude specific tests? Why might we want to do that?
Mix projects provide some conveniences when working with ExUnit.
Let's see how we would test a Math
module if it were a Mix project.
First, create a new mix project called math
.
$ mix new math
You should see an output similar to the following.
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/math.ex
* creating test
* creating test/test_helper.exs
* creating test/math_test.exs
Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:
cd math
mix test
Run "mix help" for more commands.
Tests in a mix project are in the test/
folder. Generally there is an equivalent file in the test/
folder
for each tested file in the lib
folder.
ExUnit is started for us in the test/test_helper.exs
file, and
files in the test/
folder automatically compile, evaluate, and execute when we run tests.
We can execute all tests by running the following from the command line in the project folder.
$ mix test
We can run a single test file by providing its path.
$ mix test path/to/test/file.exs
For example, we can run the math_test.exs
file.
$ mix test test/math_test.exs
We can execute a specific test or several tests under a describe
block by providing the line number.
$ mix test test/math_test.exs:5
We highly recommend using the Elixir Test extension if you are using Visual Studio Code.
Elixir Test provides several commands which facilitate more straightforward test running.
Move the Math
module and test suite you created in Math Module Testing into the math_test.exs
file. Then use the command line to experiment with running tests.
Run all tests from the command line.
$ mix test
Run tests in the math_test.exs
file.
$ mix test test/math_test.exs
Run tests in the the Math.add/2
describe block. Replace 1
with the correct line number.
$ mix test test/math_test.exs:1
Run a single test. Replace 1
with the correct line number.
$ mix test test/math_test.exs:1
We can use @moduletag
, @describetag
, and @tag
module attributes to tag our tests.
Once tagged, we can configure ExUnit to exclude, include, or only run tests with specific tags using ExUnit.configure/1.
Notice below that the test suite passes because the failing test never runs.
ExUnit.start(auto_run: false)
# We Can Configure ExUnit To Exclude, Include, Or Only Run Certain Tests.
ExUnit.configure(exclude: :my_module_tag)
defmodule TagTest do
use ExUnit.Case
@moduletag :my_module_tag
test "failing test" do
assert false
end
end
ExUnit.run()
Once tagged, we can use the flags --only
, --exclude
, and --include
to run specific tests.
$ mix test --exclude my_tag
Alternatively, we can place the ExUnit.configure/1 function in test_helpers.exs
.
ExUnit.start()
ExUnit.configure(exclude: :my_tag)
Use the @moduletag
, @describetag
, and @tag
module attributes in the math_test.exs
module. Then use the --exclude
, --include
, and --only
flags for the mix test
command to only run certain tests.
Finally, configure the test_helper.exs
file to exclude one of your tags and run all tests to verify that it has been excluded.
For more on testing, consider using the following resources.
- Mix Test, for more on how you can use the
mix test
command. - ExUnit, for documentation on ExUnit.
- ElixirSchools: Documentation, an lesson by Elixir schools on documentation and doc-testing.
DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.
Run git status
to ensure there are no undesirable changes.
Then run the following in your command line from the curriculum
folder to commit your progress.
$ git add .
$ git commit -m "finish ExUnit With Mix reading"
$ git push
We're proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.
We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.