Skip to content

Latest commit

 

History

History
210 lines (137 loc) · 5.65 KB

exunit_with_mix.livemd

File metadata and controls

210 lines (137 loc) · 5.65 KB

ExUnit With Mix

Mix.install([
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"},
  {:tested_cell, github: "brooklinjazz/tested_cell"},
  {:utils, path: "#{__DIR__}/../utils"}
])

Navigation

Return Home Report An Issue

Setup

Ensure you type the ea keyboard shortcut to evaluate all Elixir cells before starting. Alternatively you can evaluate the Elixir cells as you read.

ExUnit With Mix

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.

Run Tests

We can execute all tests by running the following from the command line in the project folder.

$ mix test

Run Tests in A File

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

Run Tests By Line Number

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

Visual Studio Code Elixir Testing Extension

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.

Your Turn

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

Test Tags

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()

Mix Project Tags

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)

Your Turn

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.

Further Reading

For more on testing, consider using the following resources.

Commit Your Progress

Run the following in your command line from the beta_curriculum folder to track and save your progress in a Git commit.

$ git add .
$ git commit -m "finish exunit with mix section"

Up Next

Previous Next
Product Filters Games Wordle