Editing
Elixir Testing with ExUnit
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Elixir Testing with ExUnit == === Introduction === ExUnit is a powerful testing framework in the Elixir programming language that allows developers to write and run tests to ensure the correctness of their code. It provides a simple and intuitive syntax for describing and organizing tests, making it easy to write comprehensive test suites. === Getting Started === To use ExUnit, you need to include the `ExUnit.Case` module and define a test case. Each test case is a module that uses `ExUnit.Case` and contains one or more test functions. These test functions should be defined with `test` followed by a meaningful name and a do block. ```elixir defmodule MyTestCase do use ExUnit.Case test "addition" do assert 1 + 1 == 2 end end ``` === Asserting and Refuting === ExUnit provides a wide range of assertions to validate the behavior of your code. These assertions can be used to check equality, inequality, type, exceptions, and more. Additionally, ExUnit allows you to refute assertions to ensure certain conditions are not met. ```elixir defmodule MyTestCase do use ExUnit.Case test "equality" do assert 1 + 1 == 2 refute 1 + 1 == 3 end test "type" do assert is_binary("hello") refute is_list("hello") end end ``` === Test Organization === ExUnit supports the organization of tests into test modules and test cases. By grouping related tests, you can easily manage and run specific subsets of your tests. ```elixir defmodule MathTests do use ExUnit.Case, async: true describe "addition" do test "positive numbers" do assert 1 + 1 == 2 end test "negative numbers" do assert -1 + -1 == -2 end end describe "subtraction" do test "positive numbers" do assert 5 - 3 == 2 end test "negative numbers" do assert -5 - -3 == -2 end end end ``` === Test Setup and Teardown === ExUnit allows you to define setup and teardown functions to prepare the test context before each test and clean up afterwards. These functions can be defined using `setup` and `teardown`, respectively. ```elixir defmodule MyTestCase do use ExUnit.Case setup do {:ok, socket: SocketServer.start_link()} end teardown %{socket: socket} do :ok = SocketServer.stop(socket) end test "sending data" do assert :ok = SocketServer.send_data(socket, "Hello, world!") end end ``` === Running Tests === To run tests using ExUnit, you can use the `mix test` command in your project's root directory. This will run all the tests defined in your project. Additionally, you can use `mix test path/to/test_file.exs` to run a specific test file. ExUnit provides several options to customize the test execution, such as running tests in parallel, filtering tests by tags, and more. Refer to the [https://elixir-lang.org/docs/stable/ExUnit.html ExUnit documentation] for more details. === Conclusion === ExUnit is a robust testing framework for Elixir that empowers developers to write clear and comprehensive test suites. By utilizing the features provided by ExUnit, you can ensure the correctness and reliability of your Elixir code. Happy testing! === See Also === * [Link to another relevant article] * [Link to another relevant article] * [Link to another relevant article]
Summary:
Please note that all contributions to Elixir Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Elixir Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information