ExUnit Documentation

From Elixir Wiki
Jump to navigation Jump to search

ExUnit Documentation[edit]

ExUnit is a testing framework for Elixir, built specifically for writing test cases and running tests efficiently. This documentation provides an in-depth overview of ExUnit's features and how to effectively use them in your projects.

Table of Contents[edit]

TOC

Getting Started[edit]

To start using ExUnit, you will need to include it as a dependency in your mix.exs file and ensure it is started as part of your application. Here is an example:

elixir defp deps do

 [
   {:ex_unit, "~> 1.12", only: :test}
 ]

end

Defining Tests[edit]

To define a test case, you need to use the @test attribute, followed by a descriptive test name and the test body. Here is an example:

elixir defmodule MyTest do

 use ExUnit.Case
 @tag :mytag
 test "addition" do
   assert 1 + 1 == 2
 end

end

Running Tests[edit]

To run your tests, you can use the mix test command in your project's root directory. By default, this command will run all the tests in your project. You can also provide specific test files or directories to run. Here is an example:

shell mix test mix test test/my_test.exs mix test test/

Test Case Structure[edit]

A test case in ExUnit consists of multiple test functions and optional setup and teardown functions. Here is an example of a structured test case:

elixir defmodule MyTest do

 use ExUnit.Case
 setup do
   # Setup code
 end
 test "test 1" do
   # Test code
 end
 test "test 2" do
   # Test code
 end
 teardown do
   # Teardown code
 end

end

Assertions[edit]

ExUnit provides a variety of assertions to validate the expected behavior of your code. Here are some commonly used assertions:

  • assert(expression) - Asserts that the given expression is true.
  • refute(expression) - Asserts that the given expression is false.
  • assert_equal(expected, actual) - Asserts that the expected and actual values are equal.
  • refute_equal(non_expected, actual) - Asserts that the non-expected and actual values are not equal.
  • assert_raise(exception, function) - Asserts that the given function raises the specified exception.

Skipping Tests[edit]

ExUnit allows you to skip specific tests using the @tag attribute. You can add the @tag :skip attribute to a test or a whole test case to skip it during test execution. Here is an example:

elixir defmodule MyTest do

 use ExUnit.Case
 @tag :skip
 test "this test will be skipped" do
   assert true
 end

end

Test Coverage[edit]

Test coverage is an important aspect of testing. ExUnit provides test coverage analysis using the mix coveralls command. This command generates a test coverage report for your project. Here is an example of how to use it:

shell mix coveralls

Further Reading[edit]

To explore more about ExUnit and testing in Elixir, check out the following articles:

Conclusion[edit]

ExUnit provides a powerful and flexible testing framework for Elixir projects. This documentation should have provided you with an in-depth understanding of its features and how to effectively use them. Start writing tests for your projects and ensure the stability and correctness of your code. Happy testing!